//自行定义类
//用下面的main()函数测试
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(float xx, float yy)
{
x = xx;
y = yy;
}
void show()
{
cout << "(" << x << ", " << y << ")" << endl;
}
friend ostream & operator << (ostream & output, Point & p)
{
p.show();
return output;
}
double distance(Point &p)
{
double dx = x - p.x;
double dy = y - p.y;
return sqrt(dx * dx + dy * dy);
}
double getx(){return x;}
double gety(){return y;}
void setx(double x1){x = x1;}
void sety(double y1){y = y1;}
~Point(){}
private:
double x;
double y;
};
class Circle:public Point
{
public:
Circle(float xx, float yy, float r):Point(xx, yy)
{
R = r;
}
double display()
{
Point::show();
cout << "半径为: ";
return R;
}
friend ostream & operator << (ostream & output, Circle & c)
{
output << c.display() << endl;
return output;
}
friend int locate(Point &p, Circle &c)
{
double d = c.distance(p);
if(d - c.R > 0)
return 1;
else if(d < c.R)
return -1;
else
return 0;
}
bool operator > (const Circle &);
bool operator < (const Circle &);
bool operator >= (const Circle &);
bool operator <= (const Circle &);
bool operator == (const Circle &);
bool operator != (const Circle &);
friend void crossover_point(Point &p1,Circle &c1, Point &p4,Point &p5);
~Circle(){}
private:
double R;
};
bool Circle::operator > (const Circle &c)
{
return (R > c.R);
}
bool Circle::operator < (const Circle &c)
{
return (R < c.R);
}
bool Circle::operator >= (const Circle &c)
{
return (R >= c.R);
}
bool Circle::operator <= (const Circle &c)
{
return (R <= c.R);
}
bool Circle::operator == (const Circle &c)
{
return (R == c.R);
}
bool Circle::operator != (const Circle &c)
{
return (R != c.R);
}
void crossover_point(Point &p1,Circle &c1, Point &p4,Point &p5)
{
double x1 = p1.getx() - c1.getx();
double y1 = p1.gety() - c1.gety();
p4.setx((c1.R / c1.distance(p1) * x1) + c1.getx());
p4.sety((c1.R / c1.distance(p1) * y1) + c1.gety());
p5.setx(c1.getx() * 2 - p4.getx());
p5.sety(c1.gety() * 2 - p4.gety());
}
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;
cout << endl;
cout << "圆c1: " << c1 << "的面积" << endl;
if(c1 > c2) cout << "大于 ";
if(c1 < c2) cout << "小于 ";
if(c1 >= c2) cout << "大于等于 ";
if(c1 <= c2) cout << "小于等于 ";
if(c1 == c2) cout << "等于 ";
if(c1 != c2) cout << "不等于 ";
cout << endl;
cout << "圆c2: " << c2 << "的面积" << endl;
cout << endl;
Point p4(1, 1) ,p5(1, 1);
crossover_point(p1,c1, p4, p5);
cout <<"点p1: " << p1;
cout << "与圆c1: " << c1;
cout << "的圆心相连,与圆交于两点,分别是:" << endl;
cout << "交点: " << p4;
cout << "交点: " << p5;
cout << endl;
system("pause");
return 0;
}
圆c1: (3, 2)
半径为: 4
点p1: (1, 1)
点p1在圆c1之内
点p2: (3, -2)
点p2在圆c1之上
点p3: (7, 3)
点p3在圆c1之外
圆c1: (3, 2)
半径为: 4
的面积
小于 小于等于 不等于
圆c2: (4, 5)
半径为: 5
的面积
点p1: (1, 1)
与圆c1: (3, 2)
半径为: 4
的圆心相连,与圆交于两点,分别是:
交点: (-0.577709, 0.211146)
交点: (6.57771, 3.78885)
请按任意键继续. . .