# polygon rotation aound their own centers

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay, first of all, this is for an assignment, so don't give me the answer straight out if you can avoid it (this is only a small part of the assignment). Most of the assignment background doesn't matter but I basically have an array of points that represent polygons. Eg, depending on the number of points each polygon has, the first n points is the first polygon, the next n points is the second polygon, etc, etc. I need to be able to rotate each of the polygons around their own center by the same amount. I didn't think it would be that hard, but I'm hitting some roadblocks. At the moment, it's a large triangle and I'm creating <insert name here I can't remember>'s Gasket, where the whole shape is subdivided into smaller and smaller shapes of the same type and I need all of the smaller ones to rotate constantly.
// if we are rotating the individual polys of the gasket
if( rotateMode ){
//	glPushMatrix();
// set rotation on the z axis

// increase the individual poly rotation

// restrict it to [0,360]
}

// because every 'num_poly_points' is a single polygon. I have to draw each poly seperately.
for(int i=0; i < vertexList[gasket_depth].getSize(); i+=num_polygon_points){

if( rotateMode ){
glPushMatrix();
}

// draw a single polygon
glBegin(draw_mode);
for(int j=0; j < num_polygon_points; ++j)
glEnd();

if( rotateMode )
glPopMatrix();
}


So what I'm ending up doing is just rotating the whole thing. Any hints? Edit:: I was hoping that I wouldn't have to do something like keep seperate translation info on each of the polys. [Edited by - Endar on April 6, 2006 3:15:16 AM]

##### Share on other sites
u will need to translate first to the center of each polygon before u do rotation
u will need to store/restor the matrix state for each polygon (push/pop)

##### Share on other sites
I was afraid of that.

Any idea how to find the center of a polygon given it's points?

##### Share on other sites
Quote:
 Original post by EndarAny idea how to find the center of a polygon given it's points?

Assuming all the points lie within a single plane, and the polygon is convex, simply averaging them might work...

##### Share on other sites
Really? I mean, currently all the points are an equal distance away from the center (the distance and angles for each edge are the same), but later on we have to make it possible to change that.

Won't this have an impact on this?

##### Share on other sites
Finding the centre of the polygon depends on the polygon itself. For eg:- if you have a square or rectangle then the interesectiong of the diagonals will be its centre. Similary for a triangle the centre would be the intersection of the lines drawn from the centre of line to its opposite vertex. It would be easier to push and pop actually than trying to find the centre of a polygon because then instead of finding the centre, you already have it - i.e. at 0, 0, 0.

##### Share on other sites
Quote:
 Original post by CRACK123It would be easier to push and pop actually than trying to find the centre of a polygon because then instead of finding the centre, you already have it - i.e. at 0, 0, 0.

Yeah, but won't I have to know the center point for each polygon so I can translate it by it's center to it's correct position?

##### Share on other sites
Sounds like a Sierpinski Gasket to me.

bpoint is correct about averaging the vertices of a planar convex polygon. It also works on things like parallelograms and trapezoids which don't have vertices that are equally spaced from the center and the angles are not the same.

DG

##### Share on other sites
Quote:
 Original post by Disgruntled GamerSounds like a Sierpinski Gasket to me.bpoint is correct about averaging the vertices of a planar convex polygon. It also works on things like parallelograms and trapezoids which don't have vertices that are equally spaced from the center and the angles are not the same.DG

Yep, that's the one. Thanks guys. Do you want credit in my assignment for this?

##### Share on other sites
Paul Bourke's geometry page has equations describing how to calculate the centroid of a polygon, in case you need it.

Quote:
 Do you want credit in my assignment for this?

For me, it's not necessary. I come here helping out for fun. :)

##### Share on other sites
:D

It's only fair. I won't feel right claiming that every inch of this was my work. Which won't be a problem. We only lose marks (or rather don't get any marks for the bit) when the code we use wasn't ours.

Anyway, I've come up with another slight problem.

Here's the basic algorithm of what I'm doing:
pushMatrix();vec3 sum;vec3 templist[];loop        sum the poly point into 'sum';        store the poly point in templist;end loopsum /= num_poly_points;        // to get the center of the polyloop        templist[i] -= sum;    // to translate the poly to the originend loopglTranslate(sum.x, sum.y, sum.z);        // to try and place the poly back where it should beglRotate(rotation, 0.0f, 0.0f, 1.0f);    // but rotated a bit.// draw around the origin (so the translation and rotation will do what we want)loop        glVertex3fv(templist[i]);end looppopMatrix();

That's basically what I'm doing and what I'm getting (not too surprising now that I think about it) is a single triangle that is centered at the origin and as I increase the depth of the gasket (increase the complexity) the triangle gets smaller (as the complexity increases, the triangles added to the final shape get smaller for each complexity level), and it stays only a single triangle.

Isn't this the way to do it? Make sure that you draw around the origin so the translation and rotation have the proper effect? Because it looks like all the triangles are getting translated to around the origin, and because I think that they should be the same size, we only see 1 triangle.

##### Share on other sites
I'm not sure if I totally understand your assignment, but it sounds like you may need translate the triangle back to its original position after you rotate it around the origin.

From the code you gave it looks like you translate to the origin then rotate and then draw so everything is drawn centered at the origin and rotated.

Hope this helps.

##### Share on other sites
Quote:
 Original post by Disgruntled GamerI'm not sure if I totally understand your assignment, but it sounds like you may need translate the triangle back to its original position after you rotate it around the origin.From the code you gave it looks like you translate to the origin then rotate and then draw so everything is drawn centered at the origin and rotated.Hope this helps.

Well, I found the problem. I was calculating everything between a glBegin(), glEnd() pair of calls, so, of course when I applied the transforms through glRotate and glTranslate, it didn't do anything and generated a nice GL_INVALID_OPERATION error. But everything did work as I had it, I just had to move all the calculations out from between the glBegin(), glEnd() calls.

But, all fixed now, and looking good.