编写一个程序。通过cin输入三个二维坐标点,能够计算出这三个二维坐标点所确定圆的圆心,要将重要的部分(如:求中垂线)写成函数形式
#include<iostream>
#include<cmath>
using namespace std;
struct Point{
double x;
double y;
};
struct Line{
double k;
double b;
};
bool IsSameSlope(Line Line1,Line Line2);
Line GetLineOfTwoPoints(Point P1,Point P2);
bool IsSamePoint(Point P1,Point P2);
Point GetMidpoint(Point P1, Point P2);
Line GetMidperpendicular(Point P,double k);
Point GetPointOfIntersection(Line Line1,Line Line2);
double GetDistanceOfTwoPoint(Point P1,Point P2);
int main()
{
Point Point1,Point2,Point3;
cout<<"请输入三个点的坐标"<<endl;
cin>>Point1.x>>Point1.y;
cin>>Point2.x>>Point2.y;
cin>>Point3.x>>Point3.y;
bool x;
x=IsSamePoint(Point1,Point2)||IsSamePoint(Point2,Point3)
||IsSamePoint(Point1,Point3);
if(x==1)
{
cout<<"错误,输入的三个点有相同点!"<<endl;
system("pause");
return 0;
}
Line Line1_P1P2,Line2_P2P3;
Line1_P1P2=GetLineOfTwoPoints(Point1,Point2);
Line2_P2P3=GetLineOfTwoPoints(Point2,Point3);
bool y;
y=IsSameSlope(Line1_P1P2,Line2_P2P3);
if(y==1)
{
cout<<"错误,输入的三个点共线!"<<endl;
system("pause");
return 0;
}
else
{
cout<<"这三个点能够求出圆心!"<<endl;
}
Point Point4_MidP1P2,Point5_MidP2P3;
Point4_MidP1P2 = GetMidpoint(Point1,Point2);
Point5_MidP2P3 = GetMidpoint(Point2,Point3);
Line Line3_P1P2_Vertical,Line4_P2P3_Vertical;
Line3_P1P2_Vertical = GetMidperpendicular(Point4_MidP1P2,Line1_P1P2.k);
Line4_P2P3_Vertical = GetMidperpendicular(Point5_MidP2P3,Line2_P2P3.k);
Point CenterOfCircle,StartPoint,EndPoint;
CenterOfCircle=GetPointOfIntersection(Line3_P1P2_Vertical,Line4_P2P3_Vertical);
double Radius;
Radius = GetDistanceOfTwoPoint(CenterOfCircle,Point2);
cout<<"圆心坐标为"<<CenterOfCircle.x<<", "<<CenterOfCircle.y<<endl;
cout<<"半径为"<<Radius<<endl;
system("pause");
return 0;
}
bool IsSameSlope(Line Line1,Line Line2)
{
if (Line1.k==Line2.k)
{
return 1;
}
else
{
return 0;
}
}
Line GetLineOfTwoPoints(Point P1,Point P2)
{
Line StraightLine={0,0};
if(P1.x==P2.x)
{
P1.x=P2.x-0.0000001;
}
if(P1.y==P2.y)
{
P1.y=P2.y-0.0000001;
}
StraightLine.k = (P2.y - P1.y)/ (P2.x -P1.x);
StraightLine.b = P1.y - P1.x * StraightLine.k ;
return StraightLine;
}
bool IsSamePoint(Point P1,Point P2)
{
if(P1.x==P2.x&&P1.y==P2.y)
{
return 1;
}
else
{
return 0;
}
}
Point GetMidpoint(Point P1, Point P2)
{
Point Midpoint={0,0};
Midpoint.x = (P1.x + P2.x )/2;
Midpoint.y = (P1.y + P2.y )/2;
return Midpoint;
}
Line GetMidperpendicular(Point P,double k)
{
Line StraightLine={0,0};
StraightLine.k = -1/k;
StraightLine.b = P.y - StraightLine.k * P.x ;
return StraightLine;
}
Point GetPointOfIntersection(Line Line1,Line Line2)
{
Point PointOfIntersection={0,0};
PointOfIntersection.x = (Line2.b-Line1.b)/(Line1.k-Line2.k);
PointOfIntersection.y = (Line2.k*Line1.b-Line1.k*Line2.b)/(Line2.k-Line1.k);
return PointOfIntersection;
}
double GetDistanceOfTwoPoint(Point P1,Point P2)
{
double DistanceOfTwoPoint=0;
DistanceOfTwoPoint=sqrt((P1.x-P2.x)*(P1.x-P2.x)+(P1.y-P2.y)*(P1.y-P2.y));
return DistanceOfTwoPoint;
}