37#define _USE_MATH_DEFINES
57 double epsilon = std::numeric_limits<double>::epsilon();
61 COLA_ASSERT(
vecDir(a, b,
c, epsilon) == 0);
63 if (fabs(a.
x - b.
x) > epsilon)
66 return (((a.
x <
c.x) && (
c.x < b.
x)) ||
67 ((b.
x <
c.x) && (
c.x < a.
x)));
71 return (((a.
y <
c.y) && (
c.y < b.
y)) ||
72 ((b.
y <
c.y) && (
c.y < a.
y)));
80 const double tolerance)
98 return (
vecDir(a, b,
c, tolerance) == 0);
106 const double tolerance)
111 return (a.
x ==
c.x) &&
112 (((a.
y <
c.y) && (
c.y < b.
y)) ||
113 ((b.
y <
c.y) && (
c.y < a.
y)));
117 return (a.
y ==
c.y) &&
118 (((a.
x <
c.x) && (
c.x < b.
x)) ||
119 ((b.
x <
c.x) && (
c.x < a.
x)));
141 int ab_d =
vecDir(a, b, d);
159 return (((ab_c * ab_d) < 0) && ((cd_a * cd_b) < 0));
167 const Point& s2,
bool& seenIntersectionAtEndpoint)
174 else if ( (((s2 == e1) ||
pointOnLine(s1, s2, e1)) &&
175 (
vecDir(s1, s2, e2) != 0))
178 (
vecDir(s1, s2, e1) != 0)) )
184 if (seenIntersectionAtEndpoint)
188 seenIntersectionAtEndpoint =
true;
205 int rSide =
vecDir(b, a0, a1);
206 int sSide =
vecDir(b, a1, a2);
208 bool rOutOn = (rSide <= 0);
209 bool sOutOn = (sSide <= 0);
211 bool rOut = (rSide < 0);
212 bool sOut = (sSide < 0);
214 if (
vecDir(a0, a1, a2) > 0)
229 return (rOutOn && !sOut) || (!rOut && sOutOn);
231 return (rOutOn || sOutOn);
246 return (IgnoreRegions ?
false : (rOutOn && sOutOn));
264 int s123 =
vecDir(c1, c2, c3);
265 int s12p =
vecDir(c1, c2, p);
266 int s23p =
vecDir(c2, c3, p);
270 if ((s12p >= 0) && (s23p >= 0))
278 if ((s12p <= 0) && (s23p <= 0))
294 double xdiff = a.
x - b.
x;
295 double ydiff = a.
y - b.
y;
297 return sqrt((xdiff * xdiff) + (ydiff * ydiff));
304 return fabs(a.
x - b.
x) + fabs(a.
y - b.
y);
312 double xdiff = a.
x - b.
x;
313 double ydiff = a.
y - b.
y;
315 return sqrt((xdiff * xdiff) + (ydiff * ydiff));
322 for (
size_t i = 1; i < poly.
size(); ++i)
324 l +=
dist(poly.
ps[i-1], poly.
ps[i]);
332 double ux = b.
x - a.
x,
336 lu = sqrt(ux*ux+uy*uy),
337 lv = sqrt(vx*vx+vy*vy),
338 udotv = ux * vx + uy * vy,
339 costheta = udotv / (lu * lv);
340 return acos(costheta);
351 size_t n = poly.
size();
352 const std::vector<Point>& P = poly.
ps;
353 bool onBorder =
false;
354 for (
size_t i = 0; i < n; i++)
357 size_t prev = (i + n - 1) % n;
358 int dir =
vecDir(P[prev], P[i], q);
365 onBorder |= (dir == 0);
367 if (!countBorder && onBorder)
388 std::vector<Point>& P = poly.
ps;
389 size_t n = poly.
size();
392 for (
size_t i = 0; i < n; ++i)
394 P[i].x = P[i].x - q.
x;
395 P[i].y = P[i].y - q.
y;
399 for (
size_t i = 0; i < n; ++i)
402 if ((P[i].x == 0) && (P[i].y == 0))
409 size_t i1 = ( i + n - 1 ) % n;
417 if ((P[i].y > 0) != (P[i1].y > 0))
420 double x = (P[i].x * P[i1].y - P[i1].x * P[i].y)
421 / (P[i1].y - P[i].y);
434 if ((P[i].y < 0) != (P[i1].y < 0))
437 double x = (P[i].x * P[i1].y - P[i1].x * P[i].y)
438 / (P[i1].y - P[i].y);
449 if ( (Rcross % 2) != (Lcross % 2) )
456 if ((Rcross % 2) == 1)
473#define SAME_SIGNS( a, b ) \
474 (((long) ((unsigned long) a ^ (unsigned long) b)) >= 0 )
477 const Point& b1,
const Point& b2,
double *x,
double *y)
479 double Ax,Bx,Cx,Ay,By,Cy,d,e,f,
num;
480 double x1lo,x1hi,y1lo,y1hi;
563 *x = a1.
x + (
num) / f;
567 *y = a1.
y + (
num) / f;
577 const Point& b1,
const Point& b2,
double *x,
double *y)
579 double Ax,Bx,Cx,Ay,By,Cy,d,f,
num;
600 *x = a1.
x + (
num) / f;
604 *y = a1.
y + (
num) / f;
615 return ((p.
x < 0) ? 180 : 0);
619 return ((p.
y < 0) ? 270 : 90);
622 double ang = atan(p.
y / p.
x);
633 COLA_ASSERT(
ang >= 0);
634 COLA_ASSERT(
ang <= 360);
The Point class defines a point in the plane.
A common interface used by the Polygon classes.
A dynamic Polygon, to which points can be easily added and removed.
size_t size(void) const
Returns the number of points in this polygon.
std::vector< Point > ps
A vector of the points that make up the Polygon.
libavoid: Object-avoiding orthogonal and polyline connector routing library.
double manhattanDist(const Point &a, const Point &b)
int segmentIntersectPoint(const Point &a1, const Point &a2, const Point &b1, const Point &b2, double *x, double *y)
bool inPoly(const Polygon &poly, const Point &q, bool countBorder)
bool inBetween(const Point &a, const Point &b, const Point &c)
static const int DO_INTERSECT
static const int DONT_INTERSECT
bool colinear(const Point &a, const Point &b, const Point &c, const double tolerance)
double angle(const Point &a, const Point &b, const Point &c)
static const int PARALLEL
bool segmentShapeIntersect(const Point &e1, const Point &e2, const Point &s1, const Point &s2, bool &seenIntersectionAtEndpoint)
double euclideanDist(const Point &a, const Point &b)
int rayIntersectPoint(const Point &a1, const Point &a2, const Point &b1, const Point &b2, double *x, double *y)
static int vecDir(const Point &a, const Point &b, const Point &c, const double maybeZero=0.0)
bool inValidRegion(bool IgnoreRegions, const Point &a0, const Point &a1, const Point &a2, const Point &b)
double rotationalAngle(const Point &p)
bool pointOnLine(const Point &a, const Point &b, const Point &c, const double tolerance)
int cornerSide(const Point &c1, const Point &c2, const Point &c3, const Point &p)
double totalLength(const Polygon &poly)
bool inPolyGen(const PolygonInterface &argpoly, const Point &q)
double dist(const Point &a, const Point &b)
bool segmentIntersect(const Point &a, const Point &b, const Point &c, const Point &d)
double ang(Point n1, Point n2)