Method to Calculate the MidPoint of a shape that is rotated

Started by
2 comments, last by Zouflain 12 years, 2 months ago
Hello

I am developing a HTML5 Canvas Web App & I am having difficulty finding a formula/algorithm that will allow me to find the mid-point of a shape when it is rotated around a certain point.

I am given information about an arrow & I need to draw that arrow on the canvas - it gets tricky when the shape is rotated.

Can you suggest an algorithm I can use to calculate the mid-point of a shape.

The information I am know:
- x,y pos(which is the red dot) - width, height
- Arrow width, Arrow height
- Rotation in degrees
Pv4iF.png
Advertisement
So arrow rotates around it's corner, not center, right?
Do you know center of unrotated arrow?
If you do, then just rotate center and add it to the point.

For example, point is at (10,10), arrow width & height are (20,10), then center of an arrow will be (10,5). Now lets say you rotate it by 45 degrees, that'll be (3.54, 10.6). Add this to your point and you get (13.54, 20.6). That's the new center.
Exactly what "mid point" means is not clear but, whatever it is, it will be a barycenter (a weighted average of points), and barycenters are preserved by affine transformations, which include rotations.

This means that you can either compute the mid point of the unrotated arrow and then rotate it, or you can compute it after the rotation, and you will get the same answer.
I'm assuming this is 2d. Anyway, you need to transform the "mid point" by the rotated translation vector from the center to the midpoint. If this is gibberish, allow me to elaborate in psuedocode:

Vector midpoint = Wherever you define the middle of your arrow. I'm guessing Vector<ArrowSpriteWidth/2,ArrowSpriteHeight/2>
Vector center = The point about which you rotate. In your picture, it looks like one of the corners.
Vector translation = midpoint - center

Now, I'll assume you have some rotation value theta (easy in 2d, eh?). the new midpoint after rotation is simply center+Vector<translation.x*cos(theta), translation.y*sin(theta)>. Unless I'm mistaken.

This topic is closed to new replies.

Advertisement