第12周 项目四-点、圆关系(4)

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


代码:

#include <iostream>
using namespace std;
class Point
{
public:
    int x;
    int y;
public:
    Point(int a,int b):x(a),y(b) {}
    friend ostream &operator <<(ostream &out,Point &a);
};
ostream &operator <<(ostream &out,Point &a)
{
    cout<<"x="<<a.x<<" y="<<a.y<<endl;
    return out;
}
class Circle:public Point
{
protected:
    int r;
public:
    Circle(int a,int b,int c):Point(a,b),r(c) {}
    friend ostream &operator <<(ostream  &out,Circle &a);
    friend int locate(Point &a,Circle &b);
};
ostream &operator <<(ostream  &out,Circle &a)
{
    cout<<"圆心:"<<" ("<<a.x<<"<"<<a.y<<") "<<"半径:"<<a.r<<endl;
    return out;
}
int locate(Point &a,Circle &b)
{
    return (b.r*b.r-(a.x-b.x)*(a.x-b.x)-(a.y-b.y)*(a.y-b.y));
}
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;
}


运行结果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的Anthony

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值