面積二等分系の第二弾
問題概要
頂点の凸多角形が与えられる。原点を通る直線であって、この凸多角形を面積が等分になるように切断するものを求めよ。複数ある場合はどれか 1 つ求めればよい。
制約
- 原点は凸多角形に内包される
考えたこと
面積二等分系。この手の問題は「傾きに対する面積の応答」を考えるのがいいんだろうと思う。今回の場合は二分探索が使える。
傾き θ を 0 〜 π の範囲で指定したとき、切断した直線の左側の多角形の面積を f(θ) とする。凸多角形の面積を S とする。
f(θ) = S/2
となるような θ を求めたい。よって
- f(α) > S/2
- f(β) < S/2
となるような初期値 α, β が決まればいいのだが、実は θ = 0 で試すだけでよい。なぜなら
f(0) + f(π) = S
という関係がある (角度 π で切り取られるものは、角度 0 で切り取られるものの反対側に一致する)。よって、0 と π を初期値に選べば、どちらかは S/2 以上でもう一方は S/2 以下になっている。
#include <iostream> #include <vector> #include <cmath> #include <iomanip> #include <algorithm> using namespace std; #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template<class T> ostream& operator << (ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } //////////////////////////// // 基本要素 (点, 線分, 円) //////////////////////////// using DD = long double; const DD INF = 1LL<<60; // to be set appropriately const DD EPS = 1e-10; // to be set appropriately const DD PI = acos(-1.0); DD torad(int deg) {return (DD)(deg) * PI / 180;} DD todeg(DD ang) {return ang * 180 / PI;} /* Point */ struct Point { DD x, y; Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {} friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << ", " << p.y << ')';} }; inline Point operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);} inline Point operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);} inline Point operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);} inline Point operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);} inline Point operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);} inline Point operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);} inline Point conj(const Point &p) {return Point(p.x, -p.y);} inline Point rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);} inline Point rot90(const Point &p) {return Point(-p.y, p.x);} inline DD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;} inline DD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;} inline DD norm(const Point &p) {return dot(p, p);} inline DD abs(const Point &p) {return sqrt(dot(p, p));} inline DD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;} inline bool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;} inline bool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);} inline bool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);} inline Point operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);} /* Line */ struct Line : vector<Point> { Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) { this->push_back(a); this->push_back(b); } friend ostream& operator << (ostream &s, const Line &l) {return s << '{' << l[0] << ", " << l[1] << '}';} }; /* Circle */ struct Circle : Point { DD r; Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {} friend ostream& operator << (ostream &s, const Circle &c) {return s << '(' << c.x << ", " << c.y << ", " << c.r << ')';} }; /////////////////////// // 多角形 /////////////////////// // 多角形の面積 DD calc_area(const vector<Point> &pol) { DD res = 0.0; for (int i = 0; i < pol.size(); ++i) { res += cross(pol[i], pol[(i+1)%pol.size()]); } return res/2.0L; } // convex cut int ccw_for_convexcut(const Point &a, const Point &b, const Point &c) { if (cross(b-a, c-a) > EPS) return 1; if (cross(b-a, c-a) < -EPS) return -1; if (dot(b-a, c-a) < -EPS) return 2; if (norm(b-a) < norm(c-a) - EPS) return -2; return 0; } vector<Point> crosspoint_for_convexcut(const Line &l, const Line &m) { vector<Point> res; DD d = cross(m[1] - m[0], l[1] - l[0]); if (abs(d) < EPS) return vector<Point>(); res.push_back(l[0] + (l[1] - l[0]) * cross(m[1] - m[0], m[1] - l[0]) / d); return res; } vector<Point> ConvexCut(const vector<Point> &pol, const Line &l) { vector<Point> res; for (int i = 0; i < pol.size(); ++i) { Point p = pol[i], q = pol[(i+1)%pol.size()]; if (ccw_for_convexcut(l[0], l[1], p) != -1) { if (res.size() == 0) res.push_back(p); else if (!eq(p, res[res.size()-1])) res.push_back(p); } if (ccw_for_convexcut(l[0], l[1], p) * ccw_for_convexcut(l[0], l[1], q) < 0) { vector<Point> temp = crosspoint_for_convexcut(Line(p, q), l); if (temp.size() == 0) continue; else if (res.size() == 0) res.push_back(temp[0]); else if (!eq(temp[0], res[res.size()-1])) res.push_back(temp[0]); } } return res; } // solve Line find_line(double theta) { Point origin(0.0, 0.0); Point A(10.0 * cos(theta), 10.0 * sin(theta)); return Line(origin, A); } DD func(double theta, const vector<Point> &vp) { auto line = find_line(theta); auto cc = ConvexCut(vp, line); DD res = calc_area(cc); return res; }; void solve(const vector<Point> &vp) { DD S = calc_area(vp); DD low = 0, high = PI; DD flow = func(low, vp), fhigh = func(high, vp); for (int _ = 0; _ < 100; ++_) { DD mid = (low + high) / 2; DD fmid = func(mid, vp); if (flow < S/2) { if (fmid > S/2) high = mid; else low = mid; } else { if (fmid > S/2) low = mid; else high = mid; } //cout << mid << ": " << fmid << endl; } auto res = find_line(low); cout << fixed << setprecision(16) << res[1].x << " " << res[1].y << endl; } int main() { int N; while (cin >> N, N) { vector<Point> vp(N); for (int i = 0; i < N; ++i) cin >> vp[i].x >> vp[i].y; solve(vp); } }