2015年第十二周项目四:点,圆的关系

【项目4 - 点、圆的关系】
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出);
(4)定义友元函数int locate,判断点p与圆的位置关系(返回值<0圆内,==0圆上,>0 圆外);
  1. int main( )  
  2. {  
  3.     Circle c1(3,2,4),c2(4,5,5);      //c2应该大于c1  
  4.     Point p1(1,1),p2(3,-2),p3(7,3);  //分别位于c1内、上、外  
  5.   
  6.   
  7.     cout<<"圆c1: "<<c1;  
  8.    
  9.     cout<<"点p1: "<<p1;  
  10.     cout<<"点p1在圆c1之"<<((locate(p1, c1)>0)?"外":((locate(p1, c1)<0)?"内":"上"))<<endl;  
  11.    
  12.     cout<<"点p2: "<<p2;  
  13.     cout<<"点p2在圆c1之"<<((locate(p2, c1)>0)?"外":((locate(p2, c1)<0)?"内":"上"))<<endl;  
  14.    
  15.     cout<<"点p3: "<<p3;  
  16.     cout<<"点p3在圆c1之"<<((locate(p3, c1)>0)?"外":((locate(p3, c1)<0)?"内":"上"))<<endl;  
  17.     return 0;  

  1. }  
  2. #include <iostream>
    #include<Cmath>
    using namespace std;
    class Point
    {
    public:
        Point(double a=0,double b=0):x(a),y(b) {} //构造函数
        double distance(const Point &p) const;	//求距离
        friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<”
    protected:	  //受保护成员
        double x,y;
    };
     
    double Point::distance(const Point &p) const	//求距离
    {
        double dx = x-p.x;
        double dy = y-p.y;
        return sqrt(dx*dx+dy*dy);
    }
     
    ostream & operator<<(ostream &output,const Point &p)
    {
        output<<"["<<p.x<<","<<p.y<<"]"<<endl;
        return output;
    }
     
    class Circle:public Point //circle是Point类的公用派生类
    {
    public:
        Circle(double a=0,double b=0,double r=0) :Point(a,b),radius(r) { }; //构造函数
        friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<”
        friend int locate(const Point &p, const Circle &c); //判断点p在圆上、圆内或圆外,返回值:<0圆内,==0圆上,>0 圆外
    protected:
        double radius;
    };
     
    //重载运算符“<<”,使之按规定的形式输出圆的信息
    ostream &operator<<(ostream &output,const Circle &c)
    {
        output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius<<endl;
        return output;
    }
     
    //判断点p在圆内、圆c内或圆c外
    int locate(const Point &p, const Circle &c)
    {
        const Point cp(c.x,c.y); //圆心
        double d = cp.distance(p);
        if (abs(d - c.radius) < 1e-7)
            return 0;  //相等
        else if (d < c.radius)
            return -1;  //圆内
        else
            return 1;  //圆外
    }
     
    int main( )
    {
        Circle c1(3,2,4);
        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;
        return 0;
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值