当所有侧面,角度和前两个点都已知时,使用Javascript函数查找三角形的第三个点 [英] Javascript function to find third point of triangle when all sides, angles and first two points are known

查看:46
本文介绍了当所有侧面,角度和前两个点都已知时,使用Javascript函数查找三角形的第三个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾经有人问过这个问题,但是我没有找到满意的答案,所以我将尝试确保这是主题,并产生一些良好的回答.

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.

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

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.

每个点A,B和C都有x和y坐标,我们知道Ax,Ay,Cx和Cy的坐标是什么.我们也知道a,b和c的长度以及A,B和C的角度.

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.

我想编写一个Javascript函数来计算点B的x和y坐标,但是我真的很努力地将已读的数学转换为Javascript.

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

有一个已关闭的问题,但是接受的答案似乎只是返回一个值P3.但是第三点需要x和y值,所以我不明白.

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.

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

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.

这里有一个javascript解决方案,但是它没有考虑x和前两个点的y坐标.

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.

推荐答案

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

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;

这篇关于当所有侧面,角度和前两个点都已知时,使用Javascript函数查找三角形的第三个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆