fsm-designer/js/math.js

17 lines
778 B
JavaScript

function det(a, b, c, d, e, f, g, h, i) {
return a * e * i + b * f * g + c * d * h - a * f * h - b * d * i - c * e * g;
}
function circleFromPoints(p1, p2, p3) {
const a = det(p1.x, p1.y, 1, p2.x, p2.y, 1, p3.x, p3.y, 1);
const bx = -det(p1.x * p1.x + p1.y * p1.y, p1.y, 1, p2.x * p2.x + p2.y * p2.y, p2.y, 1, p3.x * p3.x + p3.y * p3.y, p3.y, 1);
const by = det(p1.x * p1.x + p1.y * p1.y, p1.x, 1, p2.x * p2.x + p2.y * p2.y, p2.x, 1, p3.x * p3.x + p3.y * p3.y, p3.x, 1);
const c = -det(p1.x * p1.x + p1.y * p1.y, p1.x, p1.y, p2.x * p2.x + p2.y * p2.y, p2.x, p2.y, p3.x * p3.x + p3.y * p3.y, p3.x, p3.y);
return {
x: -bx / (2 * a),
y: -by / (2 * a),
radius: Math.sqrt(bx ** 2 + by ** 2 - 4 * a * c) / (2 * Math.abs(a)),
};
}