欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c++ 判断点和圆位置关系(类的声明和类的实现分开)

程序员文章站 2022-06-28 21:29:47
Point.h: #pragma onceclass Point{private: double p_x, p_y;public: void setXY(double x,double y); double getx(); double gety();}; AdvCircle.h: #pragma ......

point.h:

#pragma once
class point
{
private:
double p_x, p_y;
public:
void setxy(double x,double y);
double getx();
double gety();
};

 advcircle.h:

#pragma once
#include "point.h"
class advcircle
{
private:
double m_x, m_y,m_r;
public:
void set(double x, double y,double r);
int judge(point p);
};

point.cpp:

#include "point.h"

void point::setxy(double x, double y) {
p_x = x;
p_y = y;
}
double point::getx() {
return p_x;
}
double point::gety() {
return p_y;
}

advcircle.cpp:

#include "advcircle.h"
#include "cmath"

void advcircle::set(double x, double y,double r) {
m_x = x;
m_y = y;
m_r = r;
}

int advcircle::judge(point p) {
double dis = hypot(m_x-p.getx(),m_y-p.gety());
if (dis > m_r) return 1;//圆外
else if (dis == m_r) return 2;//圆上
else if (dis < m_r) return 3;//圆内
}

circle_relation_point.cpp:

#include <iostream>
#include"point.h"
#include"advcircle.h"

using namespace std;

int main()
{
point p; advcircle c1;
double x, y,r;
int jud;
cout << "请输入点的x坐标:" << endl;
cin >> x;
cout << "请输入点的y坐标:" << endl;
cin >> y;
p.setxy(x, y);
cout << "请输入圆心的x坐标:" << endl;
cin >> x;
cout << "请输入圆心的y坐标:" << endl;
cin >> y;
cout << "请输入圆心的半径:" << endl;
cin >> r;
c1.set(x, y, r);
jud = c1.judge(p);
if (jud == 1) cout << "点在圆外" << endl;
else if (jud == 2) cout << "点在圆上" << endl;
else if (jud == 3) cout << "点在圆内" << endl;
system("pause");
}