计算由两点定义的线之间的角度 [英] Calculating the angle between the line defined by two points

查看:27
本文介绍了计算由两点定义的线之间的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为 Android 开发一个简单的 2D 游戏.我有一个位于屏幕中央的静止物体,我试图让该物体旋转并指向用户触摸的屏幕区域.我有代表屏幕中心的常量坐标,我可以获得用户点击的点的坐标.我正在使用本论坛中概述的公式:如何获得两个之间的角度积分?

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 坐标是反向的.我不确定这是否是正确的方法,不胜感激任何其他想法或建议.

  • 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 为水平轴,从左向右移动时增加.y 为纵轴,自下而上递增.(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
    

    但将其描述为表达 (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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆