9 #ifndef MsiChart_Point2D_h 10 #define MsiChart_Point2D_h 12 #include "PDCHeader/PDCvector" 13 #include "PDCHeader/PDCcmath" 16 #pragma warning(disable : 4244) 17 #pragma warning(disable : 4305) 25 template <
typename T =
double>
34 Point(T iX = T(), T iY = T()):
x(iX),
y(iY){}
52 void Reset(T iX, T iY);
182 x =
static_cast<T
>(iPoint.
x);
183 y =
static_cast<T
>(iPoint.
y);
196 return (x - irPoint.
x) * (x - irPoint.
x)
197 + (y - irPoint.
y) * (y - irPoint.
y);
203 return std::sqrt(DistanceSquared(irPoint));
209 return ((x != irPoint.
x) || (y != irPoint.
y));
215 return ((x == irPoint.
x) && (y == irPoint.
y));
228 unsigned Int32 lNumberOfEdges = (
Int32)irPolygon.size();
229 if (lNumberOfEdges < 3)
233 bool lResult =
false;
234 for (
unsigned Int32 i = 0, j = lNumberOfEdges - 1; i < lNumberOfEdges; j = i++)
236 if (((irPolygon[i].y <= y && y < irPolygon[j].y) ||
237 (irPolygon[j].y <= y && y < irPolygon[i].y)) &&
238 x <= (irPolygon[j].x - irPolygon[i].x) * (y - irPolygon[i].y) * 1.0 / (irPolygon[j].y - irPolygon[i].y) + irPolygon[i].x)
249 if (!
Point<T>(x + 0.1f, y).IsInPolygon(irPolygon))
251 if (!
Point<T>(x - 0.1f, y).IsInPolygon(irPolygon))
253 if (!
Point<T>(x, y + 0.1f).IsInPolygon(irPolygon))
255 if (!
Point<T>(x, y - 0.1f).IsInPolygon(irPolygon))
263 double lXDist = x - irAnchor.
x;
264 double lYDist = y - irAnchor.
y;
266 x =
static_cast<T
>(irAnchor.
x + cos(iRadian) * lXDist - sin(iRadian) * lYDist);
267 y =
static_cast<T
>(irAnchor.
y + sin(iRadian) * lXDist + cos(iRadian) * lYDist);
273 Rotate(irAnchor, iRadian);
287 return Point<T>((x + irPoint.
x) / 2, (y + irPoint.
y) / 2);
293 return Point<T>(x + irOffset.
x, y + irOffset.
y);
305 std::swap(x, iorPoint.
x);
306 std::swap(y, iorPoint.
y);
312 const float lTolerance = iExclude ? 0.01f : -0.01f;
313 if(fabs(iSegmentStart.
x - iSegmentEnd.
x) < 0.01f)
315 if((y - iSegmentStart.
y) > lTolerance && (iSegmentEnd.
y - y) > lTolerance)
317 if((y - iSegmentEnd.
y) > lTolerance && (iSegmentStart.
y - y) > lTolerance)
322 if((x - iSegmentStart.
x) > lTolerance && (iSegmentEnd.
x - x) > lTolerance)
324 if((x - iSegmentEnd.
x) > lTolerance && (iSegmentStart.
x - x) > lTolerance)
333 if (y == irSegmentStart.
y && y == irSegmentEnd.
y)
335 return ((x - irSegmentStart.
x) >= 0 && (irSegmentEnd.
x - x) >= 0) ||
336 ((x - irSegmentStart.
x) <= 0 && (irSegmentEnd.
x - x) <= 0);
339 if (x == irSegmentStart.
x && x == irSegmentEnd.
x)
341 return ((y - irSegmentStart.
y) >= 0 && (irSegmentEnd.
y - y) >= 0) ||
342 ((y - irSegmentStart.
y) <= 0 && (irSegmentEnd.
y - y) <= 0);
351 return CGPointMake(x, y);
358 if (IsInPolygon(irPolygon))
361 const size_t lNumOfEdges = irPolygon.size();
362 double lDistance = DistanceToLine(irPolygon[0], irPolygon[lNumOfEdges - 1]);
363 for (
size_t i = 0; i + 1 < lNumOfEdges; ++i)
365 double lNewDistance = DistanceToLine(irPolygon[i], irPolygon[i + 1]);
366 if (lDistance > lNewDistance)
368 lDistance = lNewDistance;
377 double lPointToStart2 = DistanceSquared(irLineStart);
378 double lPointToEnd2 = DistanceSquared(irLineEnd);
381 const float lTolerance = 0.01;
382 if (lPointToStart2 < lTolerance || lPointToEnd2 < lTolerance)
384 if (lStartToEnd2 < lTolerance)
385 return std::sqrt(lPointToStart2);
387 if (lPointToStart2 >= lPointToEnd2 + lStartToEnd2)
388 return std::sqrt(lPointToEnd2);
390 if (lPointToEnd2 >= lPointToStart2 + lStartToEnd2)
391 return std::sqrt(lPointToStart2);
393 Point<T> lStartToPoint(x - irLineStart.
x, y - irLineStart.
y);
394 Point<T> lStartToEnd(irLineEnd.
x - irLineStart.
x, irLineEnd.
y - irLineStart.
y);
395 double lCrossProduct = lStartToPoint.
x * lStartToEnd.
y - lStartToPoint.
y * lStartToEnd.
x;
396 return (lCrossProduct < 0 ? -lCrossProduct : lCrossProduct) / std::sqrt(lStartToEnd2);
Point< float > FPoint2D
Definition: Point2D.h:403
CGPoint GetCGPoint() const
Definition: Point2D.h:349
Point< T > MiddleTo(const Point< T > &irPoint) const
Definition: Point2D.h:285
Point< Int32 > Point2D
Definition: Point2D.h:402
void Rotate(const Point< T > &irAnchor, double iRadian)
Definition: Point2D.h:261
Point(const Point< T > &irPoint)
Definition: Point2D.h:39
Point< T > RotateAndClone(const Point< T > &irAnchor, double iRadian)
Definition: Point2D.h:271
bool IsOnSegment(const Point< T > &irSegmentStart, const Point< T > &irSegmentEnd, bool iExclude) const
Definition: Point2D.h:310
double DistanceSquared(const Point< T > &irPoint) const
Definition: Point2D.h:194
Point< T > operator-() const
Definition: Point2D.h:219
Point(T iX=T(), T iY=T())
Definition: Point2D.h:34
T y
Y-coordinate of current point.
Definition: Point2D.h:175
Point< T > Shifted(const Point< T > &irOffset) const
Definition: Point2D.h:291
double DistanceToPolygon(const std::vector< Point< T > > &irPolygon) const
Definition: Point2D.h:356
#define Int32
Definition: BasicTypes.h:20
void Reset(T iX, T iY)
Definition: Point2D.h:187
bool IsInPolygon(const std::vector< Point< T > > &irPolygon) const
Definition: Point2D.h:226
T x
X-coordinate of current point.
Definition: Point2D.h:174
void Swap()
Definition: Point2D.h:297
double DistanceToLine(const Point< T > &irLineStart, const Point< T > &irLineEnd) const
Definition: Point2D.h:375
bool operator==(const Point< T > &irPoint) const
Definition: Point2D.h:213
bool IsOnVerOrHorSegment(const Point< T > &irSegmentStart, const Point< T > &irSegmentEnd) const
Definition: Point2D.h:331
double Distance(const Point< T > &irPoint) const
Definition: Point2D.h:201
bool IsStrictlyInPolygon(const std::vector< Point< T > > &irPolygon) const
Definition: Point2D.h:247
bool operator!=(const Point< T > &irPoint) const
Definition: Point2D.h:207