JavaScript 程序来寻找三角形的外心(Program to find Circumcenter of a Triangle)

给定 2D 平面中 3 个非共线点 P、Q 和 R,以及它们各自的 x 和 y 坐标,找到三角形的外心。
注意:三角形的外心是圆的中心,由三角形的三个顶点形成。请注意,三个点可以唯一地确定一个圆。
示例: 

输入:P(6, 0) 
        Q(0, 0) 
        R(0, 8)
        
输出:三角形 PQR 的外心
         是:(3, 4)

输入:P(1, 1) 
        Q(0, 0) 
        R(2, 2)
        
输出:找到的两条垂直平分线平行。因此,给定的点不形成三角形并且共线

给定三角形的三个点,我们可以轻松找到三角形的边。现在,我们有了三角形三边的直线方程。得到这些后,我们可以通过一个简单的属性找到三角形的外心,如下所示:

三角形的外心是三角形所有边的垂直平分线的交点。

下图很好地解释了这一点:

        请注意,这里不需要找到三角形的所有三条边。找到两条边就足够了,因为我们只需使用两条垂直平分线就可以唯一地找到交点。第三条垂直平分线本身将通过如此找到的外心。
要做的事情可以分为以下几部分: 

        1、找到构成三角形边的两条线(比如 PQ 和 QR)。

        2、找到 PQ 和 QR 的垂直平分线(分别称为线 L 和 M)。

        3、找到直线 L 和 M 的交点作为给定三角形的外心。

步骤 1 
参考:
JavaScript 程序寻找通过 2 个点的线 JavaScript 程序寻找通过 2 个点的线(Program to find line passing through 2 Points)_js如何判断两条线段在同一直线上-优快云博客
Python 程序寻找通过 2 个点的线 Python 程序寻找通过 2 个点的线(Program to find line passing through 2 Points)_python两点坐标确定直线方程-优快云博客
Java 程序寻找通过 2 个点的线 Java 程序寻找通过 2 个点的线(Program to find line passing through 2 Points)_使用java 输入两点得到直线的表达式-优快云博客
C# 程序寻找通过 2 个点的线 C# 程序寻找通过 2 个点的线(Program to find line passing through 2 Points)_计算两个点 画线 c#-优快云博客
C++ 程序寻找通过 2 个点的线 C++ 程序寻找通过 2 个点的线(Program to find line passing through 2 Points)_两点式直线方程 c++-优快云博客

步骤 2 
让 PQ 表示为 ax + by = c 
垂直于此线的线表示为 -bx + ay = d,其中 d 为某个。 
但是,我们对垂直平分线感兴趣。因此,我们找到 P 和 Q 的中点,并将该值放入标准方程中,我们得到 d 的值。 
同样,我们对 QR 重复该过程。
 
d = -bx + ay
其中,x = (x p + x q )/2
且 y = (y p + y q )/2

步骤 3 
参考:
c++ 两线交点计算程序 c++ 两线交点计算程序(Program for Point of Intersection of Two Lines)-优快云博客
Java 两线交点计算程序 Java 两线交点计算程序(Program for Point of Intersection of Two Lines)-优快云博客
Python 两线交点计算程序 Python 两线交点计算程序(Program for Point of Intersection of Two Lines)-优快云博客
c# 两线交点计算程序 C# 两线交点计算程序(Program for Point of Intersection of Two Lines)_c#计算两条直线的交点,直线的类型为line-优快云博客
Javascript 两线交点计算程序 Javascript 两线交点计算程序(Program for Point of Intersection of Two Lines)-优快云博客

示例代码:

// JavaScript program to find the CIRCUMCENTER of a
// triangle
 
// This pair is used to store the X and Y
// coordinate of a point respectively
// #define pdd pair<double, double>
 
// Function to find the line given two points
function lineFromPoints(P, Q)
{
    let a = Q[1] - P[1];
    let b = P[0] - Q[0];
    let c = a*(P[0])+ b*(P[1]);
    return [a, b, c];
}
 
// Function which converts the input line to its
// perpendicular bisector. It also inputs the points
// whose mid-point lies on the bisector
function perpendicularBisectorFromLine(P, Q, a, b, c)
{
    let mid_point = [(P[0] + Q[0])/2, (P[1] + Q[1])/2];
 
    // c = -bx + ay
    c = -b*(mid_point[0]) + a*(mid_point[1]);
 
    let temp = a;
    a = -b;
    b = temp;
    return [a, b, c];
}
 
// Returns the intersection point of two lines
function lineLineIntersection(a1, b1, c1, a2, b2, c2)
{
    let determinant = a1*b2 - a2*b1;
    if (determinant == 0)
    {
        // The lines are parallel. This is simplified
        // by returning a pair of FLT_MAX
        return  [(10.0)**19, (10.0)**19];
    }
 
    else
    {
        let x = (b2*c1 - b1*c2)/determinant;
        let y = (a1*c2 - a2*c1)/determinant;
        return [x, y];
    }
}
 
function findCircumCenter(P, Q, R)
{
    // Line PQ is represented as ax + by = c
    let PQ_line = lineFromPoints(P, Q);
    let a = PQ_line[0];
    let b = PQ_line[1];
    let c = PQ_line[2];
    
    // Line QR is represented as ex + fy = g
    let QR_line = lineFromPoints(Q, R);
    let e = QR_line[0];
    let f = QR_line[1];
    let g = QR_line[2];
     
    // Converting lines PQ and QR to perpendicular
    // vbisectors. After this, L = ax + by = c
    // M = ex + fy = g
    let PQ_perpendicular = perpendicularBisectorFromLine(P, Q, a, b, c);
    a = PQ_perpendicular[0];
    b = PQ_perpendicular[1];
    c = PQ_perpendicular[2];
     
    let QR_perpendicular = perpendicularBisectorFromLine(Q, R, e, f, g);
    e = QR_perpendicular[0];
    f = QR_perpendicular[1];
    g = QR_perpendicular[2];
     
    // The point of intersection of L and M gives
    // the circumcenter
    let circumcenter = lineLineIntersection(a, b, c, e, f, g);
 
    if (circumcenter[0] == (10.0)**19 && circumcenter[1] == (10.0)**19){
        console.log("The two perpendicular bisectors found come parallel" )
        console.log("Thus, the given points do not form a triangle and are collinear");
    }
    else{
        console.log("The circumcenter of the triangle PQR is: (", circumcenter[0], ",", circumcenter[1], ")");
    }
}
 
// Driver code.
let P = [6, 0];
let Q = [0, 0];
let R = [0, 8];
findCircumCenter(P, Q, R);
 
// The code is contributed by Gautam goel (gautamgoel962) 

输出: 
 
三角形 PQR 的外心是:(3,4)

时间复杂度:O(1),因为执行常量操作

辅助空间: O(1)

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值