Javascript 已知所有边、角和前两点的情况下求三角形第三点的函数
Javascript function to find third point of triangle when all sides, angles and first two points are known
之前有人问过这个问题,但我没有找到令人满意的答案,所以我将尝试确保它符合主题并产生一些好的回应。
这个问题不应该在 maths.stackexchange 上,因为它是关于完成我下面开始的功能所需的 Javascript 代码。
想象一下下面的三角形。
每个点 A、B 和 C 都有 x 和 y 坐标,我们知道这些坐标是 Ax、Ay、Cx 和 Cy 的坐标。
我们也知道a,b,c的长度和A,B,C的角度
我想编写一个 Javascript 函数来计算 B 点的 x 和 y 坐标,但我真的很难将我读过的数学转换为 Javascript。
这是我要编写的函数的开头:
/**
* Find the coordinates for the third point of a triangle.
*
* @param Ax - x coordinate value of first known point
* @param Ay - y coordinate value of first known point
* @param Cx - x coordinate value of second known point
* @param Cy - y coordinate value of second known point
* @param a - the length of side a
* @param b - the length of side b
* @param c - the length of side c
* @param A - the angle of corner A in degrees
* @param B - the angle of corner B in degrees
* @param C - the angle of corner C in degrees
* @returns {{Bx: *, By: *}}
*/
function calculate_third_point(Ax, Ay, Cx, Cy, a, b, c, A, B, C) {
var Bx;
var By;
// What code goes here?
return {Bx: Bx, By: By};
}
有一个 closed question on Whosebug here,但接受的答案似乎只有 return 一个值,P3。但是我们需要第三个点的 x 和 y 值,所以我不明白。
有 a question on maths.stackexchange 但接受的答案似乎使用了不知从何而来的 P 和 Q,而且数学符号使事情更难理解。没有明确定义的输入和输出。
有a javascript solution here但没有考虑前两点的x和y坐标
谁能帮助我完成我的功能。解决方案必须只使用提供的输入。如果不需要任何输入,则可以将其丢弃。
使用矢量旋转的众多变体之一(不要忘记弧度和度数)
Arad = A * Math.pi/180; //degrees to radians
//unit vector
uACx = (Cx - Ax) / b;
uACy = (Cy - Ay) / b;
//rotated vector
uABx = uACx * Math.cos(Arad) - uACy * Math.sin(Arad);
uABy = uACx * Math.sin(Arad) + uACy * Math.cos(Arad);
//B position uses length of edge
Bx = Ax + c * uABx;
By = Ay + c * uABy;
//vector rotated into another direction
uABx = uACx * Math.cos(Arad) + uACy * Math.sin(Arad);
uABy = - uACx * Math.sin(Arad) + uACy * Math.cos(Arad);
//second possible position
Bx = Ax + c * uABx;
By = Ay + c * uABy;
感谢 MBo 提供了这个答案的细节。我刚刚将他的代码放入函数中并处理了度数/弧度问题,这是最终函数:
/**
* Find the coordinates for the third point of a triangle.
*
* @param Ax - x coordinate value of first known point
* @param Ay - y coordinate value of first known point
* @param Cx - x coordinate value of second known point
* @param Cy - y coordinate value of second known point
* @param b - the length of side b
* @param c - the length of side c
* @param A - the angle of corner A
* @param alt - set to true to return the alternative solution.
* @returns {{Bx: *, By: *}}
*/
function calculate_third_point(Ax, Ay, Cx, Cy, b, c, A, alt) {
var Bx;
var By;
alt = typeof alt === 'undefined' ? false : alt;
//unit vector
uACx = (Cx - Ax) / b;
uACy = (Cy - Ay) / b;
if(alt) {
//rotated vector
uABx = uACx * Math.cos(toRadians(A)) - uACy * Math.sin(toRadians(A));
uABy = uACx * Math.sin(toRadians(A)) + uACy * Math.cos(toRadians(A));
//B position uses length of edge
Bx = Ax + c * uABx;
By = Ay + c * uABy;
}
else {
//vector rotated into another direction
uABx = uACx * Math.cos(toRadians(A)) + uACy * Math.sin(toRadians(A));
uABy = - uACx * Math.sin(toRadians(A)) + uACy * Math.cos(toRadians(A));
//second possible position
Bx = Ax + c * uABx;
By = Ay + c * uABy;
}
return {Bx: Bx, By: By};
}
/**
* Convert degrees to radians.
*
* @param angle
* @returns {number}
*/
function toRadians (angle) {
return angle * (Math.PI / 180);
}
希望其他人会发现有用的东西!
之前有人问过这个问题,但我没有找到令人满意的答案,所以我将尝试确保它符合主题并产生一些好的回应。
这个问题不应该在 maths.stackexchange 上,因为它是关于完成我下面开始的功能所需的 Javascript 代码。
想象一下下面的三角形。
每个点 A、B 和 C 都有 x 和 y 坐标,我们知道这些坐标是 Ax、Ay、Cx 和 Cy 的坐标。 我们也知道a,b,c的长度和A,B,C的角度
我想编写一个 Javascript 函数来计算 B 点的 x 和 y 坐标,但我真的很难将我读过的数学转换为 Javascript。
这是我要编写的函数的开头:
/**
* Find the coordinates for the third point of a triangle.
*
* @param Ax - x coordinate value of first known point
* @param Ay - y coordinate value of first known point
* @param Cx - x coordinate value of second known point
* @param Cy - y coordinate value of second known point
* @param a - the length of side a
* @param b - the length of side b
* @param c - the length of side c
* @param A - the angle of corner A in degrees
* @param B - the angle of corner B in degrees
* @param C - the angle of corner C in degrees
* @returns {{Bx: *, By: *}}
*/
function calculate_third_point(Ax, Ay, Cx, Cy, a, b, c, A, B, C) {
var Bx;
var By;
// What code goes here?
return {Bx: Bx, By: By};
}
有一个 closed question on Whosebug here,但接受的答案似乎只有 return 一个值,P3。但是我们需要第三个点的 x 和 y 值,所以我不明白。
有 a question on maths.stackexchange 但接受的答案似乎使用了不知从何而来的 P 和 Q,而且数学符号使事情更难理解。没有明确定义的输入和输出。
有a javascript solution here但没有考虑前两点的x和y坐标
谁能帮助我完成我的功能。解决方案必须只使用提供的输入。如果不需要任何输入,则可以将其丢弃。
使用矢量旋转的众多变体之一(不要忘记弧度和度数)
Arad = A * Math.pi/180; //degrees to radians
//unit vector
uACx = (Cx - Ax) / b;
uACy = (Cy - Ay) / b;
//rotated vector
uABx = uACx * Math.cos(Arad) - uACy * Math.sin(Arad);
uABy = uACx * Math.sin(Arad) + uACy * Math.cos(Arad);
//B position uses length of edge
Bx = Ax + c * uABx;
By = Ay + c * uABy;
//vector rotated into another direction
uABx = uACx * Math.cos(Arad) + uACy * Math.sin(Arad);
uABy = - uACx * Math.sin(Arad) + uACy * Math.cos(Arad);
//second possible position
Bx = Ax + c * uABx;
By = Ay + c * uABy;
感谢 MBo 提供了这个答案的细节。我刚刚将他的代码放入函数中并处理了度数/弧度问题,这是最终函数:
/**
* Find the coordinates for the third point of a triangle.
*
* @param Ax - x coordinate value of first known point
* @param Ay - y coordinate value of first known point
* @param Cx - x coordinate value of second known point
* @param Cy - y coordinate value of second known point
* @param b - the length of side b
* @param c - the length of side c
* @param A - the angle of corner A
* @param alt - set to true to return the alternative solution.
* @returns {{Bx: *, By: *}}
*/
function calculate_third_point(Ax, Ay, Cx, Cy, b, c, A, alt) {
var Bx;
var By;
alt = typeof alt === 'undefined' ? false : alt;
//unit vector
uACx = (Cx - Ax) / b;
uACy = (Cy - Ay) / b;
if(alt) {
//rotated vector
uABx = uACx * Math.cos(toRadians(A)) - uACy * Math.sin(toRadians(A));
uABy = uACx * Math.sin(toRadians(A)) + uACy * Math.cos(toRadians(A));
//B position uses length of edge
Bx = Ax + c * uABx;
By = Ay + c * uABy;
}
else {
//vector rotated into another direction
uABx = uACx * Math.cos(toRadians(A)) + uACy * Math.sin(toRadians(A));
uABy = - uACx * Math.sin(toRadians(A)) + uACy * Math.cos(toRadians(A));
//second possible position
Bx = Ax + c * uABx;
By = Ay + c * uABy;
}
return {Bx: Bx, By: By};
}
/**
* Convert degrees to radians.
*
* @param angle
* @returns {number}
*/
function toRadians (angle) {
return angle * (Math.PI / 180);
}
希望其他人会发现有用的东西!