Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate random vertices to form a convex polygon in c++?

I need to generate a set of vertices for a simple convex polygon to do a minimum weight triangluation for that polygon using dynamic programming , I thought about taking a circle of radius r and then take 20 vertices moving counter clock wise and then i will form a 20 vertex convex polygon but i how can i do that

How would i know the vertex that lies on a circle of radius r ?

and is there another easier way of generating vertices for convex polygon other than that way

Any help greatly appreciated

like image 835
user3196567 Avatar asked Feb 10 '14 23:02

user3196567


1 Answers

Generate your 20 random numbers between 0 and 2*pi, and sort them.

Now use a little basic trigonometry to convert to X,Y coordinates.

for (int i = 0; i < 20; i++)
{
    x = x0 + r*cos(angle[i]);
    y = y0 + r*sin(angle[i]);
    // ...
}
like image 126
Mark Ransom Avatar answered Oct 13 '22 23:10

Mark Ransom