Skip to content

How to rotate a polygon along any axis

Daniel

Asked Jul 29, 2010, 8:55 AM EDT1 answer
Share
LinkedInX
I need to rotate a simple 2D polygon along any axis (to mimic 3D rotation).  The polygon is draw on a canvas using lines (VS 2010). I need to keep track of all coordinates during rotation.

I was able to write code for rotating an hexagone around x or y axis using simple math where theta is the angle from the plane XY axis, Phi from the XZ plane.

(rotating around x axis)
pt.X = ptCentre.X + r * cos(theta);
pt.Y = ptCentre.Y + r * sin(theta) * cos(Phi);

(rotating around y axis)

pt.X = ptCentre.X +  r * sin(theta) * cos(Phi);
pt.Y = ptCentre.Y + r * cos(theta);

This will not work for rotating the hexagon at 45 degree angle. I am not able to find the math/code for doing rotations at any angle, not just around x or y axis.
          

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    try using PI for your rotation,
    const double dHalfPi = 2.0 * atan(1.0);
    const double dPi = 4.0 * atan(1.0);
    const double dQuarterPi = atan(1.0);
    const double dTwoPi = 8.0 * atan(1.0);
     
    0