来个理论上提高浮点数精度就能提高答案精度的 做法。
首先对着椭圆做仿射变换缩放到单位圆上,方便处理。最后算出的面积乘 即可。
接下来要求三角形和单位圆的交的面积。考虑容斥原理,答案应该为圆的面积减去三个弓形的面积,再加上三块重复的部分。
三个小角可以视为两条端点相同的射线围成的部分与圆的交。需要分讨一下。
- 如果端点在圆的内部,该图形可以分为一个弓形和一个三角形。
- 如果端点在圆的外部:
- 两条射线与圆都不相交,可能是圆完全在该区域外或完全在该区域内。根据与端点连线形成的角度判断即可,面积为 或整个单位圆的面积。
- 否则,用单位圆的面积减去形成的弓形面积即可。
#include <bits/stdc++.h>
using namespace std;
istream& fin = cin;
ostream& fout = cout;
using ui = unsigned int;
using uli = unsigned long long int;
using li = long long int;
using numbers::pi;
struct Vector2D {
double x, y;
auto operator+=(Vector2D const& that) -> Vector2D& {
this->x += that.x, this->y += that.y;
return *this;
}
auto operator-=(Vector2D const& that) -> Vector2D& {
this->x -= that.x, this->y -= that.y;
return *this;
}
auto operator*=(double that) -> Vector2D& {
this->x *= that, this->y *= that;
return *this;
}
auto operator/=(double that) -> Vector2D& {
this->x /= that, this->y /= that;
return *this;
}
friend auto operator+(Vector2D a, Vector2D const& b) -> Vector2D {
return a += b;
}
friend auto operator-(Vector2D a, Vector2D const& b) -> Vector2D {
return a -= b;
}
friend auto operator*(Vector2D a, double b) -> Vector2D {
return a *= b;
}
friend auto operator*(double b, Vector2D a) -> Vector2D {
return a *= b;
}
friend auto operator/(Vector2D a, double b) -> Vector2D {
return a /= b;
}
friend auto operator/(double b, Vector2D a) -> Vector2D {
return a /= b;
}
friend auto operator*(Vector2D const& a, Vector2D const& b) -> double {
return a.x * b.x + a.y * b.y;
}
friend auto operator^(Vector2D const& a, Vector2D const& b) -> double {
return a.x * b.y - a.y * b.x;
}
auto operator-(void) const -> Vector2D {
return {-x, -y};
}
auto len(void) const -> double {
return sqrt(*this * *this);
}
auto argument(void) const -> double {
return atan2(y, x);
}
auto unsignedArgument(void) const -> double {
return abs(argument());
}
auto rotate(double d) -> Vector2D {
return {x * cos(d) - y * sin(d), y * cos(d) + x * sin(d)};
}
static auto angle(Vector2D const& a, Vector2D const& b) -> double {
return atan2(a ^ b, a * b);
}
static auto unsignedAngle(Vector2D const& a, Vector2D const& b) -> double {
double v = angle(a, b);
if (v < 0)
v += pi;
return v;
}
friend auto operator>>(istream& in, Vector2D& v) -> istream& {
return in >> v.x >> v.y;
}
};
constexpr double EPS = 1e-12;
auto solveQuadrticEquation = [](double a, double b,
double c) -> optional<pair<double, double>> {
auto d = b * b - 4 * a * c;
if (d < 0)
return {};
return pair{(-b - sqrt(d)) / (2 * a), (-b + sqrt(d)) / (2 * a)};
};
auto unitCircleAndLine = [](Vector2D const& p0, Vector2D const& dt) {
return solveQuadrticEquation(dt.x * dt.x + dt.y * dt.y,
2 * dt.x * p0.x + 2 * dt.y * p0.y,
p0.x * p0.x + p0.y * p0.y - 1);
};
auto triangleArea = [](Vector2D const& pa, Vector2D const& pb,
Vector2D const& pc) -> double {
double a = (pa - pb).len(), b = (pb - pc).len(), c = (pc - pa).len();
double p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
};
auto unitCircleArchArea = [](double d0, double d1) -> double {
double d = fmod(d1 - d0, pi * 2);
if (d < 0)
d += pi * 2;
return d / 2 - triangleArea({0, 0}, {1, 0}, {cos(d), sin(d)});
};
auto unitCircleArchAreaByLine = [](Vector2D const& p0, Vector2D const& dt) {
auto c = unitCircleAndLine(p0, dt);
if (c.has_value())
return unitCircleArchArea((p0 + c->first * dt).argument(),
(p0 + c->second * dt).argument());
else
return Vector2D::angle(-p0, dt) > 0 ? pi : 0;
};
auto unitCircleAndTwoLines = [](Vector2D const& p0, Vector2D const& dta,
Vector2D const& dtb) -> double {
auto ca = unitCircleAndLine(p0, dta), cb = unitCircleAndLine(p0, dtb);
if (p0.len() >= 1) {
if ((!ca.has_value() || ca->second < -EPS) &&
(!cb.has_value() || cb->second < -EPS)) {
double aa = dta.argument(), ab = dtb.argument(), ao = (-p0).argument();
double dx = fmod(ao - aa, pi * 2), dy = fmod(ab - aa, pi * 2);
if (dx < 0)
dx += pi * 2;
if (dy < 0)
dy += pi * 2;
return dx < dy ? pi : 0;
}
return pi -
(ca.has_value() && ca->second >= -EPS
? unitCircleArchArea((p0 + dta * ca->first).argument(),
(p0 + dta * ca->second).argument())
: 0) -
(cb.has_value() && cb->second >= -EPS
? unitCircleArchArea((p0 + dtb * cb->first).argument(),
(p0 + dtb * cb->second).argument())
: 0);
} else {
assert(ca.has_value() && cb.has_value() && ca->second >= -EPS &&
cb->second >= -EPS);
auto pa = p0 + dta * ca->second, pb = p0 + dtb * cb->second;
double sa = unitCircleArchArea(pa.argument(), pb.argument()),
sb = triangleArea(pa, pb, p0);
return Vector2D::angle(dta, dtb) >= 0 ? sa + sb : sa - sb;
}
};
auto main(void) -> int {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
Vector2D c0, c1, p0, p1, p2;
double ca;
fin >> c0 >> c1 >> ca >> p0 >> p1 >> p2;
ca /= 2;
assert(ca > 0);
{
double theta = -(c0 - c1).argument();
c0 = c0.rotate(theta), c1 = c1.rotate(theta), p0 = p0.rotate(theta),
p1 = p1.rotate(theta), p2 = p2.rotate(theta);
assert(abs(c0.y - c1.y) <= EPS && c0.x + EPS > +c1.x);
Vector2D mid = (c0 + c1) / 2;
c0 -= mid, c1 -= mid, p0 -= mid, p1 -= mid, p2 -= mid;
assert(abs(c0.y - c1.y) <= EPS && c0.x + EPS > +c1.x &&
abs(c0.x + c1.x) <= EPS);
}
double cc = c1.x, cb = sqrt(ca * ca - cc * cc);
c0.x /= ca, c1.x /= ca, p0.x /= ca, p1.x /= ca, p2.x /= ca;
c0.y /= cb, c1.y /= cb, p0.y /= cb, p1.y /= cb, p2.y /= cb;
if (Vector2D::angle(p1 - p0, p2 - p1) < 0)
swap(p1, p2);
assert(Vector2D::angle(p1 - p0, p2 - p1) >= 0);
fout << fixed << setprecision(10)
<< (pi - unitCircleArchAreaByLine(p1, p1 - p0) -
unitCircleArchAreaByLine(p2, p2 - p1) -
unitCircleArchAreaByLine(p0, p0 - p2) +
unitCircleAndTwoLines(p0, p0 - p1, p0 - p2) +
unitCircleAndTwoLines(p1, p1 - p2, p1 - p0) +
unitCircleAndTwoLines(p2, p2 - p0, p2 - p1)) *
ca * cb;
return 0;
}