Cod sursa(job #1412436)

Utilizator danalex97Dan H Alexandru danalex97 Data 1 aprilie 2015 12:02:37
Problema Camera Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <fstream>
#include <iomanip>

using namespace std;

#define x first
#define y second

const int Nmax = 10010;
const int oo = 100010;
const int EPS = 0.000001

ifstream F("camera.in");
ofstream G("camera.out");

typedef pair<double,double> Pair;

Pair A[Nmax],B[Nmax],C[Nmax];

double Sol;
int Semn,N,M,P;

double Aria(Pair A[],int N)
{
	double Sum=0;
	for (int i=1;i<=N;++i)
		Sum+=A[i].x*A[i+1].y-A[i+1].x*A[i].y;
	return Sum/2;
}

int Sign(Pair A, Pair B, Pair C)
{
	double a = A.y - B.y;
	double b = B.x - A.x;
	double c = - B.x * A.y + B.y * A.x;
	
	if ( a*C.x+b*C.y+c > -EPS ) return 1;
	if ( a*C.x+b*C.y+c < EPS ) return -1;
	
	return 0;
}

Pair Int(Pair A, Pair B, Pair C, Pair D)
{
	double a = A.y - B.y;
	double b = B.x - A.x;
	double c = - B.x * A.y + B.y * A.x;
	
	double a2 = C.y - D.y;
	double b2 = D.x - C.x;
	double c2 = - D.x * C.y + D.y * C.x;
	
	double X = ( a*b2 - a2*b != 0 ) ? ( -c*b2 + c2*b ) / ( a*b2 - a2*b ) : -oo ;
	double Y = ( X == -oo || b == 0 ) ? -oo : (-c-ax) / b ;
	
	return make_pair(X,Y);
}

int main()
{
	F>>N; 
	for (int i=1;i<=N;++i) 
		F>>A[i];
	A[N+1]=A[1];
	
	Semn=( Aria(A,N)>0 ) ? +1 : -1;

	M=4;
	B[1]=make_pair(-oo,+oo);
	B[2]=make_pair(+oo,+oo);
	B[3]=make_pair(+oo,-oo);
	B[4]=make_pair(-oo,-oo);
	B[5]=make_pair(-oo,+oo);
	
	for (int i=1;i<=N;++i)
	{
		int Act,Next;
		
		P=0;
		if ( M<3 )
		{
			G<<0<<'\n';
			return 0;
		}
		
		for (Act=1;Act<=N;++Act)
		{
			Next=Act+1;
			int S1 = Sign(A[i],A[i+1],B[Act]);
			int S2 = Sign(A[i],A[i+1],B[Next]);
			
			if (S1 != -Semn && S2 != -Semn) C[++P] = B[next];
			if (S1 != -Semn && S2 == -Semn) C[++P] = Int(A[i], A[i+1], B[cur], B[next]);
			if (S1 == -Semn && S2 != -Semn) C[++P] = Int(A[i], A[i+1], B[cur], B[next]), C[++P] = B[next];
			
			if ( C[P-1].x == -oo || C[P-1].x == -oo ) swap(C[P-1],C[P]),--P;
			if ( C[P].x == -oo || C[P].x == -oo ) --P;
		}
		
		M=P;
		for (int i=1;i<=M;++i) B[i]=C[i];
	}
	
	Sol = Aria(B,M) ;
	G<< fixed << setprecision(2) << Sol<<'\n';
}