I have three points on the circumference of a circle:
pt A = (A.x, A.y);
pt B = (B.x, B.y);
pt C = (C.x, C.y);
How do I calculate the cente
I was looking for a similar algorithm when I hovered over this question. Took your code but found that this will not work in cases when where either of the slope is 0 or infinity (can be true when xDelta_a or xDelta_b is 0).
I corrected the algorithm and here is my code. Note: I used objective-c programming language and am just changing the code for point value initialization, so if that is wrong, I am sure programmer working in java can correct it. The logic, however, is the same for all (God bless algorithms!! :))
Works perfectly fine as far as my own functional testing is concerned. Please let me know if logic is wrong at any point.
pt circleCenter(pt A, pt B, pt C) {
float yDelta_a = B.y - A.y;
float xDelta_a = B.x - A.x;
float yDelta_b = C.y - B.y;
float xDelta_b = C.x - B.x;
pt center = P(0,0);
float aSlope = yDelta_a/xDelta_a;
float bSlope = yDelta_b/xDelta_b;
pt AB_Mid = P((A.x+B.x)/2, (A.y+B.y)/2);
pt BC_Mid = P((B.x+C.x)/2, (B.y+C.y)/2);
if(yDelta_a == 0) //aSlope == 0
{
center.x = AB_Mid.x;
if (xDelta_b == 0) //bSlope == INFINITY
{
center.y = BC_Mid.y;
}
else
{
center.y = BC_Mid.y + (BC_Mid.x-center.x)/bSlope;
}
}
else if (yDelta_b == 0) //bSlope == 0
{
center.x = BC_Mid.x;
if (xDelta_a == 0) //aSlope == INFINITY
{
center.y = AB_Mid.y;
}
else
{
center.y = AB_Mid.y + (AB_Mid.x-center.x)/aSlope;
}
}
else if (xDelta_a == 0) //aSlope == INFINITY
{
center.y = AB_Mid.y;
center.x = bSlope*(BC_Mid.y-center.y) + BC_Mid.x;
}
else if (xDelta_b == 0) //bSlope == INFINITY
{
center.y = BC_Mid.y;
center.x = aSlope*(AB_Mid.y-center.y) + AB_Mid.x;
}
else
{
center.x = (aSlope*bSlope*(AB_Mid.y-BC_Mid.y) - aSlope*BC_Mid.x + bSlope*AB_Mid.x)/(bSlope-aSlope);
center.y = AB_Mid.y - (center.x - AB_Mid.x)/aSlope;
}
return center;
}