/// <summary>
        /// Determines if a point in inside or outside a polygon. Inside
        /// includes on the edge for this method.
        /// Works for both convex and concave polygons (Winding number test)
        /// </summary>
        /// <param name="x">x-coordinate for the point</param>
        /// <param name="y">y-coordiante for the point</param>
        /// <param name="polygon">Polygon</param>
        /// <returns>
        ///	<p>true:  If the point is inside the polygon</p>
        ///	<p>false: If the point is outside the polygon.</p>
        /// </returns>
        protected static bool IsPointInPolygonOrOnEdge(double x, double y, XYPolygon polygon)
        {
            bool result = IsPointInPolygon(x, y, polygon);

            if (result)
            {
                return(result);
            }
            else
            {
                int iLine = 0;
                while ((!result) && (iLine < polygon.Points.Count))
                {
                    var line = polygon.GetLine(iLine);
                    result = IsPointInLine(x, y, line);
                    iLine++;
                }
            }
            return(result);
        }
Esempio n. 2
0
		[Test] public void GetLine()
		{
			// -- Triangle -- 
			XYPolygon xypolygon = new XYPolygon();
			xypolygon.Points.Add(new XYPoint(1,2));
			xypolygon.Points.Add(new XYPoint(4,3));
			xypolygon.Points.Add(new XYPoint(2,5));

			Assert.AreEqual(new XYLine(1,2,4,3),xypolygon.GetLine(0));
			Assert.AreEqual(new XYLine(4,3,2,5),xypolygon.GetLine(1));
			Assert.AreEqual(new XYLine(2,5,1,2),xypolygon.GetLine(2));

			
			// -- concave polygon --
			XYPolygon xypolygon4 = new XYPolygon();
			xypolygon4.Points.Add(new XYPoint(1,1));
			xypolygon4.Points.Add(new XYPoint(9,1));
			xypolygon4.Points.Add(new XYPoint(5,5));
			xypolygon4.Points.Add(new XYPoint(5,3));
			xypolygon4.Points.Add(new XYPoint(3,3));
			xypolygon4.Points.Add(new XYPoint(3,8));
			xypolygon4.Points.Add(new XYPoint(9,8));
			xypolygon4.Points.Add(new XYPoint(9,11));
			xypolygon4.Points.Add(new XYPoint(1,11));

			Assert.AreEqual(new XYLine(1, 1, 9,  1),xypolygon4.GetLine(0));
			Assert.AreEqual(new XYLine(9, 1, 5,  5),xypolygon4.GetLine(1));
			Assert.AreEqual(new XYLine(5, 5, 5,  3),xypolygon4.GetLine(2));
			Assert.AreEqual(new XYLine(5, 3, 3,  3),xypolygon4.GetLine(3));
			Assert.AreEqual(new XYLine(3, 3, 3,  8),xypolygon4.GetLine(4));
			Assert.AreEqual(new XYLine(3, 8, 9,  8),xypolygon4.GetLine(5));
			Assert.AreEqual(new XYLine(9, 8, 9, 11),xypolygon4.GetLine(6));
			Assert.AreEqual(new XYLine(9,11, 1 ,11),xypolygon4.GetLine(7));
			Assert.AreEqual(new XYLine(1,11, 1 , 1),xypolygon4.GetLine(8));
		}
        /// <summary>
        /// The method calculates the intersection points of triangle a and b both
        /// of type XYPolygon.
        /// </summary>
        /// <param name="triangleA">triangle. The search is started along triangleA.</param>
        /// <param name="triangleB">triangle. Intersection with this triangle are sought.</param>
        /// <param name="p">Starting point for the search. p must be part of triangleA.</param>
        /// <param name="i">on input: End index for the first line segment of triangleA in the search.
        /// on output: End index for the last intersected line segment in triangleA.</param>
        /// <param name="j">on input: -1 if vertices before intersection is not to be added to list.
        /// on output: End index for last intersected line segment of triangleB.</param>
        /// <param name="intersectionPolygon">polygon eventuallu describing the
        /// intersection area between triangleA and triangleB</param>
        /// <returns>
        ///	The p, i, j and intersectionPolygon are called by reference and modified in the method.
        /// </returns>
        private static void Intersect(XYPolygon triangleA, XYPolygon triangleB,
                                      ref XYPoint p, ref int i, ref int j,
                                      ref XYPolygon intersectionPolygon)
        {
            XYLine lineA;
            XYLine lineB;
            int    im1    = Decrease(i, 2); // "i-1"
            int    count1 = 0;
            bool   found  = false;

            while ((count1 < 3) && (!found))
            {
                lineA = triangleA.GetLine(im1);
                if (count1 == 0)
                {
                    lineA      = new XYLine(lineA);
                    lineA.P1.X = p.X;
                    lineA.P1.Y = p.Y;
                }
                double MinDist  = -1; // Distance used when a line is crossed more than once
                int    jm1      = 0;  // "j-1"
                int    jm1Store = -1;
                while (jm1 < 3)
                {
                    lineB = triangleB.GetLine(jm1);
                    found = IntersectionPoint(lineA, lineB, ref p);
                    double Dist = CalculatePointToPointDistance(lineA.P1, p);
                    if (Dist < EPSILON)
                    {
                        found = false;
                    }
                    if (found)
                    {
                        if ((MinDist < 0) || (Dist < MinDist))
                        {
                            MinDist  = Dist;
                            jm1Store = jm1;
                        }
                    }
                    jm1++;
                }
                if (jm1Store > -1)
                {
                    lineB = triangleB.GetLine(jm1Store);
                    found = IntersectionPoint(lineA, lineB, ref p);
                    XYPoint HelpCoordinate = new XYPoint(p.X, p.Y);
                    XYPoint HelpNode       = new XYPoint(HelpCoordinate);

                    intersectionPolygon.Points.Add(HelpNode);

                    j = Increase(jm1Store, 2);
                }
                if (!found)
                {
                    count1++;
                    im1 = Increase(im1, 2);
                    i   = Increase(i, 2);
                    if (j != -1)
                    {
                        XYPoint HelpCoordinate = new XYPoint(lineA.P2.X, lineA.P2.Y);
                        XYPoint HelpNode       = new XYPoint(HelpCoordinate);
                        intersectionPolygon.Points.Add(HelpNode);
                    }
                }
            }
            lineA = triangleA.GetLine(Decrease(i, 2));
            if (CalculatePointToPointDistance(p, lineA.P2) < EPSILON)
            {
                i = Increase(i, 2);
            }
            lineB = triangleB.GetLine(Decrease(j, 2));
            if (CalculatePointToPointDistance(p, lineB.P2) < EPSILON)
            {
                j = Increase(j, 2);
            }
        }
        /// <summary>
        /// Calculates length of line inside polygon. Parts of the line that is on the edge of
        /// the polygon only counts with half their length.
        /// </summary>
        /// <param name="line">Line</param>
        /// <param name="polygon">Polygon</param>
        /// <returns>
        /// Length of line inside polygon.
        /// </returns>
        protected static double CalculateLengthOfLineInsidePolygon(XYLine line, XYPolygon polygon)
        {
            var lineList = new List <XYLine> {
                new XYLine(line)
            };

            for (int i = 0; i < polygon.Points.Count; i++) // For all lines in the polygon
            {
                for (int n = 0; n < lineList.Count; n++)
                {
                    if (lineList.Count > 1000)
                    {
                        throw new Exception("Problems in ElementMapper, line has been cut in more than 1000 pieces !!!");
                    }

                    if (DoLineSegmentsIntersect((XYLine)lineList[n], polygon.GetLine(i)))
                    {
                        // Split the intersecting line into two lines
                        XYPoint IntersectionPoint = new XYPoint(CalculateIntersectionPoint((XYLine)lineList[n], polygon.GetLine(i)));
                        lineList.Add(new XYLine(IntersectionPoint, lineList[n].P2));
                        lineList[n].P2.X = IntersectionPoint.X;
                        lineList[n].P2.Y = IntersectionPoint.Y;
                        break;
                    }
                }
            }

            for (int i = 0; i < lineList.Count; i++)
            {
                if (lineList.Count > 1000)
                {
                    throw new Exception("Problems in ElementMapper, line has been cuttes in more than 100 pieces !!!");
                }
                for (int j = 0; j < polygon.Points.Count; j++)
                {
                    if (IsPointInLineInterior(polygon.GetLine(j).P1, ((XYLine)lineList[i])))
                    {
                        lineList.Add(new XYLine(polygon.GetLine(j).P1, ((XYLine)lineList[i]).P2));
                        lineList[i].P2.X = polygon.GetLine(j).P1.X;
                        lineList[i].P2.Y = polygon.GetLine(j).P1.Y;
                    }
                }
            }

            double lengthInside = 0;

            for (int i = 0; i < lineList.Count; i++)
            {
                double sharedLength = 0;
                for (int j = 0; j < polygon.Points.Count; j++)
                {
                    sharedLength += CalculateSharedLength(((XYLine)lineList[i]), polygon.GetLine(j));
                }
                if (sharedLength > EPSILON)
                {
                    lengthInside += sharedLength / 2;
                }
                else if (IsPointInPolygon(((XYLine)lineList[i]).GetMidpoint(), polygon))
                {
                    lengthInside += ((XYLine)lineList[i]).GetLength();
                }
            }
            return(lengthInside);
        }
 /// <summary>
 /// Determines if a point in inside or outside a polygon. Inside
 /// includes on the edge for this method.
 /// Works for both convex and concave polygons (Winding number test)
 /// </summary>
 /// <param name="x">x-coordinate for the point</param>
 /// <param name="y">y-coordiante for the point</param>
 /// <param name="polygon">Polygon</param>
 /// <returns>
 ///	<p>true:  If the point is inside the polygon</p>
 ///	<p>false: If the point is outside the polygon.</p>
 /// </returns>
 protected static bool IsPointInPolygonOrOnEdge(double x, double y, XYPolygon polygon)
 {
   bool result = IsPointInPolygon(x, y, polygon);
   if( result )
   {
     return result;
   }
   else
   {
     int iLine = 0;
     while( (!result) && (iLine < polygon.Points.Count) )
     {
       XYLine line = new XYLine();
       line = polygon.GetLine(iLine);
       result = IsPointInLine(x, y, line);
       iLine++;
     }
   }
   return result;
 }
    /// <summary>
    /// The method calculates the intersection points of triangle a and b both
    /// of type XYPolygon.
    /// </summary>
    /// <param name="triangleA">triangle. The search is started along triangleA.</param>
    /// <param name="triangleB">triangle. Intersection with this triangle are sought.</param>
    /// <param name="p">Starting point for the search. p must be part of triangleA.</param>
    /// <param name="i">on input: End index for the first line segment of triangleA in the search.
    /// on output: End index for the last intersected line segment in triangleA.</param>
    /// <param name="j">on input: -1 if vertices before intersection is not to be added to list.
    /// on output: End index for last intersected line segment of triangleB.</param>
    /// <param name="intersectionPolygon">polygon eventuallu describing the 
    /// intersection area between triangleA and triangleB</param>
    /// <returns>
    ///	The p, i, j and intersectionPolygon are called by reference and modified in the method.
    /// </returns>
    private static void Intersect (XYPolygon triangleA, XYPolygon triangleB, 
                                   ref XYPoint p, ref  int i, ref int j, 
                                   ref XYPolygon intersectionPolygon)
    {
      XYLine lineA;
      XYLine lineB;
      int im1 = Decrease(i, 2); // "i-1"
      int count1 = 0;
      bool found = false;

      while ((count1 < 3) && (!found))
      {
        lineA = triangleA.GetLine(im1);
        if (count1 == 0)
        {
          lineA.P1.X = p.X;
          lineA.P1.Y = p.Y;
        }
        double MinDist = -1; // Distance used when a line is crossed more than once
        int jm1 = 0;         // "j-1"
        int jm1Store = -1;
        while (jm1 < 3)
        {
          lineB = triangleB.GetLine(jm1);
          found = IntersectionPoint(lineA, lineB, ref p);
          double Dist = CalculatePointToPointDistance(lineA.P1,p);
          if (Dist < EPSILON)
          {
            found = false;
          }
          if (found)
          {
            if ((MinDist < 0) || (Dist < MinDist))
            {
              MinDist = Dist;
              jm1Store = jm1;
            }
          }
          jm1++;
        }
        if ( jm1Store > -1 )
        {
          lineB = triangleB.GetLine(jm1Store);
          found = IntersectionPoint(lineA, lineB, ref p);          
		      XYPoint HelpCoordinate = new XYPoint(p.X, p.Y);
		      XYPoint HelpNode = new XYPoint(HelpCoordinate);

		      intersectionPolygon.Points.Add(HelpNode);

          j = Increase(jm1Store,2);  
        }
        if (!found)
        {
          count1++;
          im1 = Increase(im1,2);
          i = Increase(i,2);
          if (j!=-1) 
          {
		        XYPoint HelpCoordinate = new XYPoint(lineA.P2.X, lineA.P2.Y);
       			XYPoint HelpNode = new XYPoint(HelpCoordinate);
       			intersectionPolygon.Points.Add(HelpNode);
          }
        }
      }
      lineA = triangleA.GetLine(Decrease(i, 2));
      if ( CalculatePointToPointDistance(p, lineA.P2)<EPSILON )
      {
        i = Increase(i, 2);
      }
      lineB = triangleB.GetLine(Decrease(j, 2));
      if ( CalculatePointToPointDistance(p, lineB.P2)<EPSILON )
      {
        j = Increase(j, 2);
      }
    }
    /// <summary>
    /// Calculates length of line inside polygon. Parts of the line that is on the edge of 
    /// the polygon only counts with half their length.
    /// </summary>
    /// <param name="line">Line</param>
    /// <param name="polygon">Polygon</param>
    /// <returns>
    /// Length of line inside polygon.
    /// </returns>
    protected static double CalculateLengthOfLineInsidePolygon(XYLine line, XYPolygon polygon)
    {
          ArrayList lineList = new ArrayList();
        lineList.Add(new XYLine(line));
          
          for (int i = 0; i < polygon.Points.Count; i++) // For all lines in the polygon
          {
              for (int n = 0; n < lineList.Count; n++)   
              {
                  if (lineList.Count > 1000)
                  {
                      throw new Exception("Problems in ElementMapper, line has been cut in more than 1000 pieces !!!");
                  }

                  if (DoLineSegmentsIntersect((XYLine)lineList[n], polygon.GetLine(i)))
                  {
                      // Split the intersecting line into two lines
                      XYPoint IntersectionPoint = new XYPoint(CalculateIntersectionPoint((XYLine)lineList[n], polygon.GetLine(i)));
                      lineList.Add(new XYLine(IntersectionPoint, ((XYLine) lineList[n]).P2));
                      ((XYLine) lineList[n]).P2.X = IntersectionPoint.X;
                      ((XYLine) lineList[n]).P2.Y = IntersectionPoint.Y;
                      break;
                  }
              }
          }
	
          for (int i = 0; i < lineList.Count; i++)
          {
              if (lineList.Count > 1000)
              {
                  throw new Exception("Problems in ElementMapper, line has been cuttes in more than 100 pieces !!!");
              }
              for (int j = 0; j < polygon.Points.Count; j++)
              {
                  if (IsPointInLineInterior( polygon.GetLine(j).P1, ((XYLine) lineList[i])))
                  {
                      lineList.Add(new XYLine(polygon.GetLine(j).P1, ((XYLine) lineList[i]).P2));
                      ((XYLine) lineList[i]).P2.X = polygon.GetLine(j).P1.X;
                      ((XYLine) lineList[i]).P2.Y = polygon.GetLine(j).P1.Y;
                  }
              }  
          }
   
          double lengthInside = 0;
          for (int i = 0; i < lineList.Count; i++)
		{
              double sharedLength = 0;
              for (int j = 0; j < polygon.Points.Count; j++)
              {
                  sharedLength += CalculateSharedLength(((XYLine) lineList[i]), polygon.GetLine(j));
              }
              if (sharedLength > EPSILON)
              {
                  lengthInside += sharedLength/2;
              }
              else if (IsPointInPolygon(((XYLine) lineList[i]).GetMidpoint(), polygon))
              {
                  lengthInside += ((XYLine) lineList[i]).GetLength();
              }
          }
          return lengthInside;
		}