C++ A method of determining whether a point is inside a circle

• 2020-06-07 04:55:07
• OfStack

This paper shares the method of C++ to determine whether a point is inside a circle for your reference. The specific content is as follows

The header file for the circle

#ifndef __CRICLE_H__
#define __CRICLE_H__
#include "point.h"
class Circle
{
public:
// Create a circle
void init(int r, int x, int y);
// Determine if the point is   The current round   Within the
bool inCircle(Point &p);
private:
Point _c;
int _r;
};

#endif// __CRICLE_H__

#ifndef __POINT_H__
#define __POINT_H__

class Point
{
public:
// Create a point
void init(int x, int y);
// measuring 1 The square of the distance between the point and the current point
int distance(Point &p);
private:
int _x;
int _y;
};

#endif//__POINT_H__

Source file for the circle

#include "circle.h"

void Circle::init(int r, int x, int y)
{
_r = r;
_c.init(x, y);
}

bool Circle::inCircle(Point &p)
{
// Calculation point p and   The current center of the circle c  The distance from the
int dis = p.distance(_c);

if (dis <= _r*_r)
return true;
else
return false;
}

Point to the source file

#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;
}

main file

#include <iostream>
#include "circle.h"
#include "point.h"
using namespace std;

int main()
{
//1 A point
Point p;
p.init(1,2);

Circle c;
c.init(3, 0, 0);

if (c.inCircle(p))
cout << " In the circle " << endl;
else
cout << " Outside the circle " << endl;

return 0;
}

Related articles: