/********************************************************** * To check the Pt is in the Triangle or not. * If the Pt is in the line or is a vertex, then return true. * If the Pt is out of the Triangle, then return false. * * This method is used for triangle only. ***********************************************************/ private bool TriangleContainsPoint(Coordinate[] trianglePts, Coordinate pt) { if (trianglePts.Length != 3) { return(false); } for (int i = trianglePts.GetLowerBound(0); i < trianglePts.GetUpperBound(0); i++) { if (pt.Equals(trianglePts[i])) { return(true); } } bool bIn = false; LineSegmentEquation line0 = new LineSegmentEquation( trianglePts[0], trianglePts[1]); LineSegmentEquation line1 = new LineSegmentEquation( trianglePts[1], trianglePts[2]); LineSegmentEquation line2 = new LineSegmentEquation( trianglePts[2], trianglePts[0]); if (line0.OnLine(pt) || line1.OnLine(pt) || line2.OnLine(pt)) { bIn = true; } else //point is not in the lines { double dblArea0 = PolygonHelper.PolygonArea( new Coordinate[] { trianglePts[0], trianglePts[1], pt }); double dblArea1 = PolygonHelper.PolygonArea( new Coordinate[] { trianglePts[1], trianglePts[2], pt }); double dblArea2 = PolygonHelper.PolygonArea( new Coordinate[] { trianglePts[2], trianglePts[0], pt }); if (dblArea0 > 0) { if ((dblArea1 > 0) && (dblArea2 > 0)) { bIn = true; } } else if (dblArea0 < 0) { if ((dblArea1 < 0) && (dblArea2 < 0)) { bIn = true; } } } return(bIn); }
/**************************************************** * To fill m_aUpdatedPolygonVertices array with input array. * * m_aUpdatedPolygonVertices is a working array that will * be updated when an ear is cut till m_aUpdatedPolygonVertices * makes triangle (a convex polygon). ******************************************************/ private void SetUpdatedPolygonVertices() { int nVertices = m_aInputVertices.Length; m_aUpdatedPolygonVertices = new Coordinate[nVertices]; for (int i = 0; i < nVertices; i++) { m_aUpdatedPolygonVertices[i] = m_aInputVertices[i]; } //m_aUpdatedPolygonVertices should be in count clock wise if (PolygonHelper.PointsDirection(m_aUpdatedPolygonVertices) == PolygonDirection.Clockwise) { PolygonHelper.ReversePointsDirection(m_aUpdatedPolygonVertices); } }
/**************************************************************** * 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(Coordinate vertex) { PolygonHelper polygon = new PolygonHelper(m_aUpdatedPolygonVertices); if (polygon.PolygonVertex(vertex)) { bool bEar = true; if (polygon.VertexType(vertex) == PolygonVertexType.ConvexPoint) { Coordinate pi = vertex; Coordinate pj = polygon.PreviousPoint(vertex); //previous vertex Coordinate pk = polygon.NextPoint(vertex); //next vertex for (int i = m_aUpdatedPolygonVertices.GetLowerBound(0); i < m_aUpdatedPolygonVertices.GetUpperBound(0); i++) { Coordinate pt = m_aUpdatedPolygonVertices[i]; if (!(pt.Equals(pi) || pt.Equals(pj) || pt.Equals(pk))) { if (TriangleContainsPoint(new Coordinate[] { 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; { Debug.WriteLine("IsEarOfUpdatedPolygon: " + "Not a polygon vertex"); return(false); } }
/******************************************************** * To update m_aUpdatedPolygonVertices: * Take out Vertex from m_aUpdatedPolygonVertices array, add 3 points * to the m_aEars **********************************************************/ private void UpdatePolygonVertices(Coordinate vertex) { ArrayList alTempPts = new ArrayList(); for (int i = 0; i < m_aUpdatedPolygonVertices.Length; i++) { //add 3 pts to FEars if (vertex.Equals(m_aUpdatedPolygonVertices[i])) { PolygonHelper polygon = new PolygonHelper(m_aUpdatedPolygonVertices); Coordinate pti = vertex; Coordinate ptj = polygon.PreviousPoint(vertex); //previous point Coordinate ptk = polygon.NextPoint(vertex); //next point //3 vertices of each ear Coordinate[] aEar = new Coordinate[3]; aEar[0] = ptj; aEar[1] = pti; aEar[2] = ptk; m_alEars.Add(aEar); } else { alTempPts.Add(m_aUpdatedPolygonVertices[i]); } //not equal points } if ((m_aUpdatedPolygonVertices.Length - alTempPts.Count) == 1) { int nLength = m_aUpdatedPolygonVertices.Length; m_aUpdatedPolygonVertices = new Coordinate[nLength - 1]; for (int i = 0; i < alTempPts.Count; i++) { m_aUpdatedPolygonVertices[i] = (Coordinate)alTempPts[i]; } } }