-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo-lib.cpp
50 lines (42 loc) · 1.27 KB
/
geo-lib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Visualize geometric shapes using desmos
// Print several objects using `cerr << my_segment.to_desmos() << endl;`, then paste
// (multiple lines at once is allowed) on https://www.desmos.com/calculator?lang=ja
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Point {
ll x, y;
ll cross(const Point p) const { return x * p.y - y * p.x; }
Point operator-(const Point p) const { return {x - p.x, y - p.y}; }
};
struct Segment {
Point p, q;
string to_desmos() const {
stringstream ss;
ss << fixed << setprecision(6) << "\\left(\\left(1-t\\right)\\cdot" << p.x
<< "+t\\cdot" << q.x << ",\\left(1-t\\right)\\cdot" << p.y << "+t\\cdot" << q.y
<< "\\right)";
return ss.str();
}
};
struct Circle {
Point center;
long double radius;
string to_desmos() const {
stringstream ss;
ss << "\\left(x-" << center.x << "\\right)^{2}+\\left(y-" << center.y
<< "\\right)^{2}=" << radius << "^{2}";
return ss.str();
}
};
string polygon_to_str(const vector<Point>& polygon) {
stringstream ss;
ss << fixed << setprecision(6);
ss << "polygon(";
for (int i = 0; i < (int)polygon.size(); i++) {
if (i > 0) ss << ", ";
ss << "(" << polygon[i].x << ", " << polygon[i].y << ")";
}
ss << ")";
return ss.str();
}