1

How we can draw 2D curves like this in Canvas?

enter image description here enter image description here enter image description here

All similar curves have mathematical formula or are fractals, but the canvas only has some methods for drawing simple shapes like a triangle or rectangle. I know that most curves can divide into simpler shapes such as with drawing a Heart Curve, but is there an easier way to draw 2D curves with the canvas?

leenephi
  • 900
  • 5
  • 13
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167

3 Answers3

3

Using the Path object you can do some fancier lines and curves in the Canvas. Just randomly found these two questions concerning those; I hope they can point you in the right direction as I personally have not used them.

Draw a perfect curve connecting three points

Bezier curve and canvas

Community
  • 1
  • 1
leenephi
  • 900
  • 5
  • 13
  • Correction: I have used the Path object quite a bit, but not for curves, is what I meant. :) The Path is pretty cool for doing more complicated lines vs the very simple canvas.DrawLine(...) – leenephi Dec 20 '12 at 22:00
  • it seems that it is not a good way to draw fractals.Do you agree? – hasanghaforian Dec 20 '12 at 22:10
  • How exactly do you plan on drawing the fractals? If you're pulling in just data points, then Morrison's suggestion up above might be best. Are you looking for realtime movement or animation or just a fractal image? If the first, you may need to consider a canvas in a SurfaceView or OpenGL. If the latter, I don't see why a Path wouldn't work, because you can add multiple path and curve instructions into a single Path object. – leenephi Dec 20 '12 at 22:31
1

If you look up how to draw 2D curves in general you'll either be drawing points or lines and it only looks smooth.

Your question has two parametric forms and one fractal form. All can be drawn with lines and points. The parametric forms can be done directly via the algorithms in your question or could be transformed into more general ones like the Bezier curve as mentioned by @leenephi

Most of the time the equations/algorithms for generating fractals are using lines, Koch Snowflake, or points, the Mandelbrot set. If you actually understand how to generate them, you'll see that fractals are less about drawing 'a curve' and more about process (recursion) and results (self-similarity).

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
0

You have to approximate the curve by simpler primitives for example short line segments.

Henry
  • 42,982
  • 7
  • 68
  • 84