38 void heapsort(
int *sid,
int *
id,
const double*
const x,
int N )
40 unsigned int n = N, i = n / 2, parent, child;
60 if ( child + 1 < n && x[
id[sid[child + 1]]] > x[
id[sid[child]]] )
64 if ( x[
id[sid[child]]] > x[
id[tx]] )
66 sid[parent] = sid[child];
68 child = parent * 2 + 1;
82 unsigned int n = N, i = n / 2, parent, child;
106 if ( child + 1 < n && heap[child + 1] > heap[child] )
110 if ( heap[child] > t )
112 heap[parent] = heap[child];
113 x[parent] = x[child];
115 child = parent * 2 + 1;
135 double x3,
double y3,
double x4,
double y4 )
144 return (
cross_product( x1, y1, x2, y2, x3, y3 ) *
cross_product( x1, y1, x2, y2, x4, y4 ) < 0
145 &&
cross_product( x3, y3, x4, y4, x1, y1 ) *
cross_product( x3, y3, x4, y4, x2, y2 ) < 0 );
153 double x3,
double y3,
double x4,
double y4,
154 double *x,
double *y )
157 double a1, a2, b1, b2, c1, c2;
162 c1 = x2 * y1 - x1 * y2;
166 c2 = x4 * y3 - x3 * y4;
168 if (( denom = a1 * b2 - a2 * b1 ) == 0 )
174 *x = ( b1 * c2 - b2 * c1 ) / denom;
175 *y = ( a2 * c1 - a1 * c2 ) / denom;
193 int convexHullId(
int *
id,
const double*
const x,
const double*
const y,
int n,
int *&cHull )
198 for ( i = 0; i < n; i++ )
204 if ( n <= 3 )
return n;
206 int* stack =
new int[n];
207 double* tan =
new double [n];
218 while ( ref < n &&
qgsDoubleNear( y[
id[cHull[ref]]], y[
id[cHull[0]]] ) ) ref++;
223 for ( i = ref; i < n; i++ )
228 tan[i] = ( x[
id[cHull[0]]] - x[
id[cHull[i]]] ) / ( y[
id[cHull[i]]] - y[
id[cHull[0]]] );
232 heapsort2( cHull + ref, tan + ref, n - ref );
242 stack[1] = cHull[ref-1];
248 for ( i = ref; i < n; i++ )
250 result =
cross_product( x[
id[stack[second]]], y[
id[stack[second]]],
251 x[
id[stack[top]]], y[
id[stack[top]]], x[
id[cHull[i]]], y[
id[cHull[i]]] );
255 if (
dist_euc2d_sq( x[
id[stack[second]]], y[
id[stack[second]]], x[
id[cHull[i]]], y[
id[cHull[i]]] )
256 >
dist_euc2d_sq( x[
id[stack[second]]], y[
id[stack[second]]], x[
id[stack[top]]], y[
id[stack[top]]] ) )
258 stack[top] = cHull[i];
261 else if ( result > 0 )
265 stack[top] = cHull[i];
269 while ( result < 0 && top > 1 )
274 y[
id[stack[second]]], x[
id[stack[top]]],
275 y[
id[stack[top]]], x[
id[cHull[i]]], y[
id[cHull[i]]] );
279 stack[top] = cHull[i];
283 for ( i = 0; i <= top; i++ )
302 int *pts =
new int[nbPoints];
303 for ( i = 0; i < nbPoints; i++ )
309 if ( pts[cHull[0]] < pts[cHull[1]] && pts[cHull[1]] < pts[cHull[2]] )
311 else if ( pts[cHull[0]] > pts[cHull[1]] && pts[cHull[1]] > pts[cHull[2]] )
313 else if ( pts[cHull[0]] > pts[cHull[1]] && pts[cHull[1]] < pts[cHull[2]] && pts[cHull[2]] < pts[cHull[0]] )
315 else if ( pts[cHull[0]] > pts[cHull[1]] && pts[cHull[1]] < pts[cHull[2]] && pts[cHull[2]] > pts[cHull[0]] )
317 else if ( pts[cHull[0]] < pts[cHull[1]] && pts[cHull[1]] > pts[cHull[2]] && pts[cHull[2]] > pts[cHull[0]] )
319 else if ( pts[cHull[0]] < pts[cHull[1]] && pts[cHull[1]] > pts[cHull[2]] && pts[cHull[2]] < pts[cHull[0]] )
323 std::cout <<
"Warning wrong cHull -> geometry: " << nbPoints << std::endl;
324 for ( i = 0; i < nbPoints; i++ )
326 std::cout << x[i] <<
";" << y[i] << std::endl;
328 std::cout <<
"hull : " << cHullSize << std::endl;
329 for ( i = 0; i < cHullSize; i++ )
331 std::cout << pts[cHull[i]] <<
" ";
333 std::cout << std::endl;
343 for ( i = 0, j = nbPoints - 1; i <= j; i++, j-- )
364 double x1,
double y1,
double x2,
double y2,
365 double& xRes,
double& yRes )
370 double A = dx * dx + dy * dy;
371 double B = 2 * ( dx * ( x1 - cx ) + dy * ( y1 - cy ) );
372 double C = ( x1 - cx ) * ( x1 - cx ) + ( y1 - cy ) * ( y1 - cy ) - radius * radius;
374 double det = B * B - 4 * A * C;
375 if ( A <= 0.0000001 || det < 0 )
382 double t = -B / ( 2 * A );
391 double t = ( -B + sqrt( det ) ) / ( 2 * A );
int reorderPolygon(int nbPoints, double *x, double *y)
bool computeLineIntersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double *x, double *y)
void heapsort(int *sid, int *id, const double *const x, int N)
double dist_euc2d_sq(double x1, double y1, double x2, double y2)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
void heapsort2(int *x, double *heap, int N)
double cross_product(double x1, double y1, double x2, double y2, double x3, double y3)
int convexHullId(int *id, const double *const x, const double *const y, int n, int *&cHull)
bool isSegIntersects(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
void findLineCircleIntersection(double cx, double cy, double radius, double x1, double y1, double x2, double y2, double &xRes, double &yRes)