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;
}
For those of you who found this as I did via the Google...
Here is the above answer in JavaScript/jQuery form, where
$element
is the$('#element')
jQuery object,iDegrees
is the angle you wish to rotate,iX
/iY
are the coordinates to the point you wish to know the destination of, andiCenterXPercent
/iCenterYPercent
represent the percentage into the element (as per CSS'stransform-origin
) where the rotation will occur:eg:
Where does the top-left corner of
<div id='element'>...</div>
end up when rotated by 45 degrees around it's bottom-left corner?We can model the trajectory of a single point by
where
Our initial point tells us that
By the power of trigonomerty:
and
Therefore, given
(xc, yc)
that stuff is rotating around(x, y)
- (your rectangle corner)theta
, in radiansThe new position of the point will be:
with
dx
anddy
given by