Javascript function to find third point of triangle when all sides, angles and first two points are known

霸气de小男生 提交于 2021-01-27 17:46:14

问题


This has been asked before however I've found no satisfactory answers so I'm going to try and makes sure this is on topic and generates some good responses.

This question should not be on maths.stackexchange because it's about the Javascript code required to complete the function I started below.

Imagine the following triangle.

Each point A, B and C have x and y coordinates and we know what these coordinates are for Ax, Ay, Cx and Cy. We also know the length of a, b and c and the angle of A, B and C.

I want to write a Javascript function to calculate the x and y coordinates of point B but I'm really struggling to convert the maths I've read to Javascript.

Here is the start of the function I'm trying to write:

/**
 * 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};
}

There is a closed question on stackoverflow here, however the accepted answer appears to just return one value, P3. But we need an x and a y value for the third point so I don't understand it.

There is a question on maths.stackexchange but the accepted answer appears to use P and Q that just appear from nowhere and the mathematical symbols make things harder to understand. There are no clear defined inputs and outputs.

There is a javascript solution here but it doesn't take into account the x and y coordinates of the first two points.

Can someone help me by completing my function. The solution must just use the provided inputs. If any inputs are not required they may be discarded.


回答1:


One of numerous variants using rotation of vector (dont forget about radians and degrees)

 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;



回答2:


Thanks to MBo for providing the nitty gritty to this answer. I've just put his code into a function and dealt with the degrees / radians issue and this is the final function:

/**
 * 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);
}

Hopefully someone else will find the useful!



来源:https://stackoverflow.com/questions/53277245/javascript-function-to-find-third-point-of-triangle-when-all-sides-angles-and-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!