三角形垂点坐标js算法(三点定圆求圆心)

时间:2021-05-11 09:42:43

已知平面三点坐标A(x1, y1)、B(x2, y2)、C(x3, y3),三点定圆也就是三角形的中垂线交点,

//平面三点定位算法
function locate(x1, y1, x2, y2, x3, y3) {
    var a, b;
    a = (y2 - y1) / (x2 - x1);
    b = y1 - a * x1;

    var xMiddle = (x1 + x2) / 2;
    var yMiddle = (y1 + y2) / 2;
    var c, lastX, lastY;
    if (a != 0) {
        c = yMiddle - (-1 / a) * xMiddle;
        lastX = (Math.pow(x1, 2) + Math.pow(y1, 2) - Math.pow(x3, 2) - Math.pow(y3, 2) - 2 * c * y1 + 2 * c * y3) / (2 * ((x1 - x3) - (1 / a) * (y1 - y3)));
        lastY = (-1 / a) * lastX + c;
    } else {
        lastX = c = xMiddle;
        lastY = (Math.pow(x1, 2) + Math.pow(y1, 2) - Math.pow(x3, 2) - Math.pow(y3, 2) + 2 * lastX * (x3 - x1)) / (2 * (y1 - y3));
    }
    console.log("定位点X坐标: " + lastX);
    console.log("定位点Y坐标: " + lastY);
}
locate(116.3, 39.9, 115.8, 39.8, 117, 39.5);
locate(7,8, 7,23, 20,10);