获取一点的斜率和角度(以度为单位)
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,从基本三角学...
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
正如多伊尔先生讽刺地暗示的那样,数学并不难,但是:
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.
假设 H = 斜边(在您的示例中为 10),这就是斜边的公式:
现在您已经得到了
其中 X2、Y2 将根据 A 和 H 的值而变化
要检查我们的计算 - A(由用户)可以使用斜率方程来计算,用原始输入和结果输出替换 X1、X2、Y1 和 Y2 值。
Assuming H = Hypotenuse (10 in your example), this is the formula for your slope:
So now you've got
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.
也许看待问题的更好方法是使用向量:
sstatic.net/eEEmJ.gif" alt="alt 文本">
(来源:equationsheet.com)
您可以也可以这样写向量:
(来源: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:
(source: equationsheet.com)
You can also write the vector this way:
(source: equationsheet.com)
where
(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:
(source: equationsheet.com)
(source: equationsheet.com)