计算几何 判断多边形顶点是否是顺时针

1.凸包的时候,只要判断前三个点即可,计算叉积,判断方向

2.凹包情况就复杂了,可以从三个方面考虑

首先,可以去凸包上的特殊点,x最大最小的点,y最大最小的点,这些极值点肯定是在凸包上的,可以计算这些的叉积,

其次,直接统计叉积正负的数量,正多负少,是逆时针,反之,顺时针,

一个简单的做法是,计算面积,用面积的正负判断方向


http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=10060

A tethered dog
Time Limit: 2000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 6, Accepted users: 4
Problem 10060 : No special judgement
Problem description
  A dog is tethered to a pole with a rope. The pole is located inside a fenced polygon (not necessarily convex) with nonzero area. The fence has no self-crosses. The Olympian runs along the fence bypassing the vertices of the polygon in a certain order which is not broken during the jog. A dog pursues him inside the fenced territory and barks. Your program is to determine how (clockwise or counter-clockwise) the rope will wind after several rounds of the Olympian's jog. 
Input
  The first input line contains a number N, which is the number of the polygon vertices. (it is known that 3 <= N <= 200000). The next N lines consist of the vertices plane coordinates, given in an order of Olympian's jog. The coordinates are a pair of real numbers separated with a space. The coordinates may be given in an exponential form. 
Output
  You are to output "cw", if the rope is winded in a clockwise order and "ccw" otherwise. 
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
cw

题意是给出简单多边形的n个点坐标,判断是否顺时针方向

#include 
#include 
#include 
using namespace std;

const int maxn = 200050;
const double eps = 1e-8;

struct Point{
    double x, y;
};
double cross(Point a,Point b,Point c) ///叉积{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
//计算多边形面积
double PolygonArea(Point p[], int n)
{
    if(n < 3) return 0.0;
    double s = p[0].y * (p[n - 1].x - p[1].x);
    p[n] = p[0];
    for(int i = 1; i < n; ++ i)
        s += p[i].y * (p[i - 1].x - p[i + 1].x);
    return s * 0.5;
}
Point p1[maxn];
int n1;
int main()
{
    while(scanf("%d",&n1) != EOF ){
        for(int i = 0; i < n1; i++)
            scanf("%lf%lf", &p1[i].x, &p1[i].y);
        if ( PolygonArea(p1,n1) > 0 )
            puts("ccw");
        else
            puts("cw");
    }
    return 0;
}


你可能感兴趣的:(算法模板,ACM)