设计一个圆形类(AdvCircle),和一个点类(Point),计算点在圆内部还是圆外
point.cpp
circle.h
即:求点和圆的关系(圆内和圆外)
point.h
#ifndef __POINT_H__
#define __POINT_H__
class Point
{
public:
// 设置点的坐标
void setXY(int x, int y);
int getX();
int getY();
// 求当前的点和另一个点的距离的平方
int distance(Point &p);
private:
int m_x; // x的坐标
int m_y; // y的坐标
};
point.cpp
#include "Point.h"
void Point::setXY(int x, int y)
{
m_x = x;
m_y = y;
}
int Point::getX()
{
return m_x;
}
int Point::getY()
{
return m_y;
}
int Point::distance(Point &p)
{
int dis = (m_x - p.getX())*(m_x - p.getX()) + (m_y - p.getY()) * (m_y - p.getY());
// int dis = (m_x - p.m_x)*(m_x - p.m_x) + (m_y - p.m_y) * (m_y - p.m_y);
return dis;
}
circle.h
#ifndef __CIRCLE_H__
#define __CIRCLE_H__
#include "Point.h"
class Circle
{
public:
void setCircle(int r, int x, int y);
bool inCircle(Point &p);
private:
int m_r; // 半径
Point m_rp; // 圆心
};
#endif // __CIRCLE_H__
circle.cpp
#include "Circle.h"
void Circle::setCircle(int r, int x, int y)
{
m_r = r; // 设置半径
m_rp.setXY(x, y); // 设置圆心
}
bool Circle::inCircle(Point &p)
{
if (m_rp.distance(p) <= m_r*m_r) // 点p 到 圆心的长度小于等于半径
return true;
else
return false;
}
main.cpp
#include <stdio.h>
#include "Circle.h"
#include "Point.h"
int main()
{
Circle c;
c.setCircle(20, 20, 20);
Point p;
p.setXY(10,20);
if (c.inCircle(p))
{
printf ("在圆内\n");
}
else
{
printf ("在圆外\n");
}
return 0;
}