Why wrong answer?
Послано
Sega 8 авг 2003 15:17
I summarise all corners of this polygon and see: if a>0(360)->ccw;
else (a=-360)->cw; Why is it wrong?
This is my program:
#include<iostream.h>
#include<math.h>
double len(double x1,double y1,double x2,double y2)
{return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
double gon(double x1,double y1,double x2,double y2,double x3,double
y3)
{return asin(((x2-x1)*(y3-y2)-(x3-x2)*(y2-y1))/(len(x1,y1,x2,y2)*len
(x2,y2,x3,y3)));}
int main()
{
double a=0,x0,y0,x1,y1,xl,yl,xr,yr,xn,yn;
long n,i; cin >> n;
cin >> x0 >> y0 >> x1 >> y1;
xl=x0; yl=y0; xr=x1; yr=y1;
for (i=3; i<=n; i++)
{
cin >> xn >> yn;
a+=gon(xl,yl,xr,yr,xn,yn);
xl=xr; yl=yr; xr=xn; yr=yn;
}
a+=gon(xl,yl,xr,yr,x0,y0);
a+=gon(xr,yr,x0,y0,x1,y1);
if (a<0) cout << "cw"; else cout << "ccw";
return 0;
}