/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.* 文件名称:派生类的继承
* 作 者: 郭岩岩
* 完成日期:2012 年5月 9日
* 版 本 号: vc.1
* 对任务及求解方法的描述部分 :点与圆的关系
* 输入描述:
* 问题描述:
* 程序输出:
*程序头部的注释结束
*/
#include<iostream>
#include<Cmath>
using namespace std;
class Point //定义坐标点类
{public:
Point(double a=0,double b=0);
double distance(Point &p);
void setpoint(double c,double d);
double getx(){return x;}
double gety(){return y;}
void setx(double x1){x=x1;}
void sety(double y1){y=y1;}
friend ostream &operator<<(ostream &output,Point &p);
protected:
double x,y;
};
Point::Point (double a,double b)
{
x=a;
y=b;
}
void Point::setpoint(double c,double d)
{
x=c;
y=d;
}
ostream& operator<<(ostream &output,Point &p)
{
output<<"("<<p.x <<","<<p.y<<")"<<endl;
return output;
}
double Point::distance(Point &p)
{
double dx = x-p.x;
double dy = y-p.y;
return sqrt(dx*dx+dy*dy);
}
class Circle:public Point
{
public:
Circle(double a,double b,double c);
void setr(double);
double getr(){return r;}
double area();
friend int locate(Point &,Circle &);
friend ostream & operator<<(ostream & output,Circle & c);
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 intersection(Point &p,Circle &c,Point &p1,Point &c2 );
protected:
double r;
};
Circle::Circle(double a,double b,double c):Point(a,b)
{
r=c;
}
void Circle::setr(double c)
{
r=c;
}
double Circle::area()
{
return 3.1415926*getr()*getr();
}
ostream & operator<<(ostream & output,Circle &c)
{
output<<"圆心为:("<<c.x<<","<<c.y <<")"<<"半径为:"<<c.getr()<<"面积为:"<<c.area()<<endl;
return output;
}
int locate(Point &p,Circle &c)
{
Point cp(c.x,c.y);
double d = cp.distance(p);
if (abs(d - c.r) < 1e-7)
return 0;
else if (d < c.r)
return -1;
else
return 1;
}
bool Circle::operator>(const Circle &c)
{
return (r - c.r) > 1e-7;
}
bool Circle::operator<(const Circle &c)
{
return (c.r - r) > 1e-7;
}
bool Circle::operator==(const Circle &c)
{
return abs(r - c.r) < 1e-7;
}
bool Circle::operator>=(const Circle &c)
{
return !(*this < c);
}
bool Circle::operator<=(const Circle &c)
{
return !(*this > c);
}
bool Circle::operator!=(const Circle &c)
{
return abs(r - c.r) > 1e-7;
}
void intersection(Point &p,Circle &c,Point &p1,Point &p2 )
{
p1.setx (c.getx() + sqrt(c.r*c.r/(1+((c.gety()-p.gety())/(c.getx()-p.getx()))*((c.gety()-p.gety())/(c.getx()-p.getx())))));
p2.setx (c.getx() - sqrt(c.r*c.r/(1+((c.gety()-p.gety())/(c.getx()-p.getx()))*((c.gety()-p.gety())/(c.getx()-p.getx())))));
p1.sety (p.gety() + (p1.getx() -p.getx())*(c.gety()-p.gety())/(c.getx()-p.getx()));
p2.sety (p.gety() + (p2.getx() -p.getx())*(c.gety()-p.gety())/(c.getx()-p.getx()));
}
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;
if(c1>c2) cout<<"大于"<<endl;
if(c1<c2) cout<<"小于"<<endl;
if(c1>=c2) cout<<"大于等于"<<endl;
if(c1<=c2) cout<<"小于等于"<<endl;
if(c1==c2) cout<<"等于"<<endl;
if(c1!=c2) cout<<"不等于"<<endl;
cout<<"圆c2: "<<c1;
cout<<endl;
Point p4,p5;
intersection(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面积为:50.2655
点p1: (1,1)
点p1在圆c1之内
点p2: (3,-2)
点p2在圆c1之上
点p3: (7,3)
点p3在圆c1之外
圆c1: 圆心为:(3,2)半径为:4面积为:50.2655
小于
小于等于
不等于
圆c2: 圆心为:(3,2)半径为:4面积为:50.2655
点p1: (1,1)
与圆c1: 圆心为:(3,2)半径为:4面积为:50.2655
的圆心相连,与圆交于两点,分别是:
交点: (6.57771,3.78885)
交点: (-0.577709,0.211146)
请按任意键继续. . .
上机感言:
经验积累:abs表示绝对值
le-7表示很小
成员函数的名子中不能出现类名