using System; namespace GeometryUtility { /// /// Summary description for CPolygon. /// public class CPolygon { private readonly CPoint2D[] _aVertices; public CPoint2D this[int index] { set { _aVertices[index]=value; } get { return _aVertices[index]; } } public CPolygon() { } //----------------------------------------------------------------------------------------- public CPolygon(CPoint2D[] points) { int numPoints = points.Length; try { if (numPoints < 3) { InvalidInputGeometryDataException ex= new InvalidInputGeometryDataException(); throw ex; } else { _aVertices = new CPoint2D[numPoints]; for (int i = 0; i < numPoints; i++) _aVertices[i] = points[i]; } } catch (Exception e) { System.Diagnostics.Trace.WriteLine( e.Message+e.StackTrace); } } //----------------------------------------------------------------------------------------- public CPolygon(CPoint2D[] points, int[] index) { int numPoints = index.Length; try { if (numPoints < 3) { InvalidInputGeometryDataException ex = new InvalidInputGeometryDataException(); throw ex; } else { _aVertices = new CPoint2D[numPoints]; for (int i = 0; i < numPoints; i++) _aVertices[i] = points[index[i]]; } } catch (Exception e) { System.Diagnostics.Trace.WriteLine(e.Message + e.StackTrace); } } /*********************************** From a given point, get its vertex index. If the given point is not a polygon vertex, it will return -1 ***********************************/ public int VertexIndex(CPoint2D vertex) { int nIndex=-1; int nNumPts=_aVertices.Length; for (int i=0; i0 : polygon in clock wise to the user If dblArea<0: polygon in count clock wise to the user *******************************************/ public static double PolygonArea(CPoint2D[] points) { double dblArea=0; int nNumOfPts=points.Length; int j; for (int i=0; i 0) vertexType= VertexType.ConcavePoint; } return vertexType; } /********************************************* To check the Line of vertex1, vertex2 is a Diagonal or not To be a diagonal, Line vertex1-vertex2 has no intersection with polygon lines. If it is a diagonal, return true; If it is not a diagonal, return false; reference: www.swin.edu.au/astronomy/pbourke /geometry/lineline2d *********************************************/ public bool Diagonal(CPoint2D vertex1, CPoint2D vertex2) { bool bDiagonal=false; int nNumOfVertices=_aVertices.Length; int j=0; for (int i= 0; iConstantValue.SmallValue) //lines are not parallel ub=((x2-x1)*(y1-y3)-(y2-y1)*(x1-x3))/de; if ((ub> 0) && (ub<1)) { bDiagonal=false; } } return bDiagonal; } /************************************************* To check FaVertices make a convex polygon or concave polygon Restriction: the polygon is not self intersecting Ref: www.swin.edu.au/astronomy/pbourke /geometry/clockwise/index.html ********************************************/ public PolygonType GetPolygonType() { int nNumOfVertices=_aVertices.Length; bool bSignChanged=false; int nCount=0; int j=0, k=0; for (int i=0; i0) && (nCount==0) ) nCount=1; else if ((crossProduct<0) && (nCount==0)) nCount=-1; if (((nCount==1) && (crossProduct<0)) ||( (nCount==-1) && (crossProduct>0)) ) bSignChanged=true; } if (bSignChanged) return PolygonType.Concave; else return PolygonType.Convex; } /*************************************************** Check a Vertex is a principal vertex or not ref. www-cgrl.cs.mcgill.ca/~godfried/teaching/ cg-projects/97/Ian/glossay.html PrincipalVertex: a vertex pi of polygon P is a principal vertex if the diagonal pi-1, pi+1 intersects the boundary of P only at pi-1 and pi+1. *********************************************************/ public bool PrincipalVertex(CPoint2D vertex) { bool bPrincipal=false; if (PolygonVertex(vertex)) //valid vertex { CPoint2D pt1=PreviousPoint(vertex); CPoint2D pt2=NextPoint(vertex); if (Diagonal(pt1, pt2)) bPrincipal=true; } return bPrincipal; } /********************************************* To check whether a given point is a CPolygon Vertex **********************************************/ public bool PolygonVertex(CPoint2D point) { bool bVertex=false; int nIndex=VertexIndex(point); if ((nIndex>=0) && (nIndex<=_aVertices.Length-1)) bVertex=true; return bVertex; } /***************************************************** To reverse polygon vertices to different direction: clock-wise <------->count-clock-wise ******************************************************/ public void ReverseVerticesDirection() { int nVertices=_aVertices.Length; CPoint2D[] aTempPts=new CPoint2D[nVertices]; for (int i=0; i0) nCount++; else nCount--; } if( nCount<0) return PolygonDirection.Count_Clockwise; else if (nCount> 0) return PolygonDirection.Clockwise; else return PolygonDirection.Unknown; } /***************************************** To check given points make a clock-wise polygon or count clockwise polygon Restriction: the polygon is not self intersecting *****************************************/ public static PolygonDirection PointsDirection( CPoint2D[] points) { int nCount=0, j=0, k=0; int nPoints=points.Length; if (nPoints<3) return PolygonDirection.Unknown; for (int i=0; i0) nCount++; else nCount--; } if( nCount<0) return PolygonDirection.Count_Clockwise; else if (nCount> 0) return PolygonDirection.Clockwise; else return PolygonDirection.Unknown; } //----------------------------------------------------------------------------------------- public static void ReversePointsDirection( CPoint2D[] points ) { int nVertices=points.Length; CPoint2D[] aTempPts=new CPoint2D[nVertices]; for (int i=0; i