码迷,mamicode.com
首页 > 其他好文 > 详细

13周 项目1 点,圆的关系

时间:2014-05-21 14:44:07      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   c   code   ext   

#include <iostream>
#include <cmath>
using namespace std;
class Point
{

public:
    Point(double a,double b):x(a),y(b) {}
    double getx()
    {
        return x;
    }
    double gety()
    {
        return y;
    }
    friend ostream&operator << (ostream&,Point&);
protected:
    double x,y;
};
ostream&operator << (ostream &output,Point &p)
{
    output<<"("<<p.getx()<<","<<p.gety()<<")"<<endl;
    return output;
}
class Circle:public Point
{

public:
    Circle(double a,double b,double c):Point(a,b),r(c) {}
    double getr()
    {
        return r;
    }
    friend ostream&operator << (ostream&,Circle&);
    friend double locate(Circle&,Point&);
protected:
    double r;
};
ostream&operator << (ostream &output,Circle &c)
{
    output<<"("<<c.getx()<<","<<c.gety()<<")"<<" ";
    output<<"r="<<c.getr()<<endl;
    return output;
}
double locate(Point &po,Circle &ci)
{
    double d,num;
    d=sqrt(pow((ci.getx()-po.getx()),2)+pow((ci.gety()-po.gety()),2));
    if(d>ci.getr())
        num=1;
    else if(d==ci.getr())
        num=0;
    else
        num=-1;
    return num;
}
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;
    return 0;
}

bubuko.com,布布扣

感悟:还没吃饭呢

13周 项目1 点,圆的关系,布布扣,bubuko.com

13周 项目1 点,圆的关系

标签:style   blog   class   c   code   ext   

原文地址:http://blog.csdn.net/zjx211314/article/details/26358805

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!