Search code examples
c#listmathvectorgestures

I have a list of points and would like to determine if they form a circle


I'm using C#, I have a list of Vector points and would like to try and approximate how close they look like a circle.

Any ideas how to implement this or if someone else has?


Solution

  • 1) Pick any three points from that list, find the center of their appropriate circle

    We can do this, using triangle circumcircle construction method, you find the medians of all three sides (two are sufficient) and their intersection is the center of the circle. Something like this:

    public PointF findCenter(PointF a, PointF b, PointF c)
    {
        float k1 = (a.Y - b.Y) / (a.X - b.X) //Two-point slope equation
        float k2 = (a.Y - c.Y) / (a.X - c.X) //Same for the (A,C) pair
        PointF midAB = new PointF((a.X + b.X) / 2, (a.Y + b.Y) / 2) //Midpoint formula
        PointF midAC = new PointF((a.X + c.X) / 2, (a.Y + c.Y) / 2) //Same for the (A,C) pair
        k1 = -1*k1; //If two lines are perpendicular, then the product of their slopes is -1.
        k2 = -1*k2; //Same for the other slope
        float n1 = midAB.Y - k1*midAB.X; //Determining the n element
        float n2 = midAC.Y - k2*midAC.Y; //Same for (A,C) pair
        //Solve y1=y2 for y1=k1*x1 + n1 and y2=k2*x2 + n2
        float x = (n2-n1) / (k1-k2);
        float y = k1*x + n1;
        return new PointF(x,y);
    }
    

    2) Check if the other points are equivalently distanced from this center, if yes, you have a circle, if no, you don't.

    P.S. I haven't tested the code, so be prepared to debug. Ask if you need anything else