码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript三点定圆

时间:2018-11-24 00:19:19      阅读:582      评论:0      收藏:0      [点我收藏+]

标签:function   算法   定位   locate   middle   ons   lse   span   var   

三点定圆也就是三角形的中垂线交点,

//平面三点定位算法
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);

JavaScript三点定圆

标签:function   算法   定位   locate   middle   ons   lse   span   var   

原文地址:https://www.cnblogs.com/7qin/p/10010273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!