获取一点的斜率和角度(以度为单位)

发布于 2024-08-12 10:37:59 字数 178 浏览 7 评论 0原文

在javascript中,我试图画一条用户定义角度的线。

基本上,我有一个点 (x,y) 和一个角度来创建下一个点。线的长度需要为 10px。

假设起始点是 (180, 200)...如果我给它角度“A”并且(我猜)斜边是 10,那么我的方程将是什么来获得 X 和 Y一个斜坡?

感谢您的帮助!

In javascript, I am trying to draw a line that is at an angle that is user defined.

Basically, I have a point (x,y) and an angle to create the next point at. The length of the line need to be 10px.

Let's say that the point to start with is (180, 200)... if I give it angle "A" and the (I guess)hypotenuse is 10, what would my equation(s) be to get the X and Y for a slope?

Thanks for your help!

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

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

发布评论

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

评论(4

稚气少女 2024-08-19 10:37:59

好吧,从基本三角学...

sin A° = Y/10

cos A° = X/10

10^2 = Y^2 + X^2

well, from basic trigonometry...

sin A° = Y/10

cos A° = X/10

10^2 = Y^2 + X^2

素衣风尘叹 2024-08-19 10:37:59

正如多伊尔先生讽刺地暗示的那样,数学并不难,但是:

1)确保你清楚角度是指什么,以及坐标的方向;最简单的三角函数假设您正在处理传统的笛卡尔坐标,x 向右增加,y 沿页面向上增加,而大多数绘图 api 则 y 沿页面向下增加,x 向右增加。

2)确保您了解数学函数是否需要度数或弧度,并为它们提供适当的参数。

As Mr Doyle snarkily implied, the math isn't that hard, but :

1) Make sure you are clear about what the angle is referenced to, and what directions your coordinates go; most simple trig stuff assumes you are dealing with traditional cartesian coordinates with x increasing to the right, and y increasing up the page, whereas most drawing api have y increasing down the page and x increasing to the right.

2) make sure you understand whether the math functions need degrees or radians and supply them with the appropriate arguments.

臻嫒无言 2024-08-19 10:37:59

假设 H = 斜边(在您的示例中为 10),这就是斜边的公式:

Y2 = H(Sin(A)) + Y1
   = 10(Sin(A)) + 200

X2 = Sqrt((H^2)-(Y2^2)) + X1
   = Sqrt(100 - (Y2^2)) + 180

现在您已经得到了

(180, 200) -> (X2, Y2)

其中 X2、Y2 将根据 A 和 H 的值而变化

要检查我们的计算 - A(由用户)可以使用斜率方程来计算,用原始输入和结果输出替换 X1、X2、Y1 和 Y2 值。

A = InvTan((Y2 - Y1) / (X2 - X1))
  = InvTan((Y2 - 200) / (X2 - 180))

Assuming H = Hypotenuse (10 in your example), this is the formula for your slope:

Y2 = H(Sin(A)) + Y1
   = 10(Sin(A)) + 200

X2 = Sqrt((H^2)-(Y2^2)) + X1
   = Sqrt(100 - (Y2^2)) + 180

So now you've got

(180, 200) -> (X2, Y2)

Where X2, Y2 will vary depending on the values of A and H

To check our calculation - A (as entered by the user) can be calculated using the slope equation replacing the X1, X2, Y1 and Y2 values with the original input and resulting output.

A = InvTan((Y2 - Y1) / (X2 - X1))
  = InvTan((Y2 - 200) / (X2 - 180))
白衬杉格子梦 2024-08-19 10:37:59

也许看待问题的更好方法是使用向量:

sstatic.net/eEEmJ.gif" alt="alt 文本">
(来源:equationsheet.com

您可以也可以这样写向量:

alt 文本
(来源:equationsheet.com

其中

< a href="https://i.sstatic.net/DCeyt.gif" rel="nofollow noreferrer">替代文本

(来源: equationsheet.com 第一个等于第二个,让我们在给定起点、角度和距离的情况下求解终点:

替代文字
(来源:equationsheet.com

替代文本
(来源:equationsheet.com

Maybe a better way to look at the problem is using vectors:

alt text
(source: equationsheet.com)

You can also write the vector this way:

alt text
(source: equationsheet.com)

where

alt text
(source: equationsheet.com)

Setting the first equal to the second lets us solve for the end point given the starting point, the angle, and the distance:

alt text
(source: equationsheet.com)

alt text
(source: equationsheet.com)

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