Jump to content
• Advertisement

# polygon rotation aound their own centers

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

If you intended to correct an error in the post then please contact us.

## 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
//	glRotatef(gasket_polygon_rotation.z, 0.0f, 0.0f, 1.0f);

// increase the individual poly rotation
gasket_polygon_rotation.z += 0.1f;

// restrict it to [0,360]
if( gasket_polygon_rotation.z > 360.0f )
gasket_polygon_rotation.z -= 360.0f;
}

// 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();
glRotatef(gasket_polygon_rotation.z, 0.0f, 0.0f, 1.0f);
}

// draw a single polygon
glBegin(draw_mode);
for(int j=0; j < num_polygon_points; ++j)
glVertex3fv( (float*) &vertexList[gasket_depth][i+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 this post

##### Share on other sites
Advertisement
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 this post

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

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

#### Share this post

##### 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 this post

##### 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 this post

##### 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.

Although I could be wrong about this, thats the best possible explanation I can think of.

#### Share this post

##### 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 this post

##### 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 this post

##### 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 this post

##### 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 this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 12
• 10
• 13
• 104
• 11
• ### Forum Statistics

• Total Topics
632981
• Total Posts
3009687
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!