, ( , ) ( ). ( ) .
, :
rotate(anglenow - angle0)
:
( ) , .
(getAngle (x1, y1, x2, y2). ( , x y, ) arctan (dy/dx).
dy/dx, :
+ / + -> +
+ / - -> -
- / + -> -
- / - -> +
. , - .
arctan doc , , ( 0 pi -pi/2 + pi/2), dx dy ( , arctan ) / pi .
getAngle, 360º.
Edit
:
Math.atan -pi/2 pi/2.
, , 0 X, , , , . , .
dx = xtarget - xorigin ( ), , , - .
, dy < 0, pi . -pi/2 3pi/2. , (-pi, pi) (0,2pi).
: , , !
onmousedown {
startpoint = (x,y);
startangle = getAngle(origin, startpoint);
}
onmousemove {
currentpoint = (x,y);
currentangle = getAngle(origin, currentpoint);
originalimage.rotate(currentangle - startangle);
}
getAngle(origin, other) {
dy = other.y - origin.y;
dx = other.x - origin.x;
if (dx == 0) // special case
angle = dy >= 0? PI/2: -PI/2;
else
{
angle = Math.atan(dy/dx);
if (dx < 0) // hemisphere correction
angle += PI;
}
// all between 0 and 2PI
if (angle < 0) // between -PI/2 and 0
angle += 2*PI;
return angle;
}