HDU - 4404 Worms (计算几何—圆与多边形的面积交)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GYH0730/article/details/82806508

Worms

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 704    Accepted Submission(s): 391


Problem Description

Worms is a series of turn-based computer games. Players control a small platoon of earthworms across a deformable landscape, battling other computer- or player-controlled teams. The game feature bright and humorous cartoon-style animation and a varied arsenal of bizarre weapons.


During the course of the game, players take turns selecting one of their worms. They then use whatever tools and weapons available to attack and kill the opponents’ worms. Over fifty weapons and tools may be available each time a game is played, and differing selections of weapons and tools can be saved into a “scheme” for easy selection in future games.

When most weapons are used, they cause explosions that deform the terrain, creating circular cavities. If a worm is hit by a weapon, the amount of damage dealt to the worm will be removed from the worm’s initial amount of health. When a worm fall into the water or its health is reduced to zero, it dies.

In this problem, the terrain of a stone can be described as a simple polygon. The worms only use the time bombs. Once a time bomb is thrown, it is only attracted by the force of gravity. In other words, the flying track of the bomb is a parabola. When it reaches the stone (the polygon), the bomb does not blow off or stop immediately. It will still fly along the parabola regardless of the resistance of the stone due to its special character. The time bomb can only be triggered by the timer. When the preset time is used up, the bomb blows up and eliminates all the materials within its explosion range. You need to calculate the area of the eliminated materials of one explosion.

 Input

There are multiple test cases.
The first line of a test case contains seven floating numbers x0, y0, v0, θ, t, g, R. (x0, y0) is the position of the worm who throws the bomb, which could be inside the polygon or outside the polygon (even in the sky or under the water). The initial value of velocity is v0 which forms aθ (0≤θ<90) angle with the positive direction of x-axis. The bomb is always thrown upwards. The preset time is t, which is also the time of flying. The value of acceleration of gravity of the worms’ planet is g. The direction of gravity is the negative direction of y-axis. The explosion range is a circle and R is the radius.

The second line contains an integer n (3≤n≤100), which indicates the number of edges of the stone(simple polygon). The following n lines contain two real numbers xi and yi each, which describe the coordinates of a vertex. Two vertexes in adjacent lines are adjacent on the polygon.

The input contains multiple test cases. It is ended by “0 0 0 0 0 0 0”.

 Output

For each test case, output one line containing the area of the eliminated materials of the explosion rounded to two digits to the right of the decimal point. 

 Sample Input

0 0 15 45 10 2 10 3 100 0 200 0 100 100 0 0 0 0 0 0 0

 Sample Output

228.74

 Source

2012 ACM/ICPC Asia Regional Jinhua Online

分解斜抛运动,计算出水平方向与竖直方向的位移,得到末点的坐标,然后就是圆与多边形面积交的模板了

#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int inf = 1e20;
const int maxp = 1005;
const double pi = acos(-1.0);
int sgn(double x)
{
    if(fabs(x) < eps) return 0;
    if(x < 0) return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;
        y = _y;
    }
    void input()
    {
        scanf("%lf %lf",&x,&y);
    }
    Point operator + (const Point &b) const
    {
        return Point(x + b.x,y + b.y);
    }
    Point operator - (const Point &b) const
    {
        return Point(x - b.x,y - b.y);
    }
    double operator ^ (const Point &b) const
    {
        return x * b.y - y * b.x;
    }
    double operator * (const Point &b) const
    {
        return x * b.x + y * b.y;
    }
    Point operator * (const double &k) const
    {
        return Point(x * k, y * k);
    }
    double distance(Point p)
    {
        return hypot(x - p.x,y - p.y);
    }
    double rad(Point a,Point b)
    {
        Point p = *this;
        return fabs(atan2( fabs((a - p) ^ (b - p)),(a - p) * (b - p) ) );
    }
    Point operator / (const double &k) const
    {
        return Point(x / k, y / k);
    }
    double len()
    {
        return hypot(x,y);
    }
    double len2()
    {
        return x * x + y * y;
    }
    Point trunc(double r)
    {
        double l = len();
        if(!sgn(l)) return *this;
        r /= l;
        return Point(x * r,y * r);
    }
};
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;
        e = _e;
    }
    double length()
    {
        return s.distance(e);
    }
    double dispointtoline(Point p)
    {
        return fabs( (p - s) ^ (e - s)) / length();
    }
    Point lineprog(Point p)
    {
        return s + ( ((e - s) * ((e - s) * (p - s))) / ((e - s).len2()) );
    }
};
struct circle
{
    Point p;
    double r;
    circle(){}
    circle(double x,double y,double _r)
    {
        p = Point(x,y);
        r = _r;
    }
    int relation(Point b)
    {
        double dst = b.distance(p);
        if(sgn(dst - r) < 0) return 2;
        else if(sgn(dst - r) == 0) return 1;
        else return 0;
    }
    int relationline(Line v)
    {
        double dst = v.dispointtoline(p);
        if(sgn(dst - r) < 0) return 2;
        else if(sgn(dst - r) == 0) return 1;
        return 0;
    }
    int pointcrossline(Line v,Point &p1,Point &p2)
    {
        if(!(*this).relationline(v)) return 0;
        Point a = v.lineprog(p);
        double d = v.dispointtoline(p);
        d = sqrt(r * r - d * d);
        if(sgn(d) == 0) {
            p1 = a;
            p2 = a;
            return 1;
        }
        p1 = a + (v.e - v.s).trunc(d);
        p2 = a - (v.e - v.s).trunc(d);
        return 2;
    }
    double areatriangle(Point a,Point b)
    {
        if( sgn( (p - a) ^ (p - b)) == 0) return 0.0;
        Point q[5];
        int len = 0;
        q[len++] = a;
        Line l(a,b);
        Point p1,p2;
        if(pointcrossline(l,q[1],q[2]) == 2) {
            if(sgn((a - q[1]) * (b - q[1])) < 0) q[len++] = q[1];
            if(sgn((a - q[2]) * (b - q[2])) < 0) q[len++] = q[2];
        }
        q[len++] = b;
        if(len == 4 && sgn((q[0] - q[1]) * (q[2] - q[1])) > 0) swap(q[1],q[2]);
        double res = 0;
        for(int i = 0; i < len - 1; i++) {
            if(relation(q[i]) == 0 || relation(q[i + 1]) == 0) {
                double arg = p.rad(q[i],q[i + 1]);
                res += r * r * arg / 2.0;
            }
            else {
                res += fabs((q[i] - p) ^ (q[i + 1] - p)) / 2.0;
            }
        }
        return res;
    }
};
struct polygon
{
    int n;
    Point p[maxp];
    void input(int _n)
    {
        n = _n;
        for(int i = 0; i < n; i++) {
            p[i].input();
        }
    }
    double areacircle(circle c)
    {
        double ans = 0;
        for(int i = 0; i < n; i++) {
            int j = (i + 1) % n;
            if(sgn ( (p[j] - c.p ) ^ (p[i] - c.p ) ) >= 0)
                ans += c.areatriangle(p[i],p[j]);
            else ans -= c.areatriangle(p[i],p[j]);
        }
        return fabs(ans);
    }
};
int main(void)
{
    int n;
    double x0,y0,v0,angle,t,g,R,x1,y1;
    double sd,cd,sv,cv;
    while(cin >> x0 >> y0 >> v0 >> angle >> t >> g >> R) {
        if(x0 + y0 + v0 + angle + t + g + R == 0) break;
        angle = (angle * 2.0 * pi ) / 360.0;
        sv = v0 * cos(angle);
        cv = v0 * sin(angle);
        sd = sv * t;
        cd = cv * t - 0.5 * g * t * t;
        x1 = x0 + sd;
        y1 = y0 + cd;
        circle c(x1,y1,R);
        polygon pl;
        scanf("%d",&n);
        pl.input(n);
        printf("%.2lf\n",pl.areacircle(c));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/82806508
今日推荐