Skip to content
Snippets Groups Projects
Commit 7b1e723b authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Add circle

parent 8b25fdc8
No related branches found
No related tags found
No related merge requests found
/// Circle
struct Circle {
Point<double> c;
double r;
Circle(Point<double> c, double r) : c(c), r(r) {}
// Circumcircle
Circle(Point<double> a, Point<double> b, Point<double> c) {
Point<double> u((b - a).y, -(b - a).x);
Point<double> v((c - a).y, -(c - a).x);
Point<double> n = (c - b)*0.5;
double t = u.cross(n) / v.cross(u);
this->c = (a + c)*0.5 + v*t;
this->r = dist(this->c, a);
}
bool contains(Point<double> p) {
return (dist(c, p) <= r + EPS);
}
};
// Minumum enclosing circle
Circle min_enclosing(vector<Point<double>> p) {
random_shuffle(all(p));
Circle C(p[0], 0.0);
for (int i = 0; i < p.size(); ++i) {
if (C.contains(p[i])) continue;
C = Circle(p[i], 0.0);
for (int j = 0; j < i; ++j) {
if (C.contains(p[j])) continue;
C = Circle((p[j] + p[i])*0.5, 0.5*dist(p[j], p[i]));
for (int k = 0; k < j; ++k) {
if (C.contains(p[k])) continue;
C = Circle(p[j], p[i], p[k]);
}
}
}
return C;
}
......@@ -12,6 +12,7 @@ struct Point {
Point operator+(Point p) { return Point(x+p.x, y+p.y); }
Point operator-(Point p) { return Point(x-p.x, y-p.y); }
Point operator*(T s) { return Point(x*s, y*s); }
T dot(Point p) { return (x*p.x) + (y*p.y); }
T cross(Point p) { return (x*p.y) - (y*p.x); }
......@@ -51,6 +52,10 @@ struct Point {
}
};
double dist(Point<double> a, Point<double> b) {
return hypot(a.x - b.x, a.y - b.y);
}
template <typename T>
struct Segment {
Point<T> a, b;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment