一.项目描述
点与圆有三种关系:
点在圆外
点在圆上
点在圆内
计算点到圆心的距离就能判断点在圆的哪个地方。
二.项目结构
三.include文件
3.1 Circle类的声明
Circle.h
// 防止头文件重复包含
#pragma once
// #include<iostream>
#include "Point.h"
using namespace std;class Circle
{
public:// 设置半径void setR(int r);//获取半径int getR();//设置圆心void setCenter(Point center);Point getCenter();private:int m_R; //半径Point m_Center; //圆心
};
3.2 Point类的声明
Point.h
#pragma once
#include<iostream>
using namespace std;class Point
{
public:// 设置xvoid setX(int x);void setY(int y);// 获取yint getX();int getY();private:int m_X;int m_Y;
};
四.src文件
4.1 Circle类的定义
Circle.cpp
#include "Circle.h"// 设置半径
void Circle::setR(int r)
{m_R = r;
}
//获取半径
int Circle::getR()
{return m_R;
}
//设置圆心
void Circle::setCenter(Point center)
{m_Center = center;
}
Point Circle::getCenter()
{return m_Center;
}
4.2 Point类的定义
Point.cpp
#include "Point.h"void Point::setX(int x)
{m_X = x;
}
void Point::setY(int y)
{m_Y = y;
}// 获取y
int Point::getX()
{return m_X;
}int Point::getY()
{return m_Y;
}
五.main.cpp
#include<iostream>using namespace std;
#include "Circle.h"
#include "Point.h"// 判断点和圆的关系
void isInCircle(Circle &c,Point &p)
{//计算两点之间距离的平方int distance = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) + (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());//计算半径的平方int rDistance = c.getR() * c.getR();//判断关系if (distance == rDistance){cout<<"点在圆上"<<endl;}else if (distance>rDistance){cout<<"点在圆外"<<endl;}else{cout<<"点在圆内"<<endl;}}int main()
{// 创建圆Circle c;c.setR(10);Point center;center.setX(10);center.setY(0);c.setCenter(center);//创建点Point p;p.setX(10);p.setY(10);//判断两者关系isInCircle(c,p);return 0;
}