/*
*Copyright (c) 2014, 烟台大学计算机学院
*All rights reserved.
*文件名称:week12-4.cpp
*作者:高赞
*完成日期:2015年 5 月 23 日
*版本号:v1.0
*
*
*/
#include <iostream>
#include <cmath>
using namespace std;
class Point //定义坐标点类
{
public:
Point(double x0=0, double y0=0):x(x0), y(y0) {}
double getx()
{
return x;
}
double gety()
{
return y;
}
friend ostream &operator<<(ostream &,Point &);
protected:
double x,y; //点的横坐标和纵坐标
};
class Circle:public Point
{
public:
Circle(double x0=0,double y0=0,double r0=0):Point(x0,y0),r(r0) {}
double getr()
{
return r;
}
double area()
{
return 3.1415926*r*r;
}
bool operator > (const Circle &c)
{
if(r>c.r)
return true;
else return false;
}
bool operator < (const Circle &c)
{
if(r<c.r)
return true;
else return false;
}
bool operator >= (const Circle &c)
{
if(r>=c.r)
return true;
else return false;
}
bool operator <= (const Circle &c)
{
if(r<=c.r)
return true;
else return false;
}
bool operator == (const Circle &c)
{
if(r==c.r)
return true;
else return false;
}
bool operator != (const Circle &c)
{
if(r!=c.r)
return true;
else return false;
}
friend double locate(Point &,Circle &);
friend ostream &operator<<(ostream &,Circle &);
protected:
double r;
};
double locate(Point &p,Circle &c )
{
double m,n;
n=(p.getx()-c.x)*(p.getx()-c.x)+(p.gety()-c.y)*(p.gety()-c.y);
m=sqrt(n);
return m-c.r;
}
ostream &operator<<(ostream &put,Point &p)
{
put<<"("<<p.x<<","<<p.y<<")"<<endl;
return put;
}
ostream &operator<<(ostream &put,Circle &c)
{
put<<"圆心:("<<c.x<<","<<c.y<<")"<<endl<<"半径:"<<c.r<<endl;
return put;
}
void intersectPoint(Point &p,Circle &c)
{
double x1,y1,x2,y2,m,n;
m=(c.gety()-p.gety())/(c.getx()-p.getx());
n=sqrt(c.gety()*c.gety()/(1+m*m));
x1=c.getx()+n;
y1=m*(x1-p.getx())+p.gety();
x2=c.getx()-n;
y2=m*(x2-p.getx())+p.gety();
cout<<"("<<x1<<","<<y1<<"),("<<x2<<","<<y2<<")";
}
int main( )
{
Circle c1(3,2,4),c2(4,5,5); //c2应该大于c1
Point p1(1,1),p2(3,-2),p3(7,3); //分别位于c1内、上、外
cout<<"圆c1: "<<c1;
cout<<"点p1: "<<p1;
cout<<"点p1在圆c1之"<<((locate(p1, c1)>0)?"外":((locate(p1, c1)<0)?"内":"上"))<<endl;
cout<<"点p2: "<<p2;
cout<<"点p2在圆c1之"<<((locate(p2, c1)>0)?"外":((locate(p2, c1)<0)?"内":"上"))<<endl;
cout<<"点p3: "<<p3;
cout<<"点p3在圆c1之"<<((locate(p3, c1)>0)?"外":((locate(p3, c1)<0)?"内":"上"))<<endl;
if (c1>c2) cout<<"c1 > c2"<<endl;
if (c1<c2) cout<<"c1 < c2"<<endl;
if (c1==c2) cout<<"c1 == c2"<<endl;
if (c1!=c2) cout<<"c1 != c2"<<endl;
if (c1>=c2) cout<<"c1 >= c2"<<endl;
if (c1<=c2) cout<<"c1 <= c2"<<endl;
cout<<"点p1与c2圆心相连成的直线和圆的两个交点为:";
intersectPoint(p1,c2);
return 0;
}