Search code examples
carduinogeometryg-code

Finding center of a circle given two points and radius


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!


Solution

  • This will return one center point. You will need to alter it for the other.

    In c#:

     private double CenterX(double x1,double y1, double x2, double y2,double radius)
        {
            double radsq = radius * radius;
            double q = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
            double x3 = (x1 + x2) / 2;
    
    
         return x3 + Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((y1 - y2) / q);
    
    
        }
    
        private double CenterY(double x1, double y1, double x2, double y2, double radius)
        {
        double radsq = radius * radius;
        double q = Math.Sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
    
         double y3 = (y1 + y2) / 2;
    
          return y3 + Math.Sqrt(radsq - ((q / 2) * (q / 2))) * ((x2-x1) / q);
    
    
        }