dr4cul4
dr4cul4

Reputation: 268

Convert shape to polygons

Is there any simple (or not) algorithm that is capable of creating polygons from closed path?

Assume w have following path:

0,0; 2,0, 2,1; 1,1;
1,2; 2,2; 2,3; 0,3;

I need to be able to create polygon indexes for OpenGL vertex buffer. Language I'm using is C#.

Someone suggested me Convex Hull, but it's not the thing I'm looking for, cause I have a shape already. I know that this could be a trivial issue, but seriously, I can't find any description or something to point mi in the right direction.

Edit:

Answer 1 suggest to select a point and connect it to other not connected points, this works fine for presented in answer shape but it will not work for shape i posted, shape above looks like this:

Shape

Upvotes: 0

Views: 1262

Answers (1)

Jacob Parker
Jacob Parker

Reputation: 2556

Converting to triangles is either easy or hard, depending on what your requirements are and how well you want to do it.

If your polygon is convex, the easiest way is to use GL_TRIANGLES with the indicies

0, 1, 2,   0, 2, 3,  0, 3, 4, ...

It will look like this:

triangulation by fanning

The situation for concave is more work. An algorithm that also works for concave polygons (not ones with holes!) is the Ear-clipping method, described on wikipedia (there is more on that page.)

Things get really interesting when you want a "good" triangulation: avoid skinny or small triangles, reduce number of triangles etc., and then you can trade off quality for speed. I won't go into any advanced algorithms here; searching for polygon triangulation on Google will get you lots of info.

As for normals, if your polygon is planar (it most likely "should" be) then take two non co-incident edges and cross product them (there are two normals and you probably only want one: you need to choose the order in which you cross product (clockwise or anticlockwise) based on the right hand rule.

Upvotes: 2

Related Questions