凸包多边形,包括多边形边缘上的点

时间:2020-07-05 21:31:02

标签: c++ c++11 convex-hull grahams-scan

我正在尝试实现Graham Scan方法来实现Convex Hull算法,但是我想在Hull上添加位于多边形边缘的点集(与多边形的相邻顶点共线的点) ) B must be included between A and C , I must be included between J and H ....

我认为我必须在排序功能中更改比较器功能(根据逆时针极角对点进行排序)。但是我做不到,这是我必须改变的:-

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; 
} 

0 个答案:

没有答案