Created
October 3, 2021 15:16
-
-
Save the-vishal-kumar/050c39bfc6d0535b6f4925034a9bc863 to your computer and use it in GitHub Desktop.
Program to determine the center and radius of the circle made by three given 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
const findCircle = (point1, point2, point3) => { | |
const x1 = point1.x | |
const x2 = point2.x | |
const x3 = point3.x | |
const y1 = point1.y | |
const y2 = point2.y | |
const y3 = point3.y | |
const x12 = (x1 - x2); | |
const x13 = (x1 - x3); | |
const y12 = (y1 - y2); | |
const y13 = (y1 - y3); | |
const y31 = (y3 - y1); | |
const y21 = (y2 - y1); | |
const x31 = (x3 - x1); | |
const x21 = (x2 - x1); | |
const sx13 = Math.pow(x1, 2) - Math.pow(x3, 2); | |
const sy13 = Math.pow(y1, 2) - Math.pow(y3, 2); | |
const sx21 = Math.pow(x2, 2) - Math.pow(x1, 2); | |
const sy21 = Math.pow(y2, 2) - Math.pow(y1, 2); | |
const f = ((sx13) * (x12) | |
+ (sy13) * (x12) | |
+ (sx21) * (x13) | |
+ (sy21) * (x13)) | |
/ (2 * ((y31) * (x12) - (y21) * (x13))); | |
const g = ((sx13) * (y12) | |
+ (sy13) * (y12) | |
+ (sx21) * (y13) | |
+ (sy21) * (y13)) | |
/ (2 * ((x31) * (y12) - (x21) * (y13))); | |
const c = -(Math.pow(x1, 2)) - | |
Math.pow(y1, 2) - 2 * g * x1 - 2 * f * y1; | |
// eqn of circle be | |
// x^2 + y^2 + 2*g*x + 2*f*y + c = 0 | |
// where centre is (h = -g, k = -f) and radius r | |
// as r^2 = h^2 + k^2 - c | |
const h = -g; | |
const k = -f; | |
const sqr_of_r = h * h + k * k - c; | |
// r is the radius | |
const r = Math.sqrt(sqr_of_r); | |
return { | |
radius: r.toFixed(5), | |
centre: { | |
x: h, | |
y: k, | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment