其属性包含圆心和半径
创建两个圆形对象,提示用户输入圆心和半径,判断两圆是是否相交
main.cpp
#include <iostream>
#include <math.h>
#include "fhnClassList.h"
using namespace std;
int main()
{
double x = 0.0;
double y = 0.0;
double r = 0.0;
cout<<"Please input the x coordinate of first sircle : ";
cin>>x;
cout<<"Please input the y coordinate of first sircle : ";
cin>>y;
cout<<"Please input the radius of first sircle : ";
cin>>r;
Point a(x, y);
Circle c1(a, r);
cout<<"Please input the x coordinate of second sircle : ";
cin>>x;
cout<<"Please input the y coordinate of second sircle : ";
cin>>y;
cout<<"Please input the radius of second sircle : ";
cin>>r;
Point b(x, y);
Circle c2(b, r);
c1.isIntersect(c2);
return 0;
}
fhnClassList.h
#ifndef _FHN_CLASS_LIST_
#define _FHN_CLASS_LIST_
#pragma once
class Point
{
public:
Point();
Point(double a, double b);
Point(const Point & anotherPoint);
~Point();
double distance(Point anotherPoint);
private:
double x;
double y;
};
class Circle
{
public:
Circle();
Circle(Point center, double r);
~Circle();
bool isIntersect(const Circle & anotherCircle);
private:
Point circleCenter;
double radius;
};
#endif
fhnClassList.cpp
#include "fhnClassList.h"
#include <iostream>
using namespace std;
Point::Point()
{
}
Point::Point(double a, double b):x(a),y(b){}
Point::Point(const Point & anotherPoint)
{
this->x = anotherPoint.x;
this->y = anotherPoint.y;
}
Point::~Point()
{
}
double Point::distance(Point anotherPoint)
{
return sqrt(pow((this->x - anotherPoint.x), 2) + pow((this->y - anotherPoint.y), 2));
}
Circle::Circle()
{
}
Circle::Circle(Point center, double r):circleCenter(center),radius(r)
{
}
Circle::~Circle()
{
}
bool Circle::isIntersect(const Circle & anotherCircle)
{
if(this->circleCenter.distance(anotherCircle.circleCenter) <= (this->radius + anotherCircle.radius))
{
cout<<"the tow sircle is intersected."<<endl;
return true;
}
else
{
cout<<"the tow sircle is not intersected."<<endl;
return false;
}
}