leahayes00 122 Report post Posted September 20, 2004 Hi, I have a basic data structure like this: struct POINT { float x, y; }; and an array like this: vector<POINT> CirclePoints; How can I generate a circle, i.e. calculate the vertex positions, when the circle has n vertices or s sides? When the circle has three sides it should be like this: (Ignore the _ symbols) ___. __._. Four sides: ___. __._. ___. Five sides: ____. __.___. ___. . Six sides: _____. . ___._____. _____. . etc... Any help would be greatly appreciated! Best Regards, Lea Hayes 0 Share this post Link to post Share on other sites
mike25025 494 Report post Posted September 20, 2004 for num of sides n do add point i (x = sin(i*360/n),y = cos(i*360/n))hope that helps 0 Share this post Link to post Share on other sites
nmi 978 Report post Posted September 20, 2004 Quote:Original post by mike25025for num of sides n do add point (x = sin(360/n),y = cos(360/n))hope that helpsfor (i = 0; i < n; i++){ xi = sin(i*360/n),yi = cos(i*360/n)} 0 Share this post Link to post Share on other sites
grhodes_at_work 1385 Report post Posted September 21, 2004 Actually, do this instead. I'm assuming vector is std::vector.N = number of pointsTWOPI = 6.28319;// reserve memory to avoid lots of memory reallocsCirclePoints.reserve(N);// make sure vector is empty. Do NOT do a clear()!CirclePoints.resize(0);POINT center; // centerpointPOINT newpoint;for (i = 0; i < N; i++){ newpoint.x = center.x + radius * cosf(i * TWOPI / N); newpoint.y = center.y + radius * sinf(i * TWOPI / N); CirclePoints.push_back(POINT}Two things. First, I'm using TWOPI instead of 360 since most compilers require an angle in radians when you call cos/sin. 360 is degrees, TWOPI is radians.Second, that loop will generate a circle that is not closed. If you want enough points to close the circle (e.g., if you draw a line between points 0 and 1, 1 and 2, ..., N-1 and N, you end up with a closed circle, then change the N in the denominator of the newpoint.x/y equations to be (N-1) instead. 0 Share this post Link to post Share on other sites