Skip to content

Instantly share code, notes, and snippets.

@skynet
Created December 19, 2018 18:12
Show Gist options
  • Save skynet/29a4b65a3561a286e640357263173e21 to your computer and use it in GitHub Desktop.
Save skynet/29a4b65a3561a286e640357263173e21 to your computer and use it in GitHub Desktop.
Create a circle out of three points
function circleFromThreePoints(p1, p2, p3) {
var x1 = p1.x;
var y1 = p1.y;
var x2 = p2.x;
var y2 = p2.y;
var x3 = p3.x;
var y3 = p3.y;
var a = x1 * (y2 - y3) - y1 * (x2 - x3) + x2 * y3 - x3 * y2;
var b = (x1 * x1 + y1 * y1) * (y3 - y2)
+ (x2 * x2 + y2 * y2) * (y1 - y3)
+ (x3 * x3 + y3 * y3) * (y2 - y1);
var c = (x1 * x1 + y1 * y1) * (x2 - x3)
+ (x2 * x2 + y2 * y2) * (x3 - x1)
+ (x3 * x3 + y3 * y3) * (x1 - x2);
var x = -b / (2 * a);
var y = -c / (2 * a);
return {
x: x,
y: y,
r: Math.hypot(x - x1, y - y1)
};
}
@skynet
Copy link
Author

skynet commented Dec 19, 2018

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment