187 lines
No EOL
5.5 KiB
C#
187 lines
No EOL
5.5 KiB
C#
using System;
|
|
|
|
namespace GeometryUtility
|
|
{
|
|
/// <summary>
|
|
/// Summary description for CPoint2D.
|
|
/// </summary>
|
|
|
|
// A point in Coordinate System
|
|
public class CPoint2D
|
|
{
|
|
private double _dCoordinate_X;
|
|
|
|
private double _dCoordinate_Y;
|
|
|
|
public CPoint2D()
|
|
{
|
|
}
|
|
|
|
public CPoint2D(double xCoordinate, double yCoordinate)
|
|
{
|
|
_dCoordinate_X = xCoordinate;
|
|
_dCoordinate_Y = yCoordinate;
|
|
}
|
|
|
|
public double X
|
|
{
|
|
get
|
|
{
|
|
return _dCoordinate_X;
|
|
}
|
|
|
|
set
|
|
{
|
|
_dCoordinate_X = value;
|
|
}
|
|
}
|
|
|
|
public double Y
|
|
{
|
|
get
|
|
{
|
|
return _dCoordinate_Y;
|
|
}
|
|
|
|
set
|
|
{
|
|
_dCoordinate_Y = value;
|
|
}
|
|
}
|
|
|
|
public static bool SamePoints(CPoint2D Point1, CPoint2D Point2)
|
|
{
|
|
double dDeff_X = Math.Abs(Point1.X - Point2.X);
|
|
double dDeff_Y = Math.Abs(Point1.Y - Point2.Y);
|
|
|
|
if ((dDeff_X < ConstantValue.SmallValue) && (dDeff_Y < ConstantValue.SmallValue)) return true;
|
|
else return false;
|
|
}
|
|
|
|
public bool EqualsPoint(CPoint2D newPoint)
|
|
{
|
|
double dDeff_X = Math.Abs(_dCoordinate_X - newPoint.X);
|
|
double dDeff_Y = Math.Abs(_dCoordinate_Y - newPoint.Y);
|
|
|
|
if ((dDeff_X < ConstantValue.SmallValue) && (dDeff_Y < ConstantValue.SmallValue)) return true;
|
|
else return false;
|
|
}
|
|
|
|
/***To check whether the point is in a line segment***/
|
|
public bool InLine(CLineSegment lineSegment)
|
|
{
|
|
bool bInline = false;
|
|
|
|
double Ax, Ay, Bx, By, Cx, Cy;
|
|
Bx = lineSegment.EndPoint.X;
|
|
By = lineSegment.EndPoint.Y;
|
|
Ax = lineSegment.StartPoint.X;
|
|
Ay = lineSegment.StartPoint.Y;
|
|
Cx = this._dCoordinate_X;
|
|
Cy = this._dCoordinate_Y;
|
|
|
|
double L = lineSegment.GetLineSegmentLength();
|
|
double s = Math.Abs(((Ay - Cy) * (Bx - Ax) - (Ax - Cx) * (By - Ay)) / (L * L));
|
|
|
|
if (Math.Abs(s - 0) < ConstantValue.SmallValue)
|
|
{
|
|
if (SamePoints(this, lineSegment.StartPoint) || (SamePoints(this, lineSegment.EndPoint))) bInline = true;
|
|
else if ((Cx < lineSegment.GetXmax()) && (Cx > lineSegment.GetXmin()) && (Cy < lineSegment.GetYmax()) && (Cy > lineSegment.GetYmin())) bInline = true;
|
|
}
|
|
|
|
return bInline;
|
|
}
|
|
|
|
/*** Distance between two points***/
|
|
public double DistanceTo(CPoint2D point)
|
|
{
|
|
return Math.Sqrt((point.X - this.X) * (point.X - this.X) + (point.Y - this.Y) * (point.Y - this.Y));
|
|
}
|
|
|
|
public bool PointInsidePolygon(CPoint2D[] polygonVertices)
|
|
{
|
|
if (polygonVertices.Length < 3) // not a valid polygon
|
|
return false;
|
|
|
|
int nCounter = 0;
|
|
int nPoints = polygonVertices.Length;
|
|
|
|
CPoint2D s1, p1, p2;
|
|
s1 = this;
|
|
p1 = polygonVertices[0];
|
|
|
|
for (int i = 1; i < nPoints; i++)
|
|
{
|
|
p2 = polygonVertices[i % nPoints];
|
|
if (s1.Y > Math.Min(p1.Y, p2.Y))
|
|
{
|
|
if (s1.Y <= Math.Max(p1.Y, p2.Y))
|
|
{
|
|
if (s1.X <= Math.Max(p1.X, p2.X))
|
|
{
|
|
if (Math.Abs(p1.Y - p2.Y) > float.Epsilon)
|
|
{
|
|
double xInters = (s1.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
|
|
if ((Math.Abs(p1.X - p2.X) < float.Epsilon) || (s1.X <= xInters))
|
|
{
|
|
nCounter ++;
|
|
}
|
|
}
|
|
|
|
// p1.y != p2.y
|
|
}
|
|
}
|
|
}
|
|
|
|
p1 = p2;
|
|
}
|
|
|
|
// for loop
|
|
|
|
if ((nCounter % 2) == 0) return false;
|
|
else return true;
|
|
}
|
|
|
|
/*********** Sort points from Xmin->Xmax ******/
|
|
public static void SortPointsByX(CPoint2D[] points)
|
|
{
|
|
if (points.Length > 1)
|
|
{
|
|
CPoint2D tempPt;
|
|
for (int i = 0; i < points.Length - 2; i++)
|
|
{
|
|
for (int j = i + 1; j < points.Length - 1; j++)
|
|
{
|
|
if (points[i].X > points[j].X)
|
|
{
|
|
tempPt = points[j];
|
|
points[j] = points[i];
|
|
points[i] = tempPt;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/*********** Sort points from Ymin->Ymax ******/
|
|
public static void SortPointsByY(CPoint2D[] points)
|
|
{
|
|
if (points.Length > 1)
|
|
{
|
|
CPoint2D tempPt;
|
|
for (int i = 0; i < points.Length - 2; i++)
|
|
{
|
|
for (int j = i + 1; j < points.Length - 1; j++)
|
|
{
|
|
if (points[i].Y > points[j].Y)
|
|
{
|
|
tempPt = points[j];
|
|
points[j] = points[i];
|
|
points[i] = tempPt;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |