【问题标题】:Find angle of rotation, inverted y axis, between two points in 2D查找二维中两点之间的旋转角度,倒置 y 轴
【发布时间】:2021-04-06 15:59:02
【问题描述】:

背景: 我正在实现一个应用程序,我需要在选定的行上进行仿射变换。线用 2 个点表示。所有代码都在鼠标事件中完成。

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
}

我有两点,p1p2。 X 轴从左到右递增。 Y 轴自上而下递增。我有一个点d,我想围绕它旋转一组点p_set
如何找到从p1p2 关于d 的旋转角度。

我正在做的是将p1p2 转换为-d 并使用atan2f 计算theta,如下面的代码所示。

请注意,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]);

当我使用它进行变换时,我得到了非常奇怪的旋转。 我想要的是找到角度作为当前点和最后一点的函数

【问题讨论】:

  • 听起来更像是三角问题而不是 C++ 问题...
  • 你关心旋转方向吗?您可以选择任意一种方式,也可以转多圈。这很重要吗?
  • @PeteBlackerThe3rd 我已经添加了我需要这个的背景。是的,它们很重要

标签: graphics trigonometry eigen3 coordinate-systems


【解决方案1】:

在我看来,您正在寻找向量p2-p1 的斜率。 如果我理解正确,你想要p2-dp1-d 这两个向量的斜率之差,所以旋转角度类似于atan((p2.y-d.y)/(p2.x-d.x)) - atan((p1.y-d.y)/(p1.x-d.x))

【讨论】:

  • 是的。工作。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 2021-12-22
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多