/// <summary> /// Determines if a point is included in a lines interior. I.e. included /// in the line and not an endpoint. /// </summary> /// <param name="x">x-coordinate</param> /// <param name="y">y-coordinate</param> /// <param name="line">Line.</param> /// <returns> /// <p>Determines if a point is included in a line.</p> /// </returns> protected static bool IsPointInLineInterior(double x, double y, XYLine line) { bool result = false; if (line.P1.X - line.P2.X != 0) //line is not vertical { if ((x > Math.Min(line.P1.X, line.P2.X)) && (x < Math.Max(line.P1.X, line.P2.X))) { if (Math.Abs(y - line.P1.Y - (line.P2.Y - line.P1.Y) / (line.P1.X - line.P2.X) * (line.P1.X - x)) < EPSILON * EPSILON) { result = true; } } } else //line is vertical { if (line.P1.X == x) { if ((y > Math.Min(line.P1.Y, line.P2.Y)) && (y < Math.Max(line.P1.Y, line.P2.Y))) { result = true; } } } return(result); }
/// <summary> /// Calculates the distance from a polyline to a point in the plane. /// The algorithm decides weather the point lies besides the line /// segment in which case the distance is the length along a line /// perpendicular to the line. Alternatively the distance is the /// smallest of the distances to either endpoint. /// </summary> /// <param name="line">Line</param> /// <param name="point">Point</param> /// <returns> /// <p>Length of the shortest path between the line and the point.</p> /// </returns> protected static double CalculateLineToPointDistance(XYLine line, XYPoint point) { double dist = 0; double a = Math.Sqrt((line.P2.X - point.X) * (line.P2.X - point.X) + (line.P2.Y - point.Y) * (line.P2.Y - point.Y)); double b = Math.Sqrt((line.P2.X - line.P1.X) * (line.P2.X - line.P1.X) + (line.P2.Y - line.P1.Y) * (line.P2.Y - line.P1.Y)); double c = Math.Sqrt((line.P1.X - point.X) * (line.P1.X - point.X) + (line.P1.Y - point.Y) * (line.P1.Y - point.Y)); if ((a == 0) || (c == 0)) { dist = 0; } else if (b == 0) { dist = a; } else { double alpha = Math.Acos((b * b + c * c - a * a) / (2 * b * c)); double beta = Math.Acos((a * a + b * b - c * c) / (2 * a * b)); if (Math.Max(alpha, beta) < Math.PI / 2) { dist = Math.Abs((line.P2.X - line.P1.X) * (line.P1.Y - point.Y) - (line.P1.X - point.X) * (line.P2.Y - line.P1.Y)) / b; } else { dist = Math.Min(a, c); } } return(dist); }
/// <summary> /// Determines if a point is included in a line either in the interior /// or as one of the end points. /// </summary> /// <param name="x">x-coordinate</param> /// <param name="y">y-coordinate</param> /// <param name="line">Line.</param> /// <returns> /// <p>Determines if a point is included in a line.</p> /// </returns> protected static bool IsPointInLine(double x, double y, XYLine line) { bool result = false; if (line.P1.X - line.P2.X != 0) { if ((x >= Math.Min(line.P1.X, line.P2.X)) && (x <= Math.Max(line.P1.X, line.P2.X))) { if (Math.Abs(y - line.P1.Y - (line.P2.Y - line.P1.Y) / (line.P1.X - line.P2.X) * (line.P1.X - x)) < EPSILON * EPSILON) { result = true; } } } else { if (line.P1.X == x) { if ((y >= Math.Min(line.P1.Y, line.P2.Y)) && (y <= Math.Max(line.P1.Y, line.P2.Y))) { result = true; } } } return(result); }
/// <summary> /// Calculates the squared distance between a point and a line segment /// </summary> /// <param name="x">X coordinate of point</param> /// <param name="y">Y coordinate of point</param> /// <param name="line">Line segment</param> /// <returns>The squared distance</returns> public static double PointLineDistanceSq(double x, double y, XYLine line) { double vxline = line.P2.X - line.P1.X; double vyline = line.P2.Y - line.P1.Y; double vxp1 = x - line.P1.X; double vyp1 = y - line.P1.Y; double projectionFactor = (vxp1 * vxline + vyp1 * vyline) / (vxline * vxline + vyline * vyline); if (projectionFactor < 0) { // Point projected on line before P1, use distance to P1 double dx = line.P1.X - x; double dy = line.P1.Y - y; return(dx * dx + dy * dy); } if (projectionFactor > 1) { // Point projected on line after P2, use distance to P2 double dx = line.P2.X - x; double dy = line.P2.Y - y; return(dx * dx + dy * dy); } { // Point projected between P1 and P2, calculate distance to projection point double xproj = line.P1.X + projectionFactor * vxline; double yproj = line.P1.Y + projectionFactor * vyline; double dx = xproj - x; double dy = yproj - y; return(dx * dx + dy * dy); } }
/// <summary> /// Checks if the lines lineA and lineB shares a point either as a real /// crossing point or as a shared end point or a end point of the one /// line being in the other line. /// </summary> /// <param name="Linea">Line.</param> /// <param name="Lineb">Line.</param> /// <param name="intersectionPoint">Point.</param> /// <returns> /// <p>True if lineA and lineB has shared point. False otherwise</p> /// <p>The shared point if any is returned in the intersectionPoint /// parameter that is called by reference</p> /// </returns> protected static bool IntersectionPoint(XYLine Linea, XYLine Lineb, ref XYPoint intersectionPoint) { if (DoLineSegmentsIntersect(Linea, Lineb)) { intersectionPoint = CalculateIntersectionPoint(Linea, Lineb); return(true); } if (IsPointInLine(Linea.P2, Lineb)) { intersectionPoint = Linea.P2; return(true); } if (IsPointInLine(Lineb.P2, Linea)) { intersectionPoint = Lineb.P2; return(true); } if (IsPointInLine(Lineb.P1, Linea)) { intersectionPoint = Lineb.P1; return(true); } if (IsPointInLine(Linea.P1, Lineb)) { intersectionPoint = Linea.P1; return(true); } return(false); }
/// <summary> /// Constructor. Copies input line. /// </summary> /// <param name="line">Line to copy</param> public XYLine(XYLine line) { P1 = new XYPoint(); P2 = new XYPoint(); P1.X = line.P1.X; P1.Y = line.P1.Y; P2.X = line.P2.X; P2.Y = line.P2.Y; }
/// <summary> /// Constructor. Copies input line. /// </summary> /// <param name="line">Line to copy</param> public XYLine(XYLine line) { _p1 = new XYPoint(); _p2 = new XYPoint(); _p1.X = line.P1.X; _p1.Y = line.P1.Y; _p2.X = line.P2.X; _p2.Y = line.P2.Y; }
public void Equals() { XYPoint p1 = new XYPoint(2,3); XYPoint p2 = new XYPoint(2,3); XYPoint p3 = new XYPoint(2,-3); XYLine l1 = new XYLine(2,3,3,4); Assert.AreEqual(true, p1.Equals(p1),"Test1"); Assert.AreEqual(true, p1.Equals(p2),"Test2"); Assert.AreEqual(false, p1.Equals(p3),"Test3"); Assert.AreEqual(false, p1.Equals(l1),"Test4"); }
/// <summary> /// Calculates the length of polyline inside polygon. Lines segments on the edges of /// polygons are included with half their length. /// </summary> /// <param name="polyline">Polyline</param> /// <param name="polygon">Polygon</param> /// <returns> /// Length of polyline inside polygon. /// </returns> public static double CalculateLengthOfPolylineInsidePolygon(XYPolyline polyline, XYPolygon polygon) { double lengthInside = 0; int numberOfLineSegments = polyline.Points.Count - 1; for (int i = 0; i < numberOfLineSegments; i++) { XYLine line = new XYLine(polyline.GetLine(i)); lengthInside += CalculateLengthOfLineInsidePolygon(line, polygon); } return(lengthInside); }
public void Equals() { XYLine l1 = new XYLine(0, 3, 3, 0); XYLine l2 = new XYLine(0, 3, 3, 0); XYLine l3 = new XYLine(3, 3, 3, 0); XYPolyline pl1 = new XYPolyline(); pl1.Points.Add(new XYPoint(0, 3)); pl1.Points.Add(new XYPoint(3, 0)); Assert.AreEqual(true, l1.Equals(l1),"Test1"); Assert.AreEqual(true, l1.Equals(l2),"Test2"); Assert.AreEqual(false, l1.Equals(l3),"Test3"); Assert.AreEqual(false, l1.Equals(pl1),"Test4"); }
public void ConstructionTest() { XYLine xyLine1 = new XYLine(1,2,3,4); Assert.AreEqual((double) 1, xyLine1.P1.X); Assert.AreEqual((double) 2, xyLine1.P1.Y); Assert.AreEqual((double) 3, xyLine1.P2.X); Assert.AreEqual((double) 4, xyLine1.P2.Y); XYLine xyLine2 = new XYLine(xyLine1); Assert.AreEqual(xyLine1.P1.X, xyLine2.P1.X); Assert.AreEqual(xyLine1.P1.Y, xyLine2.P1.Y); Assert.AreEqual(xyLine1.P2.X, xyLine2.P2.X); Assert.AreEqual(xyLine1.P2.Y, xyLine2.P2.Y); XYLine xyLine3 = new XYLine(new XYPoint(1,2),new XYPoint(3,4)); Assert.AreEqual((double) 1, xyLine3.P1.X); Assert.AreEqual((double) 2, xyLine3.P1.Y); Assert.AreEqual((double) 3, xyLine3.P2.X); Assert.AreEqual((double) 4, xyLine3.P2.Y); }
/// <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> /// Determines if a point is included in a line either in the interior /// or as one of the end points. /// </summary> /// <param name="x">x-coordinate</param> /// <param name="y">y-coordinate</param> /// <param name="line">Line.</param> /// <returns> /// <p>Determines if a point is included in a line.</p> /// </returns> protected static bool IsPointInLine(double x, double y, XYLine line) { return(PointLineDistanceSq(x, y, line) < EPSILON * EPSILON); }
/// <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> /// Calculates the distance from a polyline to a point in the plane. /// The algorithm decides weather the point lies besides the line /// segment in which case the distance is the length along a line /// perpendicular to the line. Alternatively the distance is the /// smallest of the distances to either endpoint. /// </summary> /// <param name="line">Line</param> /// <param name="point">Point</param> /// <returns> /// <p>Length of the shortest path between the line and the point.</p> /// </returns> protected static double CalculateLineToPointDistance (XYLine line, XYPoint point) { double dist = 0; double a = Math.Sqrt((line.P2.X-point.X)*(line.P2.X-point.X) + (line.P2.Y-point.Y)*(line.P2.Y-point.Y)); double b = Math.Sqrt((line.P2.X-line.P1.X)*(line.P2.X-line.P1.X)+(line.P2.Y-line.P1.Y)*(line.P2.Y-line.P1.Y)); double c = Math.Sqrt((line.P1.X-point.X)*(line.P1.X-point.X)+(line.P1.Y-point.Y)*(line.P1.Y-point.Y)); if ((a == 0) || (c == 0)) { dist = 0; } else if (b == 0) { dist = a; } else { double alpha = Math.Acos((b*b+c*c-a*a)/(2*b*c)); double beta = Math.Acos((a*a+b*b-c*c)/(2*a*b)); if (Math.Max(alpha,beta)<Math.PI/2) { dist = Math.Abs((line.P2.X-line.P1.X)*(line.P1.Y-point.Y)-(line.P1.X-point.X)*(line.P2.Y-line.P1.Y))/b; } else { dist = Math.Min(a, c); } } return dist; }
/// <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 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 { XYPoint P1A = new XYPoint(); XYPoint P2A = new XYPoint(); if (lineA.P1.X < lineA.P2.X) { P1A = lineA.P1; P2A = lineA.P2; } else { P1A = lineA.P2; P2A = lineA.P1; } XYPoint P1B = new XYPoint(); XYPoint 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); } } }
/// <summary> /// OverLoad of DoLineSegmentsIntersect(x1, y1, x2, y2, x3, y3, x4, y4) /// </summary> /// <param name="line1">First line</param> /// <param name="line2">Second line</param> /// <returns>true if the line segmenst intersects otherwise false</returns> public static bool DoLineSegmentsIntersect(XYLine line1, XYLine line2) { return(DoLineSegmentsIntersect(line1.P1.X, line1.P1.Y, line1.P2.X, line1.P2.Y, line2.P1.X, line2.P1.Y, line2.P2.X, line2.P2.Y)); }
public static double ACalculateSharedLength(XYLine lineA, XYLine lineB) { return CalculateSharedLength(lineA, lineB); }
/// <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 { XYPoint P1A = new XYPoint(); XYPoint P2A = new XYPoint(); if (lineA.P1.X < lineA.P2.X) { P1A = lineA.P1; P2A = lineA.P2; } else { P1A = lineA.P2; P2A = lineA.P1; } XYPoint P1B = new XYPoint(); XYPoint 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; } } }
/// <summary> /// Calculates the length of polyline inside polygon. Lines segments on the edges of /// polygons are included with half their length. /// </summary> /// <param name="polyline">Polyline</param> /// <param name="polygon">Polygon</param> /// <returns> /// Length of polyline inside polygon. /// </returns> public static double CalculateLengthOfPolylineInsidePolygon(XYPolyline polyline, XYPolygon polygon) { double lengthInside = 0; int numberOfLineSegments = polyline.Points.Count - 1; for (int i = 0; i < numberOfLineSegments; i++) { XYLine line = new XYLine(polyline.GetLine(i)); lengthInside += CalculateLengthOfLineInsidePolygon(line,polygon); } return lengthInside; }
/// <summary> /// OverLoad of CalculateIntersectionPoint(XYPoint p1, XYPoint p2, XYPoint p3, XYPoint p4) /// </summary> /// <param name="line1">First line</param> /// <param name="line2">Second line</param> /// <returns>Intersection point</returns> public static XYPoint CalculateIntersectionPoint(XYLine line1, XYLine line2) { return CalculateIntersectionPoint(line1.P1, line1.P2, line2.P1, line2.P2); }
/// <summary> /// OverLoad of DoLineSegmentsIntersect(x1, y1, x2, y2, x3, y3, x4, y4) /// </summary> /// <param name="line1">First line</param> /// <param name="line2">Second line</param> /// <returns>true if the line segmenst intersects otherwise false</returns> public static bool DoLineSegmentsIntersect(XYLine line1, XYLine line2) { return DoLineSegmentsIntersect(line1.P1.X, line1.P1.Y, line1.P2.X, line1.P2.Y, line2.P1.X, line2.P1.Y, line2.P2.X, line2.P2.Y); }
public static bool AIntersectionPoint(XYLine lineA, XYLine lineB, ref XYPoint intersectionPoint) { return IntersectionPoint(lineA, lineB, ref intersectionPoint); }
/// <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); } }
public static double ACalculateLengthOfLineInsidePolygon(XYLine line, XYPolygon polygon) { return CalculateLengthOfLineInsidePolygon(line, polygon); }
public static double ACalculateLineToPointDistance(XYLine line, XYPoint point) { return CalculateLineToPointDistance(line, point); }
/// <summary> /// Determines if a point is included in a line either in the interior /// or as one of the end points. /// </summary> /// <param name="x">x-coordinate</param> /// <param name="y">y-coordinate</param> /// <param name="line">Line.</param> /// <returns> /// <p>Determines if a point is included in a line.</p> /// </returns> protected static bool IsPointInLine(double x, double y, XYLine line) { bool result = false; if( line.P1.X-line.P2.X != 0 ) { if ((x >= Math.Min(line.P1.X, line.P2.X)) && (x <= Math.Max(line.P1.X, line.P2.X))) { if( Math.Abs(y-line.P1.Y-(line.P2.Y-line.P1.Y)/(line.P1.X-line.P2.X)*(line.P1.X-x)) < EPSILON*EPSILON) { result = true; } } } else { if (line.P1.X == x) { if ( (y >= Math.Min(line.P1.Y, line.P2.Y)) && (y <= Math.Max(line.P1.Y, line.P2.Y)) ) { result = true; } } } return result; }
/// <summary> /// OverLoad of CalculateIntersectionPoint(XYPoint p1, XYPoint p2, XYPoint p3, XYPoint p4) /// </summary> /// <param name="line1">First line</param> /// <param name="line2">Second line</param> /// <returns>Intersection point</returns> public static XYPoint CalculateIntersectionPoint(XYLine line1, XYLine line2) { return(CalculateIntersectionPointWithTranslation(line1.P1, line1.P2, line2.P1, line2.P2)); }
/// <summary> /// Determines if a point is included in a lines interior. I.e. included /// in the line and not an endpoint. /// </summary> /// <param name="x">x-coordinate</param> /// <param name="y">y-coordinate</param> /// <param name="line">Line.</param> /// <returns> /// <p>Determines if a point is included in a line.</p> /// </returns> protected static bool IsPointInLineInterior(double x, double y, XYLine line) { bool result = false; if( line.P1.X-line.P2.X != 0 ) //line is not vertical { if ((x > Math.Min(line.P1.X, line.P2.X)) && (x < Math.Max(line.P1.X, line.P2.X))) { if( Math.Abs(y-line.P1.Y-(line.P2.Y-line.P1.Y)/(line.P1.X-line.P2.X)*(line.P1.X-x)) < EPSILON*EPSILON) { result = true; } } } else //line is vertical { if (line.P1.X == x) { if ( (y > Math.Min(line.P1.Y, line.P2.Y)) && (y < Math.Max(line.P1.Y, line.P2.Y)) ) { result = true; } } } return result; }
/// <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 is included in a lines interior. I.e. included /// in the line and not an endpoint. /// <p>Overload to:IsPointInLineInterior(double x, double y, XYLine line)</p> /// </summary> /// <param name="point">Point.</param> /// <param name="line">Line.</param> /// <returns> /// <p>Determines if a point is included in a line.</p> /// </returns> protected static bool IsPointInLineInterior(XYPoint point, XYLine line) { return IsPointInLineInterior( point.X, point.Y, line); }
/// <summary> /// Determines if a point is included in a lines interior. I.e. included /// in the line and not an endpoint. /// <p>Overload to:IsPointInLineInterior(double x, double y, XYLine line)</p> /// </summary> /// <param name="point">Point.</param> /// <param name="line">Line.</param> /// <returns> /// <p>Determines if a point is included in a line.</p> /// </returns> protected static bool IsPointInLineInterior(XYPoint point, XYLine line) { return(IsPointInLineInterior(point.X, point.Y, line)); }
public static bool AIsPointInLineInterior(XYPoint point, XYLine line) { return IsPointInLineInterior(point, line); }
/// <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; }
/// <summary> /// Checks if the lines lineA and lineB shares a point either as a real /// crossing point or as a shared end point or a end point of the one /// line being in the other line. /// </summary> /// <param name="Linea">Line.</param> /// <param name="Lineb">Line.</param> /// <param name="intersectionPoint">Point.</param> /// <returns> /// <p>True if lineA and lineB has shared point. False otherwise</p> /// <p>The shared point if any is returned in the intersectionPoint /// parameter that is called by reference</p> /// </returns> protected static bool IntersectionPoint(XYLine Linea, XYLine Lineb, ref XYPoint intersectionPoint) { if( DoLineSegmentsIntersect(Linea, Lineb)) { intersectionPoint = CalculateIntersectionPoint(Linea, Lineb); return true; } if( IsPointInLine(Linea.P2, Lineb)) { intersectionPoint = Linea.P2; return true; } if( IsPointInLine(Lineb.P2, Linea)) { intersectionPoint = Lineb.P2; return true; } if( IsPointInLine(Lineb.P1, Linea)) { intersectionPoint = Lineb.P1; return true; } if( IsPointInLine(Linea.P1, Lineb)) { intersectionPoint = Linea.P1; return true; } return false; }