我有一组点,我想根据它们计算下一个点的位置。为了让您具体了解我到底想要什么,请考虑下图:
对于此图像,我有双眼中心点(显示为黄色),我需要计算绿色中心点的正确位置。
如果头部没有倾斜,这会很容易,因为所有点都位于同一条直线上。然而,如果不考虑角度,我无法准确地近似新点。假设地标如下:
left_eye = {X=128,Y=121}
right_eye = {X=188,Y=81}
现在,为了板着脸,我会做这样的事情:
auto distance = (right_eye_x - left_eye_x) / 4;
auto left_eye_left_corner = left_eye_x - distance;
auto left_eye_right_corner = left_eye_x + distance;
auto right_eye_left_corner = right_eye_x - distance;
auto right_eye_right_corner = right_eye_x + distance;
以及Y
我会使用各自眼睛的 Y 位置。
但是,当头部倾斜时,我显然无法使用此方法。所以我需要计算眼睛之间的角度,然后以某种方式将其合并到 Y 的计算中(也可能包括 X 的计算)。这就是我被困住的地方。
如果我没记错的话,角度可以这样计算 tan = (Y/X)
,因此为了获得实际角度(theta),我们将使用 atan
。 (也解释了here)
现在对于角度,我会写:
auto deltaY = abs(right_eye_Y - left_eye_Y);
auto deltaX = abs(right_eye_X - left_eye_X);
auto angle = atan2(deltaY, deltaX);
现在为了获取新位置,我们应用角度:
new_x = distance * cos(angle)
new_y = distance * sin(angle)
因为我们希望它不与 (0,0)
相关我们用一个点(x0,y0)来偏移它。我认为这是 left_eye
的和right_eye
的X
和Y
分别为坐标。
这就是我陷入困境的地方。其一,与 X
不同轴,我没有 Y
的任何距离这些[绿色]点的轴,因此简单地执行如下所示的操作会惨败!
auto left_eye_left_corner = distance * cos(angle) - left_eye_x;
auto left_eye_left_corner_y = left_eye_y * sin(angle);
auto left_eye_right_corner = distance * cos(angle) + left_eye_x;
auto left_eye_right_corner_y = left_eye_y * sin(angle);
auto right_eye_left_corner = distance * cos(angle) - right_eye_x;
auto right_eye_left_corner_y = right_eye_y * sin(angle);
auto right_eye_right_corner = distance * cos(angle) + right_eye_x;
auto right_eye_right_corner_y = right_eye_y * sin(angle);
我应该如何处理这个问题?
最佳答案
你把事情搞得太复杂了。
取两点:
L = {X=128,Y=121}
R = {X=188,Y=81}
并进行一些 vector 算术:
D = (R-L)/4
LL = L-D
LR = L+D
RL = R-D
RR = R+D
你就完成了。
更迂腐一点(如果你不喜欢 vector 算术,或者不想定义一个Point
类),
Dx = (Rx-Lx)/4
LLx = Lx-Dx
...
依此类推,y 也是如此。
关于c++ - 如何计算两点之间的角度并以此为基础旋转第三点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65639902/