using System;
using GeometryUtility;
namespace PolygonCuttingEar
{
///
/// Summary description for Class1.
///
public class CPolygonShape
{
private readonly CPoint2D[] _aInputVertices;
private CPoint2D[] _aUpdatedPolygonVertices;
private readonly System.Collections.ArrayList _alEars
=new System.Collections.ArrayList();
private CPoint2D[][] _aPolygons;
public int NumberOfPolygons
{
get
{
return _aPolygons.Length;
}
}
public CPoint2D[] Polygons(int index)
{
if (index< _aPolygons.Length)
return _aPolygons[index];
else
return null;
}
public CPolygonShape(CPoint2D[] vertices)
{
int nVertices=vertices.Length;
if (nVertices<3)
{
System.Diagnostics.Trace.WriteLine("To make a polygon, "
+ " at least 3 points are required!");
return;
}
//initalize the 2D points
_aInputVertices=new CPoint2D[nVertices];
for (int i=0; i0)
{
if ((dblArea1 >0) &&(dblArea2>0))
bIn=true;
}
else if (dblArea0<0)
{
if ((dblArea1 < 0) && (dblArea2< 0))
bIn=true;
}
}
return bIn;
}
/****************************************************************
To check whether the Vertex is an ear or not based updated Polygon vertices
ref. www-cgrl.cs.mcgill.ca/~godfried/teaching/cg-projects/97/Ian
/algorithm1.html
If it is an ear, return true,
If it is not an ear, return false;
*****************************************************************/
private bool IsEarOfUpdatedPolygon(CPoint2D vertex )
{
CPolygon polygon=new CPolygon(_aUpdatedPolygonVertices);
if (polygon.PolygonVertex(vertex))
{
bool bEar=true;
if (polygon.PolygonVertexType(vertex)==VertexType.ConvexPoint)
{
CPoint2D pi=vertex;
CPoint2D pj=polygon.PreviousPoint(vertex); //previous vertex
CPoint2D pk=polygon.NextPoint(vertex);//next vertex
for (int i=_aUpdatedPolygonVertices.GetLowerBound(0);
i<_aUpdatedPolygonVertices.GetUpperBound(0); i++)
{
CPoint2D pt = _aUpdatedPolygonVertices[i];
if ( !(pt.EqualsPoint(pi)|| pt.EqualsPoint(pj)||pt.EqualsPoint(pk)))
{
if (TriangleContainsPoint(new CPoint2D[] {pj, pi, pk}, pt))
bEar=false;
}
}
} //ThePolygon.getVertexType(Vertex)=ConvexPt
else //concave point
bEar=false; //not an ear/
return bEar;
}
else //not a polygon vertex;
{
System.Diagnostics.Trace.WriteLine("IsEarOfUpdatedPolygon: "+
"Not a polygon vertex");
return false;
}
}
/****************************************************
Set up _aPolygons:
add ears and been cut Polygon togather
****************************************************/
private void SetPolygons()
{
int nPolygon=_alEars.Count + 1; //ears plus updated polygon
_aPolygons=new CPoint2D[nPolygon][];
for (int i=0; i