第十二周项目4 - 点、圆的关系

/*
 *Copyright (c) 2014, 烟台大学计算机学院
 *All rights reserved.
 *文件名称:week12-4.cpp
 *作者:高赞
 *完成日期:2015年 5 月 23 日
 *版本号:v1.0
 *
 *
 */
#include <iostream>
#include <cmath>
using namespace std;
class Point //定义坐标点类
{
public:
    Point(double x0=0, double y0=0):x(x0), y(y0) {}
    double getx()
    {
        return x;
    }
    double gety()
    {
        return y;
    }
friend ostream &operator<<(ostream &,Point &);
protected:
    double x,y;   //点的横坐标和纵坐标
};
class Circle:public Point
{
public:
    Circle(double x0=0,double y0=0,double r0=0):Point(x0,y0),r(r0) {}
    double getr()
    {
        return r;
    }
    double area()
    {
        return 3.1415926*r*r;
    }
    bool operator > (const Circle &c)
    {
        if(r>c.r)
            return true;
        else return false;
    }
        bool operator < (const Circle &c)
        {
        if(r<c.r)
        return true;
        else return false;
    }
    bool operator >= (const Circle &c)
    {
        if(r>=c.r)
            return true;
        else return false;
    }
        bool operator <= (const Circle &c)
        {
        if(r<=c.r)
        return true;
        else return false;
    }
    bool operator == (const Circle &c)
    {
        if(r==c.r)
            return true;
        else return false;
    }
        bool operator != (const Circle &c)
        {
        if(r!=c.r)
        return true;
        else return false;
    }
    friend double locate(Point &,Circle &);
    friend ostream &operator<<(ostream &,Circle &);
protected:
    double r;
};
double locate(Point &p,Circle &c )
{
    double m,n;
    n=(p.getx()-c.x)*(p.getx()-c.x)+(p.gety()-c.y)*(p.gety()-c.y);
    m=sqrt(n);
    return m-c.r;
}
ostream &operator<<(ostream &put,Point &p)
{
    put<<"("<<p.x<<","<<p.y<<")"<<endl;
    return put;
}
ostream &operator<<(ostream &put,Circle &c)
{
    put<<"圆心:("<<c.x<<","<<c.y<<")"<<endl<<"半径:"<<c.r<<endl;
    return put;
}
void intersectPoint(Point &p,Circle &c)
{
    double x1,y1,x2,y2,m,n;
    m=(c.gety()-p.gety())/(c.getx()-p.getx());
    n=sqrt(c.gety()*c.gety()/(1+m*m));
    x1=c.getx()+n;
    y1=m*(x1-p.getx())+p.gety();
    x2=c.getx()-n;
    y2=m*(x2-p.getx())+p.gety();
    cout<<"("<<x1<<","<<y1<<"),("<<x2<<","<<y2<<")";
}
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;
    if (c1>c2) cout<<"c1 > c2"<<endl;
    if (c1<c2) cout<<"c1 < c2"<<endl;
    if (c1==c2) cout<<"c1 == c2"<<endl;
    if (c1!=c2) cout<<"c1 != c2"<<endl;
    if (c1>=c2) cout<<"c1 >= c2"<<endl;
    if (c1<=c2) cout<<"c1 <= c2"<<endl;
    cout<<"点p1与c2圆心相连成的直线和圆的两个交点为:";
    intersectPoint(p1,c2);
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值