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

第12周 【项目4 - 点、圆的关系】(3)

时间:2015-05-21 15:41:47      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

问题描述:

(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出);
(4)定义友元函数int locate,判断点p与圆的位置关系(返回值<0圆内,==0圆上,>0 圆外);

 

(6)与圆心相连的直线:给定一点p,其与圆心相连成的直线,会和圆有两个交点,如图。在上面定义的Point(点)类和Circle(圆)类基础上,设计一种方案,输出这两点的坐标。
提示:

技术分享


代码:

 

#include <iostream>
#include <cmath>
using namespace std;
class Circle;
class Point{
protected:
    double x,y;
public:
    Point(){x=0,y=0;}
    Point(double a,double b):x(a),y(b){}
    double getx(){return x;}
    double gety(){return y;}
    double distance(const Point& c){
        double dx = x-c.x;
        double dy = y-c.y;
        return sqrt(dx*dx+dy*dy);
    }
    friend istream&operator>>(istream& in,Point & c);
    friend ostream&operator<<(ostream& out,const Point& c);
    friend void fun(Circle &c, Point &p1);
};
istream& operator>>(istream& in,Point & c){
    cout<<"请输入点坐标(x,y)格式\n";
    char a,b,e;
    do{
        cin>>a>>c.x>>b>>c.y>>e;
        if(a=='('&&b==','&&e==')')
            break;
        cout<<"格式错误请重新输入!\n";
    } while(1);
    return in;
}
ostream& operator<<(ostream& out,const Point & c){
    cout<<"("<<c.x<<","<<c.y<<")\n";
    return out;
}
class Circle:public Point{
private:
    double r;
public:
    Circle():r(0){Point();}
    Circle(double a,double b,double c):Point(a,b),r(c){}
    friend istream&operator>>(istream& in,Circle & c);
    friend ostream&operator<<(ostream& out,const Circle& c);
    friend int locate(Point&,Circle&  );
    bool operator>(const Circle&);
    bool operator<(const Circle&);
    bool operator>=(const Circle&);
    bool operator<=(const Circle&);
    bool operator==(const Circle&);
    bool operator!=(const Circle&);
    friend void fun(Circle &c, Point &p1);
    double area(){
        return 3.14159*r*r;
    }
};
void fun(Circle &c, Point &p){
    Point p1,p2;
    p1.x = (c.x + sqrt(c.r*c.r/(1+((c.y-p.gety())/(c.x-p.getx()))*((c.y-p.gety())/(c.x-p.getx())))));
    p2.x = (c.x - sqrt(c.r*c.r/(1+((c.y-p.gety())/(c.x-p.getx()))*((c.y-p.gety())/(c.x-p.getx())))));
    p1.y = (p.gety() + (p1.x -p.getx())*(c.y-p.gety())/(c.x-p.getx()));
    p2.y = (p.gety() + (p2.x -p.gety())*(c.y-p.gety())/(c.x-p.getx()));
    cout<<"点p"<<p<<"与圆"<<c<<"相交于"<<p1<<"和"<<p2<<"两点\n";
}
bool Circle::operator>(const Circle& c){
    if(r>c.r)
        return true;
    return false;
}
bool Circle::operator<(const Circle& c){
    if(r<c.r)
        return true;
    return false;
}
bool Circle::operator>=(const Circle& c){
    if(r<c.r)
        return false;
    return true;
}
bool Circle::operator<=(const Circle& c){
    if(r>c.r)
        return false;
    return true;
}
bool Circle::operator==(const Circle& c){
    if(r==c.r)
        return true;
    return false;
}
bool Circle::operator!=(const Circle& c){
    if(r==c.r)
        return false;
    return true;
}
istream&operator>>(istream& in,Circle& c){
    Point d;
    cin>>d;
    c.x=d.getx();
    c.y=d.gety();
    cout<<"请输入半径:";
    cin>>c.r;
    return in;
}
ostream&operator<<(ostream& out,const Circle& c){
    cout<<"("<<c.x<<","<<c.y<<") 半径="<<c.r<<'\12';
    return out;
}
int locate(Point&t2,Circle&t1){
    Point t=t1;
    double x=t1.distance(t2);
    if (abs(x-t1.r)<1e-7)
        return 0;
    else if (x<t1.r)
        return -1;
    else
        return 1;
}
int main( ){
	Circle c1(3,2,4);
	Point p;
	cin>>p;
	fun(c1,p);
	return 0;
}


运行结果:

技术分享

第12周 【项目4 - 点、圆的关系】(3)

标签:

原文地址:http://blog.csdn.net/zp___waj/article/details/45890883

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