C++之我是二代我继承——圆与线

#include <iostream>
#include <cmath>
using namespace std;
class Circle;
class Point
{
public:
    Point(double a,double b)
    {
        x=a;
        y=b;
    }
    double get_distance(Point &p);
    friend ostream &operator<<(ostream &out,const Point &p);
    friend void intersection(Point &, Circle &,Point &,Point &);
protected:
    double x;
    double y;
};
double Point::get_distance(Point &p)
{
    return sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
}
class Circle:public Point
{
public:
    Circle(double a,double b,double r):Point(a,b),r(r) {}
    friend int locate(Point p,Circle c);
    friend ostream &operator<<(ostream &out,const Circle &c);
    bool operator>(Circle &c);
    bool operator<(Circle &c);
    bool operator==(Circle &c);
    bool operator>=(Circle &c);
    bool operator<=(Circle &c);
    bool operator!=(Circle &c);
    friend void intersection(Point &, Circle & ,Point &,Point & );
protected:
    double r;
    double area=3.141*r*r;
};
int locate(Point p,Circle c)
{
    Point cc(c.x,c.y);
    double d=p.get_distance(cc);
    if (d>c.r)
        return 1;
    else if (d<c.r)
        return -1;
    else
        return 0;
}
ostream & operator<<(ostream &out,const Point &p)
{
    out<<"("<<p.x<<","<<p.y<<")"<<endl;
    return out;
}
ostream & operator<<(ostream &out,const Circle &c)
{
    out<<"圆心为:("<<c.x<<","<<c.y<<"),半径为:"<<c.r<<endl;
    return out;
}
bool Circle::operator>(Circle &c)
{
    if (area>c.area)
        return true;
    return false;
}
bool Circle::operator<(Circle &c)
{
    if (area<c.area)
        return true;
    return false;
}
bool Circle::operator==(Circle &c)
{
    if (area==c.area)
        return true;
    return false;
}
bool Circle::operator>=(Circle &c)
{
    if (area>=c.area)
        return true;
    return false;
}
bool Circle::operator<=(Circle &c)
{
    if (area<=c.area)
        return true;
    return false;
}
bool Circle::operator!=(Circle &c)
{
    if (area!=c.area)
        return true;
    return false;
}
void intersection(Point &p, Circle &c ,Point &p1,Point &p2 )
{
    p1.x = (c.x + sqrt(c.r*c.r/(1+((c.y-p.y)/(c.x-p.x))*((c.y-p.y)/(c.x-p.x)))));
    p2.x = (c.x - sqrt(c.r*c.r/(1+((c.y-p.y)/(c.x-p.x))*((c.y-p.y)/(c.x-p.x)))));
    p1.y = (p.y + (p1.x -p.x)*(c.y-p.y)/(c.x-p.x));
    p2.y = (p.y + (p2.x -p.x)*(c.y-p.y)/(c.x-p.x));
    cout<<p1<<p2;
}
int main( )
{
    Circle c1(3,2,4),c2(4,5,5);      //c2应该大于c1
    Point p1(1,1),p2(3,-2),p3(7,3),pp1(0,0),pp2(0,0);  //分别位于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<<"圆c1 (";
    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<<") 圆c2"<<endl;
    cout<<endl<<"圆c1与点p3的连线在圆上的交点:"<<endl;
    intersection(p3,c1,pp1,pp2);
    return 0;
}


有一个东西叫做class Circle。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值