/// <summary>
    /// Calculates the length that two lines overlap.
    /// </summary>
    /// <param name="lineA">Line</param>
    /// <param name="lineB">Line</param>
    /// <returns>
    /// Length of shared line segment.
    /// </returns>
    protected static double CalculateSharedLength(XYLine lineA, XYLine lineB)
    {
		  if ( Math.Abs(lineA.P2.X-lineA.P1.X)<EPSILON && Math.Abs(lineB.P2.X-lineB.P1.X)<EPSILON &&Math.Abs(lineA.P1.X-lineB.P1.X)<EPSILON)
		  {
			  double YP1A = Math.Min(lineA.P1.Y, lineA.P2.Y);
			  double YP2A = Math.Max(lineA.P1.Y, lineA.P2.Y);
			  double YP1B = Math.Min(lineB.P1.Y, lineB.P2.Y);
			  double YP2B = Math.Max(lineB.P1.Y, lineB.P2.Y);

			  double YP1 = Math.Max(YP1A, YP1B);
			  double YP2 = Math.Min(YP2A, YP2B);
			  if (YP1 < YP2) 
			  {
				  return YP2-YP1;
			  }
			  else
			  {
				  return 0;
			  }
		  }
		  else if(Math.Abs(lineA.P2.X-lineA.P1.X)<EPSILON || Math.Abs(lineB.P2.X-lineB.P1.X)<EPSILON)
		  {
			  return 0;
		  }
		  else
		  {
			  IXYPoint P1A = new XYPoint();
			  IXYPoint P2A = new XYPoint();
			  if (lineA.P1.X < lineA.P2.X)
			  {
				  P1A = lineA.P1;
				  P2A = lineA.P2;
			  }
			  else
			  {
				  P1A = lineA.P2;
				  P2A = lineA.P1;
			  }
			  IXYPoint P1B = new XYPoint();
			  IXYPoint P2B = new XYPoint();
			  if (lineB.P1.X < lineB.P2.X)
			  {
				  P1B = lineB.P1;
				  P2B = lineB.P2;
			  }
			  else
			  {
				  P1B = lineB.P2;
				  P2B = lineB.P1;
			  }

			  double alphaA = (P2A.Y - P1A.Y)/(P2A.X - P1A.X);
			  double betaA = -alphaA*P2A.X + P2A.Y;
			  double alphaB = (P2B.Y - P1B.Y)/(P2B.X - P1B.X);
			  double betaB = -alphaA*P2B.X + P2B.Y;
			  if (Math.Abs(alphaA-alphaB)<EPSILON && Math.Abs(betaA-betaB)<EPSILON)
			  {
				  double x1 = Math.Max(P1A.X, P1B.X);
				  double x2 = Math.Min(P2A.X, P2B.X);
				  if (x1 < x2)
				  {
					  XYLine line = new XYLine(x1, alphaA*x1+betaA, x2, alphaA*x2+betaA);
					  return line.GetLength();
				  }
				  else
				  {
					  return 0;
				  }
			  }
			  else
			  {
				  return 0;
			  }
		  }
    }
Example #2
0
        /// <summary>
        /// Calculates the length that two lines overlap.
        /// </summary>
        /// <param name="lineA">Line</param>
        /// <param name="lineB">Line</param>
        /// <returns>
        /// Length of shared line segment.
        /// </returns>
        protected static double CalculateSharedLength(XYLine lineA, XYLine lineB)
        {
            if (Math.Abs(lineA.P2.X - lineA.P1.X) < EPSILON && Math.Abs(lineB.P2.X - lineB.P1.X) < EPSILON && Math.Abs(lineA.P1.X - lineB.P1.X) < EPSILON)
            {
                double YP1A = Math.Min(lineA.P1.Y, lineA.P2.Y);
                double YP2A = Math.Max(lineA.P1.Y, lineA.P2.Y);
                double YP1B = Math.Min(lineB.P1.Y, lineB.P2.Y);
                double YP2B = Math.Max(lineB.P1.Y, lineB.P2.Y);

                double YP1 = Math.Max(YP1A, YP1B);
                double YP2 = Math.Min(YP2A, YP2B);
                if (YP1 < YP2)
                {
                    return(YP2 - YP1);
                }
                else
                {
                    return(0);
                }
            }
            else if (Math.Abs(lineA.P2.X - lineA.P1.X) < EPSILON || Math.Abs(lineB.P2.X - lineB.P1.X) < EPSILON)
            {
                return(0);
            }
            else
            {
                IXYPoint P1A = new XYPoint();
                IXYPoint P2A = new XYPoint();
                if (lineA.P1.X < lineA.P2.X)
                {
                    P1A = lineA.P1;
                    P2A = lineA.P2;
                }
                else
                {
                    P1A = lineA.P2;
                    P2A = lineA.P1;
                }
                IXYPoint P1B = new XYPoint();
                IXYPoint P2B = new XYPoint();
                if (lineB.P1.X < lineB.P2.X)
                {
                    P1B = lineB.P1;
                    P2B = lineB.P2;
                }
                else
                {
                    P1B = lineB.P2;
                    P2B = lineB.P1;
                }

                double alphaA = (P2A.Y - P1A.Y) / (P2A.X - P1A.X);
                double betaA  = -alphaA * P2A.X + P2A.Y;
                double alphaB = (P2B.Y - P1B.Y) / (P2B.X - P1B.X);
                double betaB  = -alphaA * P2B.X + P2B.Y;
                if (Math.Abs(alphaA - alphaB) < EPSILON && Math.Abs(betaA - betaB) < EPSILON)
                {
                    double x1 = Math.Max(P1A.X, P1B.X);
                    double x2 = Math.Min(P2A.X, P2B.X);
                    if (x1 < x2)
                    {
                        XYLine line = new XYLine(x1, alphaA * x1 + betaA, x2, alphaA * x2 + betaA);
                        return(line.GetLength());
                    }
                    else
                    {
                        return(0);
                    }
                }
                else
                {
                    return(0);
                }
            }
        }