沿已知角度画一条已知距离的线

发布于 2025-01-03 11:48:57 字数 696 浏览 0 评论 0原文

我知道这不是一个很难的三角问题,但遗憾的是我数学迟钝。

我需要从已知起点沿已知角度到未知终点绘制一条 50 像素的线。该角度是从起点 (400,400) 和鼠标单击得出的;需要向鼠标单击方向绘制线条,但仅向单击方向绘制 50 像素。

我在谷歌上进行了广泛的搜索并找到了许多解决方案,但它对我来说并不合适。

这是我获取角度的方法:

float angle = (float) Math.toDegrees(Math.atan2(400 - event.getY(), 400 - event.getX()));
float angleInDegrees = (angle + 270) % 360;

“事件”是鼠标单击。

float endX = 250 + 50 * (float)Math.cos(angleInDegrees);
float endY 250 + 50 * (float)Math.sin(angleInDegrees);

line.setStartX(400);
line.setStartY(400);
line.setEndX(endX);
line.setEndY(endY);

我发现的所有内容都围绕 Math.cos 和 Math.sin 但我仍然不明白。我认为这个问题与将弧度映射到场景坐标有关,但我不确定。那么各位,我到底哪里蠢了?我将不胜感激任何帮助。

I know this isn't a hard trig issue, but sadly I am math retarded.

I need to draw a line of 50 pixels from a known starting point along a known angle to an unknown ending point. The angle is derived from a starting point (400,400) and a mouse click; the line needs to be drawn towards the mouse click, but only 50 pixels towards the click.

I've google'd extensively and found a number of solutions, but it's just not coming together for me.

Here is how I'm getting the angle.:

float angle = (float) Math.toDegrees(Math.atan2(400 - event.getY(), 400 - event.getX()));
float angleInDegrees = (angle + 270) % 360;

"event" is a mouse click.

float endX = 250 + 50 * (float)Math.cos(angleInDegrees);
float endY 250 + 50 * (float)Math.sin(angleInDegrees);

line.setStartX(400);
line.setStartY(400);
line.setEndX(endX);
line.setEndY(endY);

Everything I've found revolved around Math.cos and Math.sin but I'm still not getting it. I think the issue is related to mapping radians to scene coordinates, but I'm not sure. So people, in what way am I stupid? I'd appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜爱皱眉﹌ 2025-01-10 11:48:57

我不会为角度而烦恼。您只需使用比率即可做到这一点:

int startX = 400;
int startY = 400;
int dx = event.getX() - startX;
int dy = event.getY() - startY;
float distToTarget = Math.sqrt(dx * dx + dy * dy);
float ratio = 50 / distToTarget;
int endX = startX + Math.round(ratio * dx);
int endY = startY + Math.round(ratio * dy);

然后从 (startX, startY) 绘制到 (endX, endY)。

事情是这样的:

  1. 计算从 (400, 400) 到鼠标单击的向量(即 (dx, dy))
  2. 缩放向量,使其
  3. 围绕缩放后的向量有 50 像素长,因此它的 x 长度为整数,并且y
  4. 将缩放后的舍入向量添加到 (400, 400) 以计算终点

I wouldn't bother with angles. You can do this just using ratios:

int startX = 400;
int startY = 400;
int dx = event.getX() - startX;
int dy = event.getY() - startY;
float distToTarget = Math.sqrt(dx * dx + dy * dy);
float ratio = 50 / distToTarget;
int endX = startX + Math.round(ratio * dx);
int endY = startY + Math.round(ratio * dy);

Then draw from (startX, startY) to (endX, endY).

Here's what's going on:

  1. compute the vector that goes from (400, 400) to the mouse click (this is (dx, dy))
  2. scale the vector so it is 50 pixels long
  3. round the scaled vector so it has integer length in x and y
  4. add the scaled, rounded vector to (400, 400) to compute the end point
不一样的天空 2025-01-10 11:48:57

您甚至不必处理弧度/度数。回到正弦和余弦的几何定义:正弦是对边/斜边,余弦是邻边/斜边。 (“对边”和“相邻”是指直角三角形的边分别与您要计算正弦或余弦的角度相对和相邻)。

所以:

float opposite = event.getY() - 400;
float adjacent = event.getX() - 400;
float hypotenuse = Math.sqrt(opposite*opposite + adjacent*adjacent);

float cosine = adjacent/hypotenuse;
float sine = opposite/hypotenuse;

float endX = 400 + 50 * cosine;
float endY = 400 + 50 * sine;

You don't even have to deal with radians/degrees. Go back to the geometrical definition of sine and cosine: sine is opposite/hypotenuse, cosine is adjacent/hypotenuse. ("Opposite" and "adjacent" mean the legs of the right triangle respectively opposite and adjacent to the angle you're taking the sine or cosine of).

So:

float opposite = event.getY() - 400;
float adjacent = event.getX() - 400;
float hypotenuse = Math.sqrt(opposite*opposite + adjacent*adjacent);

float cosine = adjacent/hypotenuse;
float sine = opposite/hypotenuse;

float endX = 400 + 50 * cosine;
float endY = 400 + 50 * sine;
聚集的泪 2025-01-10 11:48:57

代码中的错误是您使用了度数,而 Math.cosMath.sin 需要 弧度
使用 Math.toRadians 而不是 Math.toDegrees,您的代码将开始工作。

Error in your code is that you using degrees, while Math.cos and Math.sin requires argument in radians.
Use Math.toRadians instead of Math.toDegrees and your code will start to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文