您的位置:首页 > 其它

uva 11177 不明所以的凸多边形与圆的面积模板——二分

2016-03-05 16:04 295 查看
#include <bits/stdc++.h>
using namespace std;
const int maxn = 50 + 10;
const double PI = acos(-1);
struct Point
{
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
typedef vector<Point> Polygon;
Vector operator +(Vector A, Vector B)//
{
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator -(Point A, Point B)//
{
return Vector(A.x - B.x , A.y - B.y);
}
Vector operator *(Vector A, double p)//
{
return Vector(A.x * p, A.y * p);
}
Vector operator /(Vector A, double p)//
{
return Vector(A.x / p, A.y / p);
}
bool operator <(const Point &a, const Point &b)//
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
double Dot(Vector A, Vector B)//
{
return A.x * B.x + A.y * B.y;
}
double Length(Vector A)//
{
return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B)//
{
return acos(Dot(A, B) / Length(A) / Length(B));
}
double Cross(Vector A, Vector B)//
{
return A.x * B.y - A.y * B.x;
}
const double eps = 1e-10;
int dcmp(double x)//
{
if (fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
bool operator ==(const Point &a, const Point &b)//
{
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
void read_point(Point &p)
{
double x, y;
scanf("%lf%lf", &x, &y);
p = Point(x, y);
}
struct Circle
{
Point c;
double r;
Circle(Point c, double r): c(c), r(r) {}
Point point(double a)
{
return Point(c.x + cos(a) * r, c.y + sin(a) * r);
}
};

int n, kase;
double R;
Point P[maxn];
//--------------------------------------不明所以的模板分割线------------------------------------------------------------------
bool isPointInCircle(Point P, Circle C)
{
return dcmp(Length(C.c - P) - C.r) <= 0;
}

bool OnSegment(Point p, Point p1, Point p2)
{
return dcmp(Dot(p1 - p, p2 - p)) <= 0;//这里很费解
}

void SegmentCircleIntersection(Point p1, Point p2, Circle C, Point* sol, int& num)
{
double t1, t2;
double a = (p1 - C.c).x, b = (p2 - p1).x, c = (p1 - C.c).y, d = (p2 - p1).y;
double e = b * b + d * d, f = 2 * (a * b + c * d), g = a * a + c * c - C.r * C.r;
double delta = f * f - 4 * e * g;
Point p;
num = 0;
if (dcmp(delta) < 0)
return;
else if (dcmp(delta) == 0)
{
t1 = t2 = (-f) / (2 * e);
p = p1 + (p2 - p1) * t1;
if (OnSegment(p, p1, p2))
sol[num++] = p;
}
else
{
t1 = (-f - sqrt(delta)) / (2 * e);
p = p1 + (p2 - p1) * t1;
if (OnSegment(p, p1, p2))
sol[num++] = p;
t2 = (-f + sqrt(delta)) / (2 * e);
p = p1 + (p2 - p1) * t2;
if (OnSegment(p, p1, p2))
sol[num++] = p;
}
}

double FanArea(Circle C, Vector v1, Vector v2)
{
double rad = Angle(v1, v2);
if (dcmp(rad - PI) == 0)
rad = 0;
return C.r * C.r * rad * 0.5;
}

double GetCommonArea(Point p1, Point p2, Point p3, Circle C)
{
int flag1 = isPointInCircle(p2, C);
int flag2 = isPointInCircle(p3, C);
int num;
Point sol[5];
if (flag1 + flag2 == 2)
return fabs(Cross(p2 - p1, p3 - p1)) * 0.5;
if (flag1 + flag2 == 1)
{
SegmentCircleIntersection(p2, p3, C, sol, num);
if (flag1)
return FanArea(C, p3 - p1, sol[0] - p1) + fabs(Cross(p2 - p1, sol[0] - p1)) * 0.5;
return FanArea(C, p2 - p1, sol[0] - p1) + fabs(Cross(p3 - p1, sol[0] - p1)) * 0.5;
}
SegmentCircleIntersection(p2, p3, C, sol, num);//注意两个端点对应不同交点,写反就错了
if (num == 2)
return FanArea(C, p2 - p1, sol[0] - p1) + fabs(Cross(sol[0] - p1, sol[1] - p1)) * 0.5 + FanArea(C, p3 - p1, sol[1] - p1);
return FanArea(C, p2 - p1, p3 - p1);
}
double CommonArea(Circle C)
{
double res = 0;
for (int i = 0; i < n; i++)
res += GetCommonArea(C.c, P[i], P[i + 1], C);
return res;
}
//---------------------------------不明所以的分割线-------------------------------------------------------------------------------------------
int main(int argc, char const *argv[])
{
while (~scanf("%d", &n) && n)
{
scanf("%lf", &R);
for (int i = 0; i < n; i++)
read_point(P[i]);
P
= P[0];
double l = 0, r = 1E9, mid;
while (dcmp(l - r))
{
mid = (l + r) / 2.0;
if (dcmp(CommonArea(Circle(Point(0, 0), mid)) - R) >= 0) r = mid;
else l = mid;
}
printf("Case %d: %.2lf\n", ++kase, mid);
}
return 0;
}


把凸n边形的每条边都和圆判断关系,如果是边的两点都在圆内,两条边对应一个三角形的面积,如果一个点在圆外一个在圆内(包括边界),那就是一个三角形加一个扇形,如果两点都在圆外,分两种情况,和圆有两个交点的话是两个扇形和一个三角形,如果和圆无交点是一个扇形。

但是这份模板还是没全看懂。。。。。。。。。水过去了;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: