欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UVA 190 - Circle Through Three Points

程序员文章站 2022-04-01 12:42:40
...

题意:给出三点坐标,求过三点圆的标准方程和一般方程
解法:

已知两点(x1,y1)(x2,y2)求直线中垂线方程ax+by+c=0结论:
a= x2-x1,b=y2-y1,c=(x1*x1-x2*x2+y1*y1-y2*y2)/2
已知两直线方程a1x+b1y+c1=0,a2x+b2y+c2=0求交点结论:
(  (b1*c2-b2*c1)/ (a1*b2-b1*a2)  ,  (c1*a2-c2*a1)/(a1*b2-b1*a2)  )

两条直线中垂线交点坐标即为圆心坐标
代码如下:(这题我就不该用cout,太恶心了=-=)

#include <bits/stdc++.h>

using namespace std;
const double M=1e-10;
double a1,b1,c1,a2,b2,c2,x,y;
void A(double x1,double y1,double x2,double y2,double x3,double y3)
{
    a1=x2-x1;
    b1=y2-y1;
    c1=(x1*x1-x2*x2+y1*y1-y2*y2)/2;
    a2=x3-x1;
    b2=y3-y1;
    c2=(x1*x1-x3*x3+y1*y1-y3*y3)/2;
    x=(b1*c2-b2*c1)/(a1*b2-a2*b1);
    y=(c1*a2-c2*a1)/(a1*b2-a2*b1);
}
int main()
{
    double x1,y1,x2,y2,x3,y3,r;
    while(cin>>x1>>y1>>x2>>y2>>x3>>y3)
    {
        A(x1,y1,x2,y2,x3,y3);
        r=sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y));
        if(x>M)
        {
            if(y>M)
                {cout<<"(x - "<<fixed<<setprecision(3)<<x<<")^2 + (y - "<<fixed<<setprecision(3)<<y<<")^2 = "<<fixed<<setprecision(3)<<r<<"^2"<<endl;
                if((x*x+y*y-r*r)>M)
                    cout<<"x^2 + y^2 - "<<fixed<<setprecision(3)<<2*x<<"x - "<<fixed<<setprecision(3)<<2*y<<"y + "<<fixed<<setprecision(3)<<(x*x+y*y-r*r)<<" = 0"<<endl;
                else
                    cout<<"x^2 + y^2 - "<<fixed<<setprecision(3)<<2*x<<"x - "<<fixed<<setprecision(3)<<2*y<<"y - "<<fixed<<setprecision(3)<<abs(x*x+y*y-r*r)<<" = 0"<<endl;}
            else
                {cout<<"(x - "<<fixed<<setprecision(3)<<x<<")^2 + (y + "<<fixed<<setprecision(3)<<abs(y)<<")^2 = "<<fixed<<setprecision(3)<<r<<"^2"<<endl;
                if((x*x+y*y-r*r)>M)
                    cout<<"x^2 + y^2 - "<<fixed<<setprecision(3)<<2*x<<"x + "<<fixed<<setprecision(3)<<abs(2*y)<<"y + "<<fixed<<setprecision(3)<<(x*x+y*y-r*r)<<" = 0"<<endl;
                else
                    cout<<"x^2 + y^2 - "<<fixed<<setprecision(3)<<2*x<<"x + "<<fixed<<setprecision(3)<<abs(2*y)<<"y - "<<fixed<<setprecision(3)<<abs(x*x+y*y-r*r)<<" = 0"<<endl;}
        }
        else
        {
            if(y>M)
                {cout<<"(x + "<<fixed<<setprecision(3)<<abs(x)<<")^2 + (y - "<<fixed<<setprecision(3)<<y<<")^2 = "<<fixed<<setprecision(3)<<r<<"^2"<<endl;
                if((x*x+y*y-r*r)>M)
                    cout<<"x^2 + y^2 + "<<fixed<<setprecision(3)<<abs(2*x)<<"x - "<<fixed<<setprecision(3)<<2*y<<"y + "<<fixed<<setprecision(3)<<(x*x+y*y-r*r)<<" = 0"<<endl;
                else
                    cout<<"x^2 + y^2 + "<<fixed<<setprecision(3)<<abs(2*x)<<"x - "<<fixed<<setprecision(3)<<2*y<<"y - "<<fixed<<setprecision(3)<<abs(x*x+y*y-r*r)<<" = 0"<<endl;}
            else
                {cout<<"(x + "<<fixed<<setprecision(3)<<abs(x)<<")^2 + (y + "<<fixed<<setprecision(3)<<abs(y)<<")^2 = "<<fixed<<setprecision(3)<<r<<"^2"<<endl;
                if((x*x+y*y-r*r)>M)
                    cout<<"x^2 + y^2 + "<<fixed<<setprecision(3)<<abs(2*x)<<"x + "<<fixed<<setprecision(3)<<abs(2*y)<<"y + "<<fixed<<setprecision(3)<<(x*x+y*y-r*r)<<" = 0"<<endl;
                else
                    cout<<"x^2 + y^2 + "<<fixed<<setprecision(3)<<abs(2*x)<<"x + "<<fixed<<setprecision(3)<<abs(2*y)<<"y - "<<fixed<<setprecision(3)<<abs(x*x+y*y-r*r)<<" = 0"<<endl;}
        }
        cout<<endl;
    }
    return 0;
}