Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Points on a rotated rectangle

I'm attempting to calculate the bottom-left point in a rectangle as it is rotated around. I've attempted to Google it, but apparently I'm missing something. I'm attempting to use a transformation matrix to calculate the point.

For my setup, I have a rectangle clip called "test" and a clip called "pnt" that I'm trying to keep on the lower left point. Here is the code for my demo. I've just thrown this onto the first frame of the timeline to test:

//declare initial position of points
pnt.x = (test.x - test.width/2);
pnt.y = (test.y + test.height/2);

//distance between corner and center
var dx:Number = pnt.x - test.x;
var dy:Number = pnt.y - test.y;

addEventListener(Event.ENTER_FRAME,rotate);


//x' = xc + dx cos(theta) - dy sin(theta)
//y' = yc + dx sin(theta) + dy cos(theta)
function rotate(e:Event):void{
    test.rotation++;

    // use the transformation matrix to calculate the new x and y of the corner
    pnt.x = test.x + dx*Math.cos(test.rotation*(Math.PI/180)) - dy*Math.sin(test.rotation*(Math.PI/180));
    pnt.y = test.y + dx*Math.sin(test.rotation*(Math.PI/180)) + dy*Math.cos(test.rotation*(Math.PI/180));

    trace("X: " + Math.cos(rotation));
    trace("Y: " + pnt.y);
    // calculate the new distance to the center
    dx = pnt.x - test.x;
    dy = pnt.y - test.y;
}
like image 421
Snukus Avatar asked Oct 20 '25 09:10

Snukus


1 Answers

We can model the trajectory of a single point by

(x',y') = (xc + r cos(theta + theta0), yc + r sin(theta + theta0))

where

(x', y') = new position
(xc, yc) = center point things rotate around
(x, y) = initial point
r = distance between (x,y) and (xc, yc)
theta = counterclockwise rotation, in radians
theta0 = initial rotation of (x,y), in radians

Our initial point tells us that

r sin(theta0) = (y - yc) 
r cos(theta0) = (x - xc)

By the power of trigonomerty:

r cos(theta + theta0) =
r cos(theta)cos(theta0) - r sin(theta)sin(theta0) = 
cos(theta)(x - xc) - sin(theta)(y - yc)

and

r sin(theta + theta0) = 
r sin(theta)cos(theta0) + r cos(theta)sint(theta0)
sin(theta)(x - xc) + cos(theta)(y - yc)

Therefore, given

  1. The center point (xc, yc) that stuff is rotating around
  2. The point to track (x, y) - (your rectangle corner)
  3. A rotation theta, in radians

The new position of the point will be:

x' = xc + dx cos(theta) - dy sin(theta)
y' = yc + dx sin(theta) + dy cos(theta)

with dx and dy given by

dx = x - xc
dy = y - yc    
like image 91
hugomg Avatar answered Oct 22 '25 04:10

hugomg