JavaScript三点定圆
Posted 笠航
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript三点定圆相关的知识,希望对你有一定的参考价值。
三点定圆也就是三角形的中垂线交点,
//平面三点定位算法 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三点定圆的主要内容,如果未能解决你的问题,请参考以下文章