设计一个圆形类(AdvCircle),和一个点类(Point),计算点在圆内部还是圆外
即:求点和圆的关系(圆内和圆外)
point.h
#ifndef __POINT_H__
#define __POINT_H__
class point
{
public:
void init(int x,int y);
//计算点到圆心的距离
int distance(point &p);
private:
int _x;
int _y;
};
#endif
point.cpp
#include <stdio.h>
#include "point.h"
void point::init(int x, int y)
{
_x = x;
_y = y;
}
int point::distance(point &p)
{
int dis = (_x - p._x)*(_x - p._x) + (_y -p._y)*(_y - p._y);
return dis;
}
circle.h
#ifndef __circle_h__
#define __circle_h__
#include "point.h"
class circle
{
public:
void init(int r,int x,int y);
//判断点是否在园内
bool incircle(point &p);
private:
int _r;
point _c;
};
#endif
circle.cpp
#include <stdio.h>
#include "circle.h"
void circle::init(int r,int x,int y)
{
_r = r;
_c.init(x, y);
}
bool circle::incircle(point &p)
{
int dis = _c.distance(p);
if (dis < _r *_r)
{
return true;
}
else
{
return false;
}
}
main.cpp
#include <iostream>
#include "circle.h"
#include "point.h"
using namespace std;
int main()
{
point p;
p.init(1, 5);
circle c;
c.init(3, 0, 0);
if(c.incircle(p))
{
cout<<"在圆内 "<< endl;
}
else
{
cout<<"在圆外"<< endl;
}
return 0;
}