当所有边,角度和前两个点已知时,使用Javascript函数查找三角形的第三个点

时间:2018-11-13 09:01:01

标签: javascript math trigonometry

之前曾有人问过这个问题,但是我没有找到满意的答案,因此我将尝试确保这是主题,并产生一些良好的回答。

这个问题不应该放在maths.stackexchange上,因为它与完成我在下面开始的功能所需的Javascript代码有关。

想象一下下面的三角形。

Labeled triangle

每个点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 stackoverflow here,但是可接受的答案似乎只是返回一个值P3。但是第三点需要x和y值,所以我不明白。

a question on maths.stackexchange,但被接受的答案似乎是使用P和Q,它们从无处出现,并且数学符号使事情变得更难以理解。没有明确定义的输入和输出。

a javascript solution here,但未考虑前两个点的x和y坐标。

有人可以通过完成我的功能来帮助我。解决方案必须仅使用提供的输入。如果不需要任何输入,则可以将其丢弃。

2 个答案:

答案 0 :(得分:1)

使用矢量旋转的众多变体之一(不要忘记弧度和度)

 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;

答案 1 :(得分:0)

感谢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);
}

希望其他人会发现它有用!