graphicstrigonometryeigen3coordinate-systems

Find angle of rotation, inverted y axis, between two points in 2D


Background : I am implementing an application where I need to do affine transformations on a selected line. Line is represented using 2 points. All the code is done in mouse events.

void mousePressEvent(){
    lastPoint = event.point();
}
void mouseMoveEvent(){
    currentPoint = event.point();

    //Do transformations here.

    translate_factor = currentPoint - lastPoint
    scale_factor = currentPoint / lastPoint

    rotation_angle = f(current_point,last_point) //FIND f

    //Apply transformation
    lastPoint = currentPoint
}

I have two points, p1 and p2. The X axis increases from left to right. Y axis increases top to bottom. I have a point d around which I want to rotate a set of points p_set .
How do I find the angle of rotation from p1 to p2 about d .

What I am doing is translate p1 , p2 by -d and calculate theta by using atan2f as shown in the following code.

Note that y is inverted : goes from top to bottom. Here is the code

const auto about = stroke->getMidPoint();
Eigen::Affine2f t1(Eigen::Translation2f(0-about.x(),0-about.y()));

Eigen::Vector3f p1 = t1.matrix()*Eigen::Vector3f(lastPoint.x(),lastPoint.y(),1.0);
Eigen::Vector3f p2 = t1.matrix()*Eigen::Vector3f(currentPoint.x(),currentPoint.y(),1.0);

float theta = atan2f(p2[1]-p1[1],p2[0]-p1[0]);

When I transform using this, I get very weird rotations. What I want is to find angle as a function of current and last points


Solution

  • It seems to me that you're finding the slope of the vector p2-p1. If I understood correctly, you want the difference of the slopes of the two vectors p2-d and p1-d, so the rotation angle is something like atan((p2.y-d.y)/(p2.x-d.x)) - atan((p1.y-d.y)/(p1.x-d.x))