I'm writing a G-Code interpreter and am having difficulties determining the center of a circle when given (X, Y) for two Points on the circle and the radius. I can plot a circle from 2 Points when given the center coint, but if a radius value is given instead, I can't use that to the a center point.
I've looked up multiple examples that are written in different forms of math (calculus, geometry, trig, etc.) but can't translate any of them to code. My understanding is that the values given generate two different center/intersecting points. Those are what I need to figure out.
The interpreter is running on an Arduino and written in C. If someone could just walk me through it in pseudo code even, I would be very grateful.
Thanks!
Given the equation of a circle and the equations of the midpoints:
q = sqrt((x2-x1)^2 + (y2-y1)^2)
y3 = (y1+y2)/2
x3 = (x1+x2)/2
One answer will be:
x = x3 + sqrt(r^2-(q/2)^2)*(y1-y2)/q
y = y3 + sqrt(r^2-(q/2)^2)*(x2-x1)/q
The other will be:
x = x3 - sqrt(r^2-(q/2)^2)*(y1-y2)/q
y = y3 - sqrt(r^2-(q/2)^2)*(x2-x1)/q
Assuming the variables for the points have been declared already, your code should look like this:
double q = Math.Sqrt(Math.Pow((x2-x1),2) + Math.Pow((y2-y1),2));
double y3 = (y1+y2)/2;
double x3 = (x1+x2)/2;
double basex = Math.Sqrt(Math.Pow(r,2)-Math.Pow((q/2),2))*(y1-y2)/q; //calculate once
double basey = Math.Sqrt(Math.Pow(r,2)-Math.Pow((q/2),2))*(x2-x1)/q; //calculate once
double centerx1 = x3 + basex; //center x of circle 1
double centery1 = y3 + basey; //center y of circle 1
double centerx2 = x3 - basex; //center x of circle 2
double centery2 = y3 - basey; //center y of circle 2