最小圆覆盖算法实现C++

#include <iostream>
#include <math.h>
#include <typeinfo>
#define MAXN 3000

using namespace std;

template <typename T> class Circle;

template<typename T>
class Point {
	public:
		T x;
		T y;
		Point(T _x = 0, T _y = 0) {
			x = _x;
			y = _y;
		}
		~Point() {}
		//计算两点之间距离, this->p1 
		double distance(Point<T> p2);
		//判断三点是否在同一直线上, this->p1
		bool inOneLine(Point<T> p2, Point<T> p3); 
		//判断两点是否相同
		bool equal(Point<T> p2); 
}; 

template <typename T>
bool Point<T>::equal(Point<T> p2) {
	return (this->x == p2.x && this->y == p2.y);
}

template <typename T>
double Point<T>::distance(Point<T> p2){
	return sqrt((this->x - p2.x) * (this->x - p2.x) + (this->y - p2.y) * (this->y - p2.y));
}

template <typename T>
bool Point<T>::inOneLine(Point<T> p2, Point<T> p3) {
	Point<T> vect1(0, 0), vect2(0, 0);
	vect1.x = this->x - p2.x;
	vect1.y = this->y - p2.y;
	vect2.x = this->x - p3.x;
	vect2.y = this->y - p3.y;
	if (vect1.x == 0) {
		return (vect2.x == 0 && vect1.y == vect2.y); 
	}
	double x_div = (1.0 * vect2.x) / vect1.x;
	double y_div = (1.0 * vect2.y) / vect1.y;
	return (x_div == y_div);
}

template <typename T>
class Circle {
	private:
		Point<T> heart;
		double radius;
	public:
		Circle() {
			heart.x = 0;
			heart.y = 0;
			radius = 0.0;
		}
		~Circle() {}
		//根据两点坐标更新圆心和半径
		void circleTwoPoint(Circle<T> &circle, Point<T> p1, Point<T> p2);
		//根据三点坐标更新圆心和半径 
		void circleThreePoint(Circle<T> &circle, Point<T> p1, Point<T> p2, Point<T> p3);
		//判断点是否在园内 
		bool inOrOut(Point<T> point);
		//输出不在园内的点,ps数组保存点坐标 
		Point<T>* notInCircle(Point<T> ps[], int size);	
		//获得圆心坐标
		void printHeart() {
			cout << "x: " << this->heart.x << "   " << "y: " << this->heart.y << endl;
		} 
		//获取半径信息	 
		void printRadius() {
			cout << "radius: " << this->radius << endl; 
		}
};



template <typename T>
void Circle<T>::circleTwoPoint(Circle<T> &circle, Point<T> p1, Point<T> p2) {
	circle.heart.x = (p1.x + p2.x) * 0.5; 
	circle.heart.y = (p1.y + p2.y) * 0.5;
	circle.radius = circle.heart.distance(p2);
}

template <typename T>
void Circle<T>::circleThreePoint(Circle<T> &circle, Point<T> p1, Point<T> p2, Point<T> p3) {
	//判断三点是否在一条直线上 
	bool oneLine = p1.inOneLine(p2, p3);
	if(oneLine) throw "Error: Three Points on One Line.";
	double e = 2 * (p2.x - p1.x);
    double f = 2 * (p2.y - p1.y);
    double g = p2.x * p2.x - p1.x * p1.x + p2.y * p2.y - p1.y * p1.y;
    double a = 2 * (p3.x - p2.x);
    double b = 2 * (p3.y - p2.y);
    double c = p3.x * p3.x - p2.x * p2.x + p3.y * p3.y - p2.y * p2.y;
    circle.heart.x = (g * b - c * f) / (e * b - a * f);
    circle.heart.y = (a * g - c * e) / (a * f - b * e);
    circle.radius = circle.heart.distance(p1);
}

template <typename T>
bool Circle<T>::inOrOut(Point<T> point) {
	double distance = this->heart.distance(point);
	return (distance <= this->radius);
}

template <typename T>
Point<T>* Circle<T>::notInCircle(Point<T> ps[], int size) {
	Point<T>* newList = new Point<T>[MAXN];
	int j = 0;
	for (int i = 0; i < size; i++) {
		//不在圆内,推出数组 
		if (!this->inOrOut(ps[i])) {
			 newList[j++] = ps[i];
		}
	}
	return newList;
}

template <typename T>
void minCircleAlgorithm(Point<T>* points, int size) {
	try {
		Point<double> fPoints[3] = {};
		Circle<double> circle;
		//初始化最小圆,取第1点和第2点 
		circle.circleTwoPoint(circle, points[0], points[1]);
		for (int i = 2; i < size; i++) {
			//在园内,i = i + 1; 否则更新圆 
			if (!circle.inOrOut(points[i])) { 
				fPoints[0] = points[i];
				circle.circleTwoPoint(circle, points[0], fPoints[0]);
				break; 
			}
		} 
		//寻找第二个在圆边界上的点 
		Point<double>* outCircle1 = circle.notInCircle(points, size);
		fPoints[1] = outCircle1[0];
		circle.circleTwoPoint(circle, fPoints[0], fPoints[1]);
		//寻找第三个在圆边界上的点 
		Point<double>* outCircle2 = circle.notInCircle(points, size);
		for (int t = 0; t < size; t++) {
			if (!outCircle2[t].equal(fPoints[0])
			 && !outCircle2[t].inOneLine(fPoints[0], fPoints[1])
			) {
				fPoints[2] = outCircle2[t];
				break;
			}
		}
		cout << "随机散点坐标为: " << endl;
		for (int q = 0; q < size; q++) {
			cout << "(" << points[q].x << ", " << points[q].y << ")" << endl;
		} 
		circle.circleThreePoint(circle, fPoints[0], fPoints[1], fPoints[2]); 
		circle.printHeart();
		circle.printRadius();
	} catch(const char* e) {
		cout << e << endl;
	}
}

int main() {
	int N;
	cout << "请输入平面内点的个数和点坐标: " << endl;
	cin >> N; 
	Point<double>* points = new Point<double>[2 * N];
	for (int i = 0; i < N; i++) {
		cout << "第" << i + 1 << "个点x坐标为: " << endl;
		cin >> points[i].x;
		cout << "第" << i + 1 << "个点y坐标为: " << endl;
		cin >> points[i].y;
	}
	minCircleAlgorithm(points, N); 
	return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值