# [SOLVED]Triangular Rotation\Creating a "Circle" out of Triangles

## Recommended Posts

MichaelBarth    341
[Trigonometric Knowledge Useful!]

Alright, I'm trying to create my own version of a circle by using triangles, I guess right now my intended circle will be an octagon because it has 8 sides, well it will.

So far I have this one triangle:

[img]http://i610.photobucket.com/albums/tt190/Spirrwell/Screenshotfrom2012-11-16135637.png[/img]

The two sides on the left and right is known as r for radius which is equal to half of the window's height. Since I was going to try to deal with a circle with 8 "slices" the angle measure of the bottom angle has to be 45 degrees. and I use that to find the length of the top side which is done with this function:

[CODE]
double FindMissingSide(double a, double b, double C)
{
//ToRadians is a variable which is PI/180.0
double c = sqrt(pow(a, 2.0) + pow(b, 2.0) - 2 * a * b * cos(C * ToRadians));
return c;
}
[/CODE]

I use it in my drawing code (Yes I know I'm calculating the unknown side every time, but this is a small project so I'm not worried):

[CODE]
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glTranslated((double)ScreenWidth/2.0, (double)ScreenHeight/2.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex2d(0.0 - (FindMissingSide(r, r, 360.0/slices)/2), 0.0 - r);
glVertex2d(0.0 + (FindMissingSide(r, r, 360.0/slices)/2), 0.0 - r);
glVertex2d(0.0, 0.0);
glEnd();
glPopMatrix();
[/CODE]

Now before I went and created a for loop, I decided I'd create another triangle and rotate 45 degrees with [i]glRotated(45.0, 0.0, 0.0, 1.0);[/i], which I would expect the two triangles to share one side, but it has a gap of about 3 degrees:

[img]http://i610.photobucket.com/albums/tt190/Spirrwell/Screenshotfrom2012-11-16135708.png[/img]

Now I said it seemed to have a gap of 3 degrees because if I set the x rotation to 42, it shares the side, but then it's off by 3 degrees, and the next triangle I would have to subtract 6 degrees because I already subtracted 3 from the last rotation.

I don't understand, I must be doing something plainly stupid and I don't see it. I would think that I must be calculating the missing side wrong somehow...

Any ideas?

##### Share on other sites
Wouldn't it be easier to just calculate where the vertices should be, instead of making triangles and rotating them? Then you'd have one vertex in the middle and the vertices in the outside - which you can calculate using sines and cosines (and a multiplication), no need for square root at all.

Unless there's some extra requirement here I'm missing.

EDIT: also that still wouldn't be the optimal solution since you can ditch the middle vertex and just make all the triangles touch one of the vertices in the outside (while also reducing triangle count in the process!), but then it wouldn't be split like pizza slices (which may or may not matter for your project). Edited by Sik_the_hedgehog

##### Share on other sites
MichaelBarth    341
Because I'm stupid and I don't know how. I actually haven't even learned trigonometric math, I had to teach myself some stuff because I'm still in high school in my first semester of geometry. I'm trying to teach myself how this works that's why I'm working on it the only way I know how. I mean I could do this using GLU\GLUT or whatever to take the easy way out, but I'm just trying to figure out it works.

##### Share on other sites
de_mattT    308
[quote]glVertex2d(0.0 - (FindMissingSide(r, r, 360.0/slices)/2), 0.0 - r);
glVertex2d(0.0 + (FindMissingSide(r, r, 360.0/slices)/2), 0.0 - r);[/quote]

I think your y values might be the problem.
But what you want is 0-0 - h (the Apothem) - the height of the triangle in your case.

Where h is calculated using pythagoros as:
h = sqrt(pow(r, 2.0) + pow(FindMissingSide(r, r, 360.0/slices)/2, 2.0)

Wikipedia's got a good description of regular polygon's which I found pretty useful. http://en.wikipedia.org/wiki/Regular_polygon

Hope this works.

Matt

##### Share on other sites
de_mattT    308
Here's the algorithm I'm using to perform the same kind of thing you're doing but closer to the way suggested by #2 Sik_the_hedgehog. I've inlined some support methods so it's all in one place so hopefully it still works .

(Java code)

[CODE]
public static Polygon createRegularPolygon(int numberOfSides,
double sideLength, double dblStartAngleInDegrees)
{
// slice angle
double dblSliceAngle = (2 * Math.PI) / numberOfSides;

double dblRad = sideLength / (2 * Math.cos( (180 - dblSliceAngle) / 2);

// calculate the start angle in radians for repeated use in the for loop

// now define the polygon
for (int i = 0; i < numberOfSides; ++i)
{
double dblAngle = startAngleInRadians + (dblSliceAngle * i);
int x = (int) Math.round(Math.cos(dblAngle) * dblRad);
int y = (int) Math.round(Math.sin(dblAngle) * dblRad);

// store x and y somewhere for later use...
}
}
[/CODE]

Matt

##### Share on other sites
MichaelBarth    341
[quote name='de_mattT' timestamp='1353100774' post='5001640']
[quote]glVertex2d(0.0 - (FindMissingSide(r, r, 360.0/slices)/2), 0.0 - r);
glVertex2d(0.0 + (FindMissingSide(r, r, 360.0/slices)/2), 0.0 - r);[/quote]

I think your y values might be the problem.
But what you want is 0-0 - h (the Apothem) - the height of the triangle in your case.

Where h is calculated using pythagoros as:
h = sqrt(pow(r, 2.0) + pow(FindMissingSide(r, r, 360.0/slices)/2, 2.0)

Wikipedia's got a good description of regular polygon's which I found pretty useful. [url="http://en.wikipedia.org/wiki/Regular_polygon"]http://en.wikipedia....Regular_polygon[/url]

Hope this works.

Matt
[/quote]

Thank you! I could've stared at my work for a year and probably never have noticed that. Since I was thinking circle I thought radius, which would be the same distance from the center in any direction, but because I'm using triangles, that's not true. I guess I accidentally created a faulty syllogism by thinking that way. Again thank you very much! Also, thank you for the extra information, although there was a bug in that one line you gave me: [i]h = sqrt(pow(r, 2.0) + pow(FindMissingSide(r, r, 360.0/slices)/2, 2.0) [/i]That plus sign should be a subtraction one. That's at least how I did it...