Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/Routines2D.cs =================================================================== diff -u -r4897 -r5028 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/Routines2D.cs (.../Routines2D.cs) (revision 4897) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/Routines2D.cs (.../Routines2D.cs) (revision 5028) @@ -586,7 +586,7 @@ /// X-coordinate of the second point /// Y-coordinate of the second point /// The distance between the given points. - private static double Compute2DDistance(double aX1, double aY1, double aX2, double aY2) + public static double Compute2DDistance(double aX1, double aY1, double aX2, double aY2) { double lX = aX1 - aX2; double lY = aY1 - aY2; @@ -764,4 +764,70 @@ public double Y { get; set; } public double Z { get; set; } } + + public static double CalculateDistanceToLine( + double pointX, + double pointY, + double segmentStartX, + double segmentStartY, + double segmentEndX, + double segmentEndY) + { + return Math.Sqrt(CalculateSquaredDistance(new Point2D(pointX, pointY), new Point2D(segmentStartX, segmentStartY), + new Point2D(segmentEndX, segmentEndY))); + } + + private static double CalculateSquaredDistance(Point2D point, Point2D segmentPoint1, Point2D segmentPoint2) + { + Point2D point2D1 = segmentPoint2 - segmentPoint1; + Point2D point2D2 = point - segmentPoint1; + Func func = (first, second) => first.X * second.X + first.Z * second.Z; + double num1 = func(point2D2, point2D1); + double num2 = func(point2D1, point2D1); + Point2D point2D3; + if (num1 <= 0.0) + point2D3 = point - segmentPoint1; + else if (num2 <= num1) + { + point2D3 = point - segmentPoint2; + } + else + { + double num3 = num1 / num2; + Point2D point2D4 = segmentPoint1 + num3 * point2D1; + point2D3 = point - point2D4; + } + return func(point2D3, point2D3); + } + + public static void GetPointOnLineClosestTo( + double aPointX, + double aPointY, + double aLine1X, + double aLine1Y, + double aLine2X, + double aLine2Y, + out double aResultX, + out double aResultY) + { + double x1 = Compute2DDistance(aLine1X, aLine1Y, aLine2X, aLine2Y); + double x2 = Compute2DDistance(aLine1X, aLine1Y, aPointX, aPointY); + double x3 = Compute2DDistance(aLine2X, aLine2Y, aPointX, aPointY); + double num = (Math.Pow(x2, 2.0) - Math.Pow(x3, 2.0) + Math.Pow(x1, 2.0)) / (2.0 * x1); + if (num <= 0.0) + { + aResultX = aLine1X; + aResultY = aLine1Y; + } + else if (num >= x1) + { + aResultX = aLine2X; + aResultY = aLine2Y; + } + else + { + aResultX = aLine1X + num / x1 * (aLine2X - aLine1X); + aResultY = aLine1Y + num / x1 * (aLine2Y - aLine1Y); + } + } } \ No newline at end of file