根据三角形获取屏幕坐标
如果我有斜边的长度和角度,我如何找到邻边和对边?
我正在用 JavaScript 编写此内容,我只是想找到给定长度和角度的斜边结束处的屏幕坐标 (x/y)。 因此,另外,我需要知道如何从笛卡尔坐标转换为屏幕坐标。
抱歉,这可能是一个非常愚蠢的问题。
如果我不清楚的话,这是一个草图:
所以像这样:
function getLineEndCoords(originX, originY, hypotenuse, angle){
// Probably something to do with tangents and cosines.
return [x,y]
}
另外,如果有人知道任何好的 JS此类内容(等轴测图、显示相关数学)的库会很方便。
If I have the length of the hypotenuse and its angle, how do I find the adjacent and opposite?
I'm writing this in JavaScript and I'm just trying to find the screen coordinates (x/y) where the hypotenuse ends given a length and angle. So, additionally, I need to know how to convert from a cartesian coordinates to screen coordinates.
Sorry, this is probably a pretty dumb question.
Here is a sketch if I'm being unclear:
So something like this:
function getLineEndCoords(originX, originY, hypotenuse, angle){
// Probably something to do with tangents and cosines.
return [x,y]
}
Additionally, if anyone knows any good JS libraries for this sort of stuff (isometric drawings, display related math) that would be convenient.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 soh cah toa:
因此:
通常更容易记住,一个轴是角度的正弦乘以长度,另一个轴是角度的余弦乘以长度。
编辑:
抱歉,我忘记添加原点。
Using soh cah toa:
therefore:
It is usually easier to memorise that one axis is the sine of the angle multiplied by the length, and the other is the cosine of the angle times the length.
EDIT:
Sorry, I forgot to include adding the origin.
给你:
上面的代码有相对于右 x 轴的角度(如草图中所示)。例如,角度 0 将是时钟上的数字 3。以弧度表示的角度 PI/2(90 度表示)将为 12 点钟方向。该函数还将坐标返回为屏幕坐标,其中 Y 随着向下而增加,而不是像在笛卡尔平面上那样向上。
请注意,javascript 三角函数仅接受弧度,而不接受度数。您的角度参数必须以弧度为单位。以下是将度数转换为弧度的代码:
Here you go:
The code above has angle relative to the right x axis (as was in your sketch). For example, angle 0 would be number 3 on a clock. Angle PI/2 in radians (90 in degrees) would be 12 o'clock. The function also returns the coordinates as screen coordinates, where Y increases as you go down, not up as on a Cartesian plane.
Please note that the javascript trigonometry functions only accept radians, not degrees. Your angle argument must be in radians. Here is the code to convert from degrees to radians: