首页 > 代码库 > 13周 项目1 点,圆的关系
13周 项目1 点,圆的关系
#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;}
感悟:还没吃饭呢!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。