#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() {}
double distance(Point<T> p2);
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);
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;
circle.circleTwoPoint(circle, points[0], points[1]);
for (int i = 2; i < size; i++) {
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;
}