polygon rotation aound their own centers

Started by
11 comments, last by Endar 18 years ago
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]
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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)
I was afraid of that.

Any idea how to find the center of a polygon given it's points?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Quote:Original post by Endar
Any 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...
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?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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.


The more applications I write, more I find out how less I know
Quote:Original post by CRACK123
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.


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?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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
Quote:Original post by Disgruntled Gamer
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


Yep, that's the one. Thanks guys. Do you want credit in my assignment for this?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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. :)

This topic is closed to new replies.

Advertisement