开发者

How to calculate image rotation from touches?

I'm an iphone developer, but this question is about geometry.

I have a simple rectangle (maybe a photo). The user touches this photo at a point and drags their finger to a new point:

http://dl.dropbox.com/u/792862/Untitleddrawing.png

How many radians I must rotate this rectangle to simulate开发者_开发问答 a rotation given by the touches?


I'm assuming that you have a fixed origin for your rotation (the crosshair in your picture would suggest so) and the touch sets the other point.

First you need a method to figure out the angle of a line. The atan2 function (available in any well-equipped math library) figures out the angle between any line and the X axis. First figure out the starting angle:

startAngle = atan2(startY - originY, startX - originX)

And then the ending angle:

endAngle = atan2(endY - originY, endX - originX)

And then just subtract the two angles from each other to figure out your rotation:

angleToRotate = endAngle - startAngle

You might need to swap things around depending on which way your rotations work.


Use atan2 to convert the starting point and the ending point to angles, after translating each to correct for a non-(0,0) origin if necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜