Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/Routines2D.cs =================================================================== diff -u -r6245 -r6404 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/Routines2D.cs (.../Routines2D.cs) (revision 6245) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Geometry/Routines2D.cs (.../Routines2D.cs) (revision 6404) @@ -1,4 +1,4 @@ -// Copyright (C) Stichting Deltares 2024. All rights reserved. +// Copyright (C) Stichting Deltares 2025. All rights reserved. // // This file is part of the Dam Engine. // @@ -127,14 +127,14 @@ { return true; } - + double linePoint1X = linePoint1.X; double linePoint1Z = linePoint1.Z; double linePoint2X = linePoint2.X; double linePoint2Z = linePoint2.Z; double pointX = point.X; double pointZ = point.Z; - + // then check if point is within the bounding rectangle formed by 2 line points double maxX = Math.Max(linePoint1X, linePoint2X); double minX = Math.Min(linePoint1X, linePoint2X); @@ -520,6 +520,44 @@ return false; } + /// + /// Calculates the Euclidean distance between two points on a 2-dimensional plane + /// + /// X-coordinate of the first point + /// Y-coordinate of the first point + /// X-coordinate of the second point + /// Y-coordinate of the second point + /// The distance between the given points. + public static double Compute2DDistance(double aX1, double aY1, double aX2, double aY2) + { + double lX = aX1 - aX2; + double lY = aY1 - aY2; + + return Math.Sqrt(lX * lX + lY * lY); + } + + /// + /// Calculates the distance between a point and a line segment. + /// + /// + /// + /// + /// + /// + /// + /// + public static double CalculateDistanceToLine( + double pointX, + double pointY, + double lineStartX, + double lineStartY, + double lineEndX, + double lineEndY) + { + return Math.Sqrt(CalculateSquaredDistance(new Point2D(pointX, pointY), new Point2D(lineStartX, lineStartY), + new Point2D(lineEndX, lineEndY))); + } + private static void UndoAddIfNeeded(GeometryLoop polygon, bool needed) { if (needed) @@ -579,22 +617,6 @@ } /// - /// Calculates the Euclidean distance between two points on a 2-dimensional plane - /// - /// X-coordinate of the first point - /// Y-coordinate of the first point - /// X-coordinate of the second point - /// Y-coordinate of the second point - /// The distance between the given points. - public static double Compute2DDistance(double aX1, double aY1, double aX2, double aY2) - { - double lX = aX1 - aX2; - double lY = aY1 - aY2; - - return Math.Sqrt(lX * lX + lY * lY); - } - - /// /// Checks if the 2D Lines strictly intersect. Strictly means that the intersection point must be part of both lines so /// extrapolated points do not count. /// @@ -758,58 +780,39 @@ } } - /// - /// Structure for line constant in 3D - /// - public struct LineConstant - { - public double X { get; set; } - public double Y { get; set; } - public double Z { get; set; } - } - - /// - /// Calculates the distance between a point and a line segment. - /// - /// - /// - /// - /// - /// - /// - /// - public static double CalculateDistanceToLine( - double pointX, - double pointY, - double lineStartX, - double lineStartY, - double lineEndX, - double lineEndY) - { - return Math.Sqrt(CalculateSquaredDistance(new Point2D(pointX, pointY), new Point2D(lineStartX, lineStartY), - new Point2D(lineEndX, lineEndY))); - } - private static double CalculateSquaredDistance(Point2D point, Point2D lineStart, Point2D lineEnd) { - Point2D distanceVectorLineStartToEnd = lineEnd - lineStart; + Point2D distanceVectorLineStartToEnd = lineEnd - lineStart; Point2D distanceVectorLineStartToPoint = point - lineStart; double DotProduct(Point2D first, Point2D second) => first.X * second.X + first.Z * second.Z; - double dotProductLineStartAndEnd = DotProduct(distanceVectorLineStartToPoint, distanceVectorLineStartToEnd ); - double dotProductPointAndLineStart = DotProduct(distanceVectorLineStartToEnd , distanceVectorLineStartToEnd ); + double dotProductLineStartAndEnd = DotProduct(distanceVectorLineStartToPoint, distanceVectorLineStartToEnd); + double dotProductPointAndLineStart = DotProduct(distanceVectorLineStartToEnd, distanceVectorLineStartToEnd); Point2D point2D3; if (dotProductLineStartAndEnd <= 0.0) + { point2D3 = point - lineStart; + } else if (dotProductPointAndLineStart <= dotProductLineStartAndEnd) { point2D3 = point - lineEnd; } else { double vectorToClosestPointOnLine = dotProductLineStartAndEnd / dotProductPointAndLineStart; - Point2D point2D4 = lineStart + vectorToClosestPointOnLine * distanceVectorLineStartToEnd ; + Point2D point2D4 = lineStart + vectorToClosestPointOnLine * distanceVectorLineStartToEnd; point2D3 = point - point2D4; } + return DotProduct(point2D3, point2D3); } + + /// + /// Structure for line constant in 3D + /// + public struct LineConstant + { + public double X { get; set; } + public double Y { get; set; } + public double Z { get; set; } + } } \ No newline at end of file