Created
December 19, 2018 18:12
-
-
Save skynet/29a4b65a3561a286e640357263173e21 to your computer and use it in GitHub Desktop.
Create a circle out of three points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}; | |
} |
Author
skynet
commented
Dec 19, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment