I almost gave up during the contest for this ... but I noticed that there are some important points that led to my AC here:
-> The points can be in clockwise or counter-clockwise order.
-> There might be > 180 angles.
-> I don't care whether or not we have 3 consecutive points being colinear, I treated the angle as 180 degree angle instead of ignoring it.
-> The polygon is not necessarily convex.
-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
It's possible, that input looks like:
4 0 0 1 0 1 1000 1 0
??
My program fails only on such case I think ....
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
# include <stdio.h>
# include <math.h>
# define MaxN 200
# define PI 3.14159265359
struct Tpoint
{ long x; long y;}
Polygon[MaxN];
typedef struct Tpoint line;
typedef struct Tpoint point;
int N;
int Clockwise;
double LineDis(line a)
{
return(sqrt(a.x*a.x+a.y*a.y));
}
long Cross_Product(line a,line b)
{
return(a.x*b.y-b.x*a.y);
}
double arg(line a,line b)
{
if (Cross_Product(a,b)*Clockwise<0)
return(
PI-acos((a.x*b.x+a.y*b.y)/LineDis(a)/LineDis(b))
);
else
return(
PI+acos((a.x*b.x+a.y*b.y)/LineDis(a)/LineDis(b))
);
}
int Inline(point a,point b,point c)
{
line t1,t2;
t1.x=a.x-b.x;
t1.y=a.y-b.y;
t2.x=b.x-c.x;
t2.y=b.y-c.y;
return(Cross_Product(t1,t2)==0);
}
long Area(void)
{
int i;
long s=0;
for (i=0;i<N;i++)
s+=Cross_Product(Polygon[i],Polygon[(i+1)%N]);
return(s/2);
}
void ReadInput(void)
{
int i;
for (i=0;i<N;i++)
scanf("%ld %ld",&Polygon[i].x,&Polygon[i].y);
}
main()
{
int i;
double MaxArg,MinArg;
double CurArg;
line t1,t2;
while (scanf("%d",&N),N>=3)
{
ReadInput();
Clockwise=(Area()<0);
if (Clockwise==0) Clockwise=-1;
MaxArg=0;
MinArg=360;
for (i=0;i<N;i++)
if (!Inline(Polygon[i],Polygon[(i+1)%N],Polygon[(i+N-1)%N]))
{
t1.x=Polygon[i].x-Polygon[(i+N-1)%N].x;
t1.y=Polygon[i].y-Polygon[(i+N-1)%N].y;
t2.x=Polygon[(i+1)%N].x-Polygon[i].x;
t2.y=Polygon[(i+1)%N].y-Polygon[i].y;
CurArg=arg(t1,t2)/PI*180;
if (MaxArg<CurArg)
MaxArg=CurArg;
if (MinArg>CurArg)
MinArg=CurArg;
}
printf("%0.6f %0.6f\n",MinArg,MaxArg);
}
}
i think it will help u . try to assign pi=acos(-1) or 2*acos(0). in this problem points may be given in clockwise or anticlockwise order. so u have to handle this case.