计算两点定义的直线之间的角度 [英] Calculating the angle between the line defined by two points
问题描述
我目前正在开发一个简单的2D游戏为Android。我有多数民众赞成位于屏幕中央固定的物体上,我试图让该对象旋转并指向区域用户触摸屏幕上。我有一个重新present屏幕中心的不断坐标,我可以得到该用户点击该点的坐标。我使用的是在这个论坛中列出的公式:如何获得两点之间的夹角
I'm currently developing a simple 2D game for Android. I have a stationary object that's situated in the center of the screen and I'm trying to get that object to rotate and point to the area on the screen that the user touches. I have the constant coordinates that represent the center of the screen and I can get the coordinates of the point that the user taps on. I'm using the formula outlined in this forum: How to get angle between two points?
-
它说如下:如果你想在这两点定义的线与水平轴之间的夹角:
It says as follows "If you want the the angle between the line defined by these two points and the horizontal axis:
double angle = atan2(y2 - y1, x2 - x1) * 180 / PI;".
我实现了这一点,但我觉得其实我在屏幕上正在工作的坐标是造成误判,因为Y坐标是相反的。我不知道这是正确的方式去了解它,任何其他的想法或建议是AP preciated。
I implemented this, but I think the fact the I'm working in screen coordinates is causing a miscalculation, since the Y-coordinate is reversed. I'm not sure if this is the right way to go about it, any other thoughts or suggestions are appreciated.
推荐答案
假设: X
是横轴,并增加从左至右移动时。
是
为纵坐标,并从底部上升到顶部。 (touch_x,touch_y)
是
点由用户选择。 (center_x,center_y)
是点的中央
屏幕。 THETA
是逆时针从 + X
轴测量。然后:
Assumptions: x
is the horizontal axis, and increases when moving from left to right.
y
is the vertical axis, and increases from bottom to top. (touch_x, touch_y)
is the
point selected by the user. (center_x, center_y)
is the point at the center of the
screen. theta
is measured counter-clockwise from the +x
axis. Then:
delta_x = touch_x - center_x
delta_y = touch_y - center_y
theta_radians = atan2(delta_y, delta_x)
修改:您在发表评论时提及使y增大从上到下。在那里面 情况下,
Edit: you mentioned in a comment that y increases from top to bottom. In that case,
delta_y = center_y - touch_y
不过,这将是更正确的描述这是前pressing (touch_x,touch_y)
相对于极坐标(center_x,center_y)
。正如ChrisF提到,
采取两点之间的夹角的想法并不明确。
But it would be more correct to describe this as expressing (touch_x, touch_y)
in polar coordinates relative to (center_x, center_y)
. As ChrisF mentioned,
the idea of taking an "angle between two points" is not well defined.
这篇关于计算两点定义的直线之间的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!