第十二周上机项目4 点,圆的关系

【项目4 - 点、圆的关系】
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出);

(4)定义友元函数int locate,判断点p与圆的位置关系(返回值<0圆内,==0圆上,>0 圆外);

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;  
}  

/*
* Copyright (c) 2015,烟台大学计算机学院
* All right reserved.
* 作者:曹莉萍
* 文件:Demo.cpp
* 完成时间:2015年05月31日
* 版本号:v1.0
*/
#include <iostream>
#include<Cmath>
using namespace std;
class Point
{
public:
    Point(double a=0,double b=0):x(a),y(b) {} //构造函数
    double distance(const Point &p) const;  //求距离
    friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<”
protected:    //受保护成员
    double x,y;
};

double Point::distance(const Point &p) const    //求距离
{
    double dx = x-p.x;
    double dy = y-p.y;
    return sqrt(dx*dx+dy*dy);
}

ostream & operator<<(ostream &output,const Point &p)
{
    output<<"["<<p.x<<","<<p.y<<"]"<<endl;
    return output;
}

class Circle:public Point //circle是Point类的公用派生类
{
public:
    Circle(double a=0,double b=0,double r=0) :Point(a,b),radius(r) { }; //构造函数
    friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<”
    friend int locate(const Point &p, const Circle &c); //判断点p在圆上、圆内或圆外,返回值:<0圆内,==0圆上,>0 圆外
protected:
    double radius;
};

//重载运算符“<<”,使之按规定的形式输出圆的信息
ostream &operator<<(ostream &output,const Circle &c)
{
    output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius<<endl;
    return output;
}

//判断点p在圆内、圆c内或圆c外
int locate(const Point &p, const Circle &c)
{
    const Point cp(c.x,c.y); //圆心
    double d = cp.distance(p);
    if (abs(d - c.radius) < 1e-7)
        return 0;  //相等
    else if (d < c.radius)
        return -1;  //圆内
    else
        return 1;  //圆外
}

int main( )
{
    Circle c1(3,2,4);
    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)在圆类上重载关系运算符(6种),使之能够按圆的面积比较两个圆的大小。自编main函数完成测试。

/*
* Copyright (c) 2015,烟台大学计算机学院
* All right reserved.
* 作者:曹莉萍
* 文件:Demo.cpp
* 完成时间:2015年05月31日
* 版本号:v1.0
*/
#include <iostream>
#include<Cmath>
using namespace std;
class Point
{
public:
    Point(double a=0,double b=0):x(a),y(b) {}       //构造函数
protected:                                       //受保护成员
    double x,y;
};

class Circle:public Point //circle是Point类的公用派生类
{
public:
    Circle(double a=0,double b=0,double r=0): Point(a,b),radius(r) { }//构造函数
    double area ( ) const; //计算圆面积
    friend ostream &operator<<(ostream &,const 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 &);
protected:
    double radius;
};

//计算圆面积
double Circle::area( ) const
{
    return 3.14159*radius*radius;
}

//重载运算符“<<”,使之按规定的形式输出圆的信息
ostream &operator<<(ostream &output,const Circle &c)
{
    output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius;
    return output;
}

//重载关系运算符(种)运算符,使之能够按圆的面积比较两个圆的大小;
bool Circle::operator>(const Circle &c)
{
    return (this->radius - c.radius) > 1e-7;
}

bool Circle::operator<(const Circle &c)
{
    return (c.radius - this->radius) > 1e-7;
}

bool Circle::operator>=(const Circle &c)
{
    return !(*this < c);
}

bool Circle::operator<=(const Circle &c)
{
    return !(*this > c);
}

bool Circle::operator==(const Circle &c)
{
    return abs(this->radius - c.radius) < 1e-7;
}

bool Circle::operator!=(const Circle &c)
{
    return abs(this->radius - c.radius) > 1e-7;
}

int main( )
{
    Circle c1(3,2,4),c2(4,5,5);      //c2应该大于c1
    cout<<"圆c1( "<<c1<<" )的面积是 "<<c1.area()<<endl;
    cout<<"圆c2( "<<c2<<" )的面积是 "<<c2.area()<<endl;
    cout<<"圆c1 ";
    if(c1>c2) cout<<"大于, ";
    if(c1<c2) cout<<"小于, ";
    if(c1>=c2) cout<<"大于等于, ";
    if(c1<=c2) cout<<"小于等于, ";
    if(c1==c2) cout<<"等于, ";
    if(c1!=c2) cout<<"不等于, ";
    cout<<"圆c2"<<endl;

    return 0;
}



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

方法1:用引用类型参数

/*
* Copyright (c) 2015,烟台大学计算机学院
* All right reserved.
* 作者:曹莉萍
* 文件:Demo.cpp
* 完成时间:2015年05月31日
* 版本号:v1.0
*/
#include <iostream>
#include<Cmath>
using namespace std;
class Circle;  //由于在Point中声明友元函数crossover_point中参数中用了Circle,需要提前声明
class Point
{
public:
    Point(double a=0,double b=0):x(a),y(b) {}       //构造函数
    friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<”
    friend void crossover_point(Point &p,Circle &c, Point &p1,Point &p2 ) ;  //求交点的友元函数
protected:                                       //受保护成员
    double x,y;
};
ostream & operator<<(ostream &output,const Point &p)
{
    output<<"["<<p.x<<","<<p.y<<"]";
    return output;
}

class Circle:public Point //circle是Point类的公用派生类
{
public:
    Circle(double a=0,double b=0,double r=0):Point(a,b),radius(r) { } //构造函数
    friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<”
    friend void crossover_point(Point &p,Circle &c, Point &p1,Point &p2 ) ;  //求交点的友元函数
protected:
    double radius;
};

//重载运算符“<<”,使之按规定的形式输出圆的信息
ostream &operator<<(ostream &output,const Circle &c)
{
    output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius;
    return output;
}

//给定一点p,求出该点与圆c的圆心相连成的直线与圆的两个交点p1和p2
//关键问题是求得的交点如何返回
//方案1:利用引用类型的形式参数,注意,下面的p1和p2将“带回”求得的结果
//crossover_point函数已经声明为Point和Circle类的友元函数,类中私有成员可以直接访问
void crossover_point(Point &p, Circle &c, Point &p1,Point &p2 )
{
    p1.x = (c.x + sqrt(c.radius*c.radius/(1+((c.y-p.y)/(c.x-p.x))*((c.y-p.y)/(c.x-p.x)))));
    p2.x = (c.x - sqrt(c.radius*c.radius/(1+((c.y-p.y)/(c.x-p.x))*((c.y-p.y)/(c.x-p.x)))));
    p1.y = (p.y + (p1.x -p.x)*(c.y-p.y)/(c.x-p.x));
    p2.y = (p.y + (p2.x -p.x)*(c.y-p.y)/(c.x-p.x));
}

int main( )
{
    Circle c1(3,2,4);
    Point p1(1,1),p2,p3;

    crossover_point(p1,c1, p2, p3);

    cout<<"点p1: "<<p1<<endl;
    cout<<"与圆c1: "<<c1<<endl;
    cout<<"的圆心相连,与圆交于两点,分别是:"<<endl;
    cout<<"交点1: "<<p2<<endl;
    cout<<"交点2: "<<p3<<endl;
    return 0;
}

方法2:结构体(定义两个包含两个点的结构体,用于返回值)

/*
* Copyright (c) 2015,烟台大学计算机学院
* All right reserved.
* 作者:曹莉萍
* 文件:Demo.cpp
* 完成时间:2015年05月31日
* 版本号:v1.0
*/
#include <iostream>
#include<Cmath>
using namespace std;
class Circle;  //由于在Point中声明友元函数crossover_point中参数中用了Circle,需要提前声明
struct DoublePoint;  //也先声明,Point中声明友元函数crossover_point中要用到
class Point
{
public:
    Point(double a=0,double b=0):x(a),y(b) {}       //构造函数
    friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<”
    friend DoublePoint crossover_point(Point &p,Circle &c) ;  //求交点的友元函数
protected:                                       //受保护成员
    double x,y;
};
ostream & operator<<(ostream &output,const Point &p)
{
    output<<"["<<p.x<<","<<p.y<<"]";
    return output;
}

class Circle:public Point //circle是Point类的公用派生类
{
public:
    Circle(double a=0,double b=0,double r=0):Point(a,b),radius(r) { } //构造函数
    friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<”
    friend DoublePoint crossover_point(Point &p,Circle &c) ;  //求交点的友元函数
protected:
    double radius;
};

//重载运算符“<<”,使之按规定的形式输出圆的信息
ostream &operator<<(ostream &output,const Circle &c)
{
    output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius;
    return output;
}

struct DoublePoint   //专门用于返回值的结构体类型
{
    Point p1;
    Point p2;
};

//给定一点p,求出该点与圆c的圆心相连成的直线与圆的两个交点
//方案2:结果返回到DoublePoint类型的结构体中
//crossover_point函数已经声明为Point和Circle类的友元函数,类中私有成员可以直接访问
DoublePoint crossover_point(Point &p, Circle &c)
{
    DoublePoint pp;
    pp.p1.x = (c.x + sqrt(c.radius*c.radius/(1+((c.y-p.y)/(c.x-p.x))*((c.y-p.y)/(c.x-p.x)))));
    pp.p2.x = (c.x - sqrt(c.radius*c.radius/(1+((c.y-p.y)/(c.x-p.x))*((c.y-p.y)/(c.x-p.x)))));
    pp.p1.y = (p.y + (pp.p1.x -p.x)*(c.y-p.y)/(c.x-p.x));
    pp.p2.y = (p.y + (pp.p2.x -p.x)*(c.y-p.y)/(c.x-p.x));
    return pp;
}

int main( )
{
    Circle c1(3,2,4);
    Point p1(1,1);
    DoublePoint pp;

    pp = crossover_point(p1,c1);

    cout<<"点p1: "<<p1<<endl;
    cout<<"与圆c1: "<<c1<<endl;
    cout<<"的圆心相连,与圆交于两点,分别是:"<<endl;
    cout<<"交点1: "<<pp.p1<<endl;
    cout<<"交点2: "<<pp.p2<<endl;
    return 0;
}



好复杂的项目4,千辛万苦弄完以后竟然有种字符搬家的感觉。

赶快吸收为自己的吧。

加油加油!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值