Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Spirrwell   Members   -  Reputation: 265

Like
0Likes
Like

Posted 16 November 2012 - 01:22 PM

[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:

Posted Image

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:

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;
}


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

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();

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

Posted Image

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?

Sponsor:

#2 Sik_the_hedgehog   Crossbones+   -  Reputation: 1747

Like
0Likes
Like

Posted 16 November 2012 - 02:49 PM

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, 16 November 2012 - 02:51 PM.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#3 Spirrwell   Members   -  Reputation: 265

Like
0Likes
Like

Posted 16 November 2012 - 03:13 PM

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.

#4 de_mattT   Members   -  Reputation: 308

Like
2Likes
Like

Posted 16 November 2012 - 03:19 PM

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


I think your y values might be the problem.
Your y values are 0-0 - r (the circumRadius)
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

#5 de_mattT   Members   -  Reputation: 308

Like
0Likes
Like

Posted 16 November 2012 - 03:30 PM

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)

public static Polygon createRegularPolygon(int numberOfSides,
   double sideLength, double dblStartAngleInDegrees)
{
   // slice angle
  double dblSliceAngle = (2 * Math.PI) / numberOfSides;
 
  // calculate circum radius
  double dblRad = sideLength / (2 * Math.cos( (180 - dblSliceAngle) / 2);

  // calculate the start angle in radians for repeated use in the for loop
  double startAngleInRadians = Math.toRadians(dblStartAngleInDegrees);
 
  // 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...
  }
}

Matt

#6 Spirrwell   Members   -  Reputation: 265

Like
0Likes
Like

Posted 16 November 2012 - 10:47 PM

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


I think your y values might be the problem.
Your y values are 0-0 - r (the circumRadius)
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....Regular_polygon

Hope this works.

Matt


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: h = sqrt(pow(r, 2.0) + pow(FindMissingSide(r, r, 360.0/slices)/2, 2.0) That plus sign should be a subtraction one. That's at least how I did it...




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS