Graphics 求二维两点之间的旋转角度,y轴反转
背景: 我正在实现一个应用程序,其中我需要在选定的行上进行仿射变换。线用2个点表示。所有代码都是在鼠标事件中完成的Graphics 求二维两点之间的旋转角度,y轴反转,graphics,trigonometry,eigen3,coordinate-systems,Graphics,Trigonometry,Eigen3,Coordinate Systems,背景: 我正在实现一个应用程序,其中我需要在选定的行上进行仿射变换。线用2个点表示。所有代码都是在鼠标事件中完成的 void mousePressEvent(){ lastPoint = event.point(); } void mouseMoveEvent(){ currentPoint = event.point(); //Do transformations here. translate_factor = currentPoint - lastPoi
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
}
我有两点,p1
和p2
。X轴从左向右增加。Y轴从上到下递增。我有一个点d
,我想围绕它旋转一组点p\u集
如何找到从
p1
到p2
关于d
的旋转角度
我所做的是通过-d
翻译p1
,p2
,并通过使用atan2f
计算θ,如下代码所示
请注意,y是反向的:从上到下。
这是密码
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]);
当我用这个变换时,我得到了非常奇怪的旋转。
我想要的是找到角度,作为当前点和最后点的函数,在我看来,你找到的是向量p2-p1的斜率。
如果我理解正确,你需要两个向量的斜率之差
p2-d
和p1-d
,因此旋转角度类似于atan((p2.y-d.y)/(p2.x-d.x))-atan((p1.y-d.y)/(p1.x-d.x))
在我看来,你正在寻找向量的斜率p2-p1
。
如果我理解正确,您需要两个向量的斜率之差
p2-d
和p1-d
,因此旋转角度类似于atan((p2.y-d.y)/(p2.x-d.x))-atan((p1.y-d.y)/(p1.x-d.x)) >/P>听起来像是一个三角问题而不是C++问题…你关心旋转方向吗?你可以走任何一条路,也可以多个转弯。这很重要吗?@peteblackert第三次我添加了我需要这个的背景。是的,它们听起来像是一个三角问题而不是C++问题……你关心旋转方向吗?你可以走任何一条路,也可以多个转弯。这很重要吗?@peteblackert第三次我添加了我需要这个的背景。是的,它们很重要。工作。谢谢,是的。工作。谢谢