I keep getting WA... Is it possible for an admin to post some test cases? I'm losing my mind here
My idea:
I move the polygon sides 'd' centimeters through the inside, and then compute the intersections, being these the new vertices. Then, sum all sub-triangle's areas.
[cpp]
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef pair<double, double> punto;
typedef pair<punto, punto> segmento;
segmento corre(punto p, punto q, double d){
punto w = make_pair(q.first - p.first, q.second - p.second);
punto v = make_pair(w.second, -w.first);
double l = v.first * v.first + v.second * v.second;
l = sqrt(l);
v.first = (v.first * d) / l;
v.second = (v.second * d) / l;
p.first += v.first; p.second += v.second;
q.first += v.first; q.second += v.second;
return make_pair(p,q);
}
punto interseccion(segmento a, segmento b){
// segmento a: puntos P y Q. segmento b: puntos R y S. interseccion, T
// NO SON PARALELOS SINO SE PUDRE MALA ONDA EH
double yqp, xsr, xpr, xqp, yrp, ysr,xr,yr,xp,yp;
yqp = a.second.second - a.first.second;
xsr = b.second.first - b.first.first;
xpr = a.first.first - b.first.first;
xqp = a.second.first - a.first.first;
yrp = b.first.second - a.first.second;
ysr = b.second.second - b.first.second;
xp = a.first.first;
yp = a.first.second;
xr = b.first.first;
yr = b.first.second;
double xt;
double yt;
xt = (yqp * xsr * xp - ysr * xqp * xr + xqp * xsr * yrp) / (yqp * xsr - ysr * xqp);
if(abs(xqp)<abs(xsr)){
yt = (xt - xr) * ysr / xsr + yr;
} else {
yt = (xt - xp) * yqp / xqp + yp;
}
return make_pair(xt, yt);
}
double area(punto q, punto p1, punto p2) {
p1.first -= q.first;
p1.second -= q.second;
p2.first -= q.first;
p2.second -= q.second;
return (p1.first*p2.second)-(p2.first*p1.second);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","rt",stdin);
#endif
double d;
long n;
cin>>d>>n;
while((d!=0)||(n!=0)){
vector <punto> p;
vector <segmento> m;
if(n<3){
cout<<"0.000"<<endl;
double x,y;
for(long i=0; i<n; i++) cin>>x>>y;
} else {
for(long i=0; i<n; i++){
double x,y;
cin>>x>>y;
p.push_back(make_pair(x,y));
}
for(long i=0; i<n; i++){
m.push_back(corre(p
, p[(i+1)%n], d));
}
for(long i=0; i<n; i++){
p = interseccion(m, m[(i+1)%n]);
}
double a = 0;
for(long i=0; i<n; i++){
a+= (area(p[1], p[0], p));
}
a /= 2;
printf("%.3lf\n",a);
}
cin>>d>>n;
}
return 0;
}
[/cpp]