Saturday, January 21, 2012

Circle from 3 point

#include <iostream>


typedef struct
{
    double x, y;
}pp;


pp A, B, C, m_AB, m_BC;


int main()
{
    double a1, a2, b1, b2, c1, c2, t_a;
    while(1)
    {
        printf("Enter three points A, B, C:\n");
        scanf("%lf %lf %lf %lf %lf %lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y);
        m_AB.x = (A.x+B.x)/2.0;
        m_AB.y = (A.y+B.y)/2.0;
        //printf("mid of AB (%lf,%lf)\n", m_AB.x, m_AB.y);
        a1 = B.y - A.y;
        b1 = A.x - B.x;
        c1 = a1*A.x+b1*A.y;
        //printf("a1 %lf b1 %lf c1 %lf\n", a1, b1, c1);
        t_a = a1;
        a1 = b1;
        b1 = -1*t_a;
        c1 = a1*m_AB.x+b1*m_AB.y;
        //printf("a1 %lf b1 %lf c1 %lf\n", a1, b1, c1);


        m_BC.x = (B.x+C.x)/2.0;
        m_BC.y = (B.y+C.y)/2.0;
        //printf("mid of BC (%lf,%lf)\n", m_BC.x, m_BC.y);
        a2 = C.y - B.y;
        b2 = B.x - C.x;
        c2 = a2*B.x+b2*B.y;
        //printf("a2 %lf b2 %lf c2 %lf\n", a2, b2, c2);
        t_a = a2;
        a2 = b2;
        b2 = -1*t_a;
        c2 = a2*m_BC.x+b2*m_BC.y;
        //printf("a2 %lf b2 %lf c2 %lf\n", a2, b2, c2);
        double det, xx, yy;
        det = a1*b2 - a2*b1;


        if(det == 0)
        {
            printf("A, B and C are co-linear\n");
        }
        else
        {
            xx = (b2*c1 - b1*c2)/det;
            yy = (a1*c2 - a2*c1)/det;


            printf("The Centre of the circle is (x, y) = (%lf, %lf)\n", xx, yy);
        }
    }
    return 0;
}

No comments:

Post a Comment