第12周项目4-圆与点的关系

来源:互联网 发布:成都正规java培训机构 编辑:程序博客网 时间:2024/05/01 02:54
#include <iostream>#include <cmath>using namespace std;class Point{public:Point() :x(0), y(0){};Point(int xx, int yy) :x(xx), y(yy){};friend ostream &operator<<(ostream &out, const Point &a);int x;int y;};ostream &operator<<(ostream &out, const Point &a){out << "Point=[" << a.x << ", " << a.y << "]" << endl;return out;}class Circle:public Point{public:Circle(int xx, int yy, int rr) :Point(xx,yy), r(rr){};friend ostream &operator<<(ostream &out, Circle &a);friend int locate(Point &p, Circle &c);int r;};int locate(Point &p, Circle &c){double l,m;l = sqrt((p.x - c.x)*(p.x - c.x) + (p.y - c.y)*(p.y - c.y));m=l - c.r;if(m>0)        return 11;    if(m<0)        return -11;    if(m==0)        return 0;}ostream &operator<<(ostream &out, Circle &a){out << "Center=[" << a.x << ", " << a.y << "], r=" << a.r << endl;return out;}int main(){Circle c1(3, 2, 4), c2(4, 5, 5);      //c2应该大于c1Point 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 0
原创粉丝点击