判断两圆的位置关系

题目要求:(1)定义一个Point类,其属性包括点的坐标,提供计算两点之间距离的方法;
                   (2)定义一个圆形类,其属性包括圆心和半径;
                   (3)创建两个圆形对象,提示用户输入圆心坐标和半径,判断两个圆是否相交,并输出结果。

类的声明:

MyPoint.h
#pragma once
class MyPoint
{
private:
double x;
double y;


public:
void setxy(double _x1, double _y1);
double getx();
double gety();
    double judge(MyPoint &p1, MyPoint &p2);

};
MyCircle.h#pragma once
#include"MyPoint.h"

class MyCircle
{
public:
void setxyr(double _x, double _y, double _r);
double getr();

private:
double x;
double y;
double r;

};

类的实现:

MyPont.cpp
#include<iostream>
#include "MyPoint.h"
#include <math.h>
#include"MyCircle.h"
using namespace std;
void MyPoint::setxy(double _x1, double _y1)
{
x = _x1;
y = _y1;
}


double MyPoint::getx()
{
return x;
}
double MyPoint::gety()
{
return y;
}

double MyPoint::judge(MyPoint &p1, MyPoint &p2)
{

double a = sqrt((p1.getx() - p2.getx())*(p1.getx() - p2.getx()) + (p1.gety() - p2.gety())*(p1.gety() - p2.gety()));
cout << a;
return a;


MyCircle.cpp
#include "MyCircle.h"
#include<iostream>
using namespace std;


void MyCircle::setxyr(double _x, double _y,double _r)
{
x = _x;
y = _y;
r = _r;
}


double MyCircle::getr()
{
return r;
}

测试程序:

#include<iostream>
#include"MyPoint.h"
#include"MyCircle.h"
//#include <stdlib.h>
using namespace std;
void main()
{
double x1, x2, y1, y2,r1,r2;
MyPoint P1, P2;
cout<<"请输入第一个圆的圆心坐标"<<endl;
cin>>x1>>y1;
P1.setxy(x1,y1);
cout << "请输入第二个圆的圆心坐标" << endl;
cin >> x2 >> y2;
P2.setxy(x2, y2);

double b = P2.judge(P1,P2);
MyCircle c1, c2;
cout<<"请分别输入两个圆的半径";
cin >> r1 >> r2;
c1.setxyr(x1,y1,r1);
c2.setxyr(x2, y2, r2);
cout << c1.getr()<<endl;
cout << c2.getr() << endl;
double c = b - (c1.getr()+c2.getr());
cout <<"c="<< c << endl;

if (c > 0)
{
cout << "两圆相离";
}
if (c < 0)
{
cout << "两圆相交";
}

if(c == 0)
{
cout << "两圆相切";
}
system("pause");
}

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值