Skip to content

Instantly share code, notes, and snippets.

@ktcy
Created April 20, 2017 09:03
Show Gist options
  • Save ktcy/0f81fc99fb81b05b527a4b5639e4e77a to your computer and use it in GitHub Desktop.
Save ktcy/0f81fc99fb81b05b527a4b5639e4e77a to your computer and use it in GitHub Desktop.
Circumcircle from three points
function circumcircle(point0, point1, point2) {
const x1 = point1.x - point0.x;
const y1 = point1.y - point0.y;
const x2 = point2.x - point0.x;
const y2 = point2.y - point0.y;
const k = 2 * (x1 * y2 - y1 * x2);
if (k === 0) {
return null;
}
const a = (point1.x + point0.x) * x1 + (point1.y + point0.y) * y1;
const b = (point2.x + point0.x) * x2 + (point2.y + point0.y) * y2;
const x = (a * y2 - b * y1) / k;
const y = (b * x1 - a * x2) / k;
const radius = Math.sqrt((point0.x - x) * (point0.x - x) + (point0.y - y) * (point0.y - y));
return {x, y, radius};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment