150 const bool getsigned =
false)
const;
162template <
class T,
class U>
166template <
class T,
class U>
170template <
class T,
class U>
174template <
class T,
class U>
190 const bool getsigned =
false);
226{ i += p.
i; j += p.
j;
return *
this; }
231{ i -= p.
i; j -= p.
j;
return *
this; }
236{ i *= p.
i; j *= p.
j;
return *
this; }
241{ i /= p.
i; j /= p.
j;
return *
this; }
274{ i += val; j += val;
return *
this; }
279{ i -= val; j -= val;
return *
this; }
284{ i *= val; j *= val;
return *
this; }
289{ i /= val; j /= val;
return *
this; }
320template <
class T,
class U>
322{
return p1.
i == p2.
i && p1.
j == p2.
j; }
325template <
class T,
class U>
327{
return p1.
i != p2.
i || p1.
j != p2.
j; }
330template <
class T,
class U>
332{
return p1.
i > p2.
i && p1.
j > p2.
j; }
335template <
class T,
class U>
337{
return p1.
i < p2.
i && p1.
j < p2.
j; }
342{
return ((i >= 0) && (j >= 0)); }
350 TF d1 = p.
i - i, d2 = p.
j - j;
351 return (d1 * d1 + d2 * d2);
358{
return sqrt(squdist(p)); }
364{
return sqrt((i*i) + (j*j)); }
370 const bool getsigned)
const
374 TF a = q.j - p.
j, b = q.i - p.
i, c = q.i * p.
j - q.j * p.
i;
375 if (getsigned)
return (a*i-b*j+c)/sqrt(a*a + b*b);
376 else return fabs(a*i-b*j+c)/sqrt(a*a + b*b);
385 Point2D<T> V0 = q-p, V1 = (*this)-q, V2 = (*this)-p;
386 if (V0.
i*V1.i + V0.
j*V1.j>0)
return distance(q);
387 if (V0.
i*V2.i + V0.
j*V2.j<0)
return distance(p);
388 return distanceToLine(p,q);
398 bool c =
false;
const uint ps = polygon.size();
399 for (
uint i = 0, j = ps-1; i < ps; j = i++)
400 if ((((polygon[i].j <= p.
j) && (p.
j < polygon[j].j)) ||
401 ((polygon[j].j <= p.
j) && (p.
j < polygon[i].j))) &&
402 (p.
i < (polygon[j].i - polygon[i].i) *
403 (p.
j - polygon[i].j) / (polygon[j].j - polygon[i].j)
415 const uint ps = polygon.size();
416 for (
uint i = 0; i < ps; i++) CM += polygon[i];
425 const uint ps = polygon.size();
426 for (
uint i = 0, j = ps-1; i < ps; j = i++)
427 area += polygon[i].i*polygon[j].j - polygon[i].j*polygon[j].i;
428 if (getsigned)
return (0.5 *
area);
429 else return fabs(0.5 *
area);
438 const double eps = 0.00001;
441 if (Sab * Sac * Sbc == 0 || fabs(Sab - Sac + Sbc) < eps
442 || fabs(- Sab + Sac + Sbc) < eps)
return 0;
443 else if(fabs(Sab + Sac - Sbc) < eps)
return 3.1415;
445 int sgn = (A.
i*(B.
j-C.
j) + B.
i*(C.
j-A.
j) * C.
i*(A.
j-B.
j)) < 0 ? 1 : -1;
446 return sgn * acos((Sac*Sac+Sab*Sab-Sbc*Sbc)/(2*Sab*Sac));
458 T ax = C.
i - B.
i; T ay = C.
j - B.
j;
459 T bx = A.
i - C.
i; T by = A.
j - C.
j;
460 T cx = B.
i - A.
i; T cy = B.
j - A.
j;
461 T apx= P.
i - A.
i; T apy= P.
j - A.
j;
462 T bpx= P.
i - B.
i; T bpy= P.
j - B.
j;
463 T cpx= P.
i - C.
i; T cpy= P.
j - C.
j;
465 T aCROSSbp = ax*bpy - ay*bpx;
466 T cCROSSap = cx*apy - cy*apx;
467 T bCROSScp = bx*cpy - by*cpx;
469 return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
bool operator!=(const Point2D< T > &p1, const Point2D< U > &p2)
!= operator
bool pnpoly(const std::vector< Point2D< T > > &polygon, const Point2D< T > &p)
Is a point inside a polygon?
double angleMeasure(const Point2D< T > &A, const Point2D< T > &B, const Point2D< T > &C)
What is the measure (in radians, signed -pi to pi) of angle BAC?
bool operator<(const Point2D< T > &p1, const Point2D< U > &p2)
< operator
bool pnTriangle(const Point2D< T > &A, const Point2D< T > &B, const Point2D< T > &C, const Point2D< T > &P)
determine if a point P is inside a triangle with vertices A,B,C
double area(const std::vector< Point2D< T > > &polygon, const bool getsigned=false)
What is the area of a polygon?
bool operator>(const Point2D< T > &p1, const Point2D< U > &p2)
‍operator
Point2D< T > centroid(const std::vector< Point2D< T > > &polygon)
Where is the center of mass of a polygon?
bool operator==(const Point2D< T > &p1, const Point2D< U > &p2)
== operator
unsigned int uint
Canonical unsigned int.
This is a basic class to encode 2D integer coordinates.
Point2D< T > & operator/=(const Point2D< T > &p)
/= operator
promote_trait< T, float >::TP squdist(const Point2D< T > &p) const
the square of the euclidean distance
Point2D< typename promote_trait< T, U >::TP > operator-(const Point2D< U > &p) const
promote_trait< T, float >::TP magnitude() const
the Magnitude
Point2D< typename promote_trait< T, U >::TP > operator/(const Point2D< U > &p) const
/ operator
Point2D< typename promote_trait< T, U >::TP > operator+(const Point2D< U > &p) const
Point2D< T > & operator/=(const T val)
/= operator
Point2D< typename promote_trait< T, U >::TP > operator+(const U val) const
Point2D< T > & operator-=(const T val)
-= operator
promote_trait< T, float >::TP distance(const Point2D< T > &p) const
the euclidean distance from p
Point2D< T > & operator-=(const Point2D< T > &p)
-= operator
Point2D< typename promote_trait< T, U >::TP > operator*(const Point2D< U > &p) const
promote_trait< T, float >::TP distanceToSegment(const Point2D< T > &p, const Point2D< T > &q) const
the euclidean distance from segment pq
Point2D(const Point2D< U > &a)
Explicit conversion from type T to another type U.
Point2D< T > & operator+=(const T val)
+= operator
Point2D< T > & operator*=(const T val)
*= operator
bool isValid() const
test whether i and j are both positive
Point2D< typename promote_trait< T, U >::TP > operator/(const U val) const
/ operator
promote_trait< T, float >::TP distanceToLine(const Point2D< T > &p, const Point2D< T > &q, const bool getsigned=false) const
the euclidean distance from line pq
Point2D< typename promote_trait< T, U >::TP > operator*(const U val) const
Point2D< typename promote_trait< T, U >::TP > operator-(const U val) const
Point2D(const T ii, const T jj)
Initialize the Point2D from horizontal & vertical coordinates.
Point2D()
The default constructor initializes the coordinates to (0,0)
Point2D< T > & operator+=(const Point2D< T > &p)
+= operator
Point2D< T > & operator*=(const Point2D< T > &p)
*= operator