Drawing 2D curves in Android canvas? - android

How we can draw 2D curves like this in Canvas?
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?

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

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).

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

Related

How to efficiently draw vector curves on mobile devices?

I was working on a vector based mobile app. First I've started using polygons to represent Curves. However, I was reaching quickly the Polygon Limit on mobile phones. To overcome this limit, I've started using a Texture and was coloring the pixels. Even though this was a pretty easy solution, I was limited by the max resolution of textures and operations.
The only promising thing I've found was OpenVG, but it seems like it is not very popular.
So how are vector drawing apps on mobile phones created? I was stunned by Adobe Illustrator mobile, which seems to be able to draw limitless curves/lines in vector graphics.
One possible method to allow vector based free form curve drawing is to use Bézier curves. Bezier curves are constructed by interpolating values defined by a start point, an end point and any number of control points. This results in the ability to construct a free form curve from a set of as little as 3 cartesian points.
A benefit of this is that by only storing tthe point data representing the curve, you can then render the same curve at any scale without having to store the curve in a texture. Therefore you do not need to store hundreds of intermediate points to form small line segments to represent the same curve.
Here are a number of sample code snippets that use the Path object in Android to construct free form vector curves.
If you have a very large number of curves to render to the canvas, you only need to store the point data defining the bezier curve. It is important that you only create the Path object once, and use the reset method to redefine the points each time you would like to draw a new curve. Sample code to achieve this can be found here.

Digging a polygon according to another with libgdx

I actually develop a 3D application on Android, and i manipulate 3D object.
I want to dig a polygon according to another polygon form such as a cube or a cylinder.
In the following image you can imagine I have two superimposed polygons, a cylinder and a rectangular cuboid.
If I get the intersection of these two polygons and I calculate the inverse (included in the rectangular cuboid) I get a new polygon with a hole in the shape of the cylinder like in this picture.
I don't know how to achieve this with libGDX, I know the Intersector class, but I do not know how to retrieve the reverse of an intersection between two polygons.

opencv - Detect and optimize shapes

I have a challenge where I have to implement a sample apps on Android. This app allows user draw some shapes on the screen (by finger) and then optimize it to vertex based shapes. I am thinking about Opencv but I don't know what function/feature can help me detect a shape and optimize it. I need some hints or recommendations.
Update: User can draw any type of shapes such as: Circle, ellipse, rectangle, line, curve, polygon, ... So I think it should have an general algorithm for all kind of shapes.
Thanks.

Spinning globe in Opengl-es

I need to draw a spinning globe using opengl es in android. I think we need to draw a sphere and then apply a texture map on it. If I am correct, we cannot use the utility library glu in Opengl ES for drawing a sphere. I did find some code in objective C, but then I would have to make it work on android.
http://www.iphone4gnew.com/procedural-spheres-in-opengl-es.html
Is there any other way to do this ? I'm no sure how to approach this problem, Could you give me some inputs that would set me looking in the right direction.
Thanks
You could actually create your own sphere rendering function.
A tesselated sphere is no more then a stack of n cone segments, each approximated with m slices.
This image (courtsey of dglwiki.de) illustrates this:
(the german text translates to 'If the resolution is to low, the sphere degenerates to other symetric Bodies)
In order to construct the sphere, you'll need to specify the center point, radius, number of stacks and number of slices per stack.
The first pole of your sphere can be any point with a distance of radius from the center point. The vector from this point to the center point defines your sphere's axis of rotation (and thereby the position of the second pole)
Next, you'll need to approximate several equidistant circles of latitude on your sphere around the axis of rotation. The number of circles should be number of stacks -1. Each of these circles should have as much vertices as your desired number of slices.
Having calculated these, you have enough geometry information to construct your spheres faces.
Begin with a triangle fan originating at one of the poles using the vertices of the first circle.
Then, construct Triangle strips for each pair of neighbouring circles of latitude. The last step is to construct another triangle fan from the second pole to the last of your circles of latitude.
Using this approach, you can generate arbitrary spheres of arbitrary smoothness
In addition to what sum1 says, the link you provide to obj-C code is mostly just C, which translates quite nicely to Java/android. The technique provided is very similar to the one sum1 suggests, although the author uses only one fan at the top, then draws the entire remainder of the sphere with a single triangle strip. In addition, his globe is "laying on its side", with the fan at the "East pole" and the other point at the "west pole."
However, you can either use the link you provide as-is, or make the adjustments easily enough.

Android 2D opengl line circle

I am new to open gl, and have been trying to do some basic 2d openGL in android. I am able to set up my 2D view, and draw squares and triangles. I am trying to draw a circle and am not exactly sure how to do it. I have found several techniques while searching, one using triangles rotated around the center w/ the given radius, this will not work as I do not want a filled circle. I also found other suggestions to do this with lines moving around the outer edge of the circle.
I have chosen to implement the latter. The issue I am having is with the IndexBuffer that is passed into glDrawElements, if my circle(lines) buffer has too many points, I am unable to create the byte array to create the IndexBuffer, as the maximum value for a byte is 127, any help or direction on how to do this would be appreciated.
Use an IntBuffer, that should allow you to use as many indices as you'll ever need.

Categories

Resources