我正在尝试实现Graham Scan方法来实现Convex Hull算法,但是我想在Hull上添加位于多边形边缘的点集(与多边形的相邻顶点共线的点) )
我认为我必须在排序功能中更改比较器功能(根据逆时针极角对点进行排序)。但是我做不到,这是我必须改变的:-
int orientation(Point p, Point q, Point r)
{
int val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // colinear
return (val > 0)? 1: 2; // clock or counterclock wise
}
// A function used by library function qsort() to sort an array of
// points with respect to the first point
// p0 is the first point
int compare(const void *vp1, const void *vp2)
{
Point *p1 = (Point *)vp1;
Point *p2 = (Point *)vp2;
// Find orientation
int o = orientation(p0, *p1, *p2);
if (o == 0)
return (distSq(p0, *p2) >= distSq(p0, *p1))? -1 : 1;
return (o == 2)? -1: 1;
}