Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/Calculation/Math2D.cs =================================================================== diff -u -r602479eb3666493485aee246d56b08958a6fc958 -rf69d756f50ae1464a1a11f0780b6d6aa646f3114 --- Ringtoets/Piping/src/Ringtoets.Piping.IO/Calculation/Math2D.cs (.../Math2D.cs) (revision 602479eb3666493485aee246d56b08958a6fc958) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/Calculation/Math2D.cs (.../Math2D.cs) (revision f69d756f50ae1464a1a11f0780b6d6aa646f3114) @@ -1,5 +1,6 @@ using System; using Ringtoets.Piping.Data; +using Ringtoets.Piping.IO.Properties; namespace Ringtoets.Piping.IO.Calculation { @@ -11,36 +12,43 @@ /// /// Constant which is used to precision errors in comparisons. /// - public const double EpsilonForComparisons = 1e-8; + private const double epsilonForComparisons = 1e-8; /// - /// Determines the intersection point of a line which passes through the and - /// the ; and a line which passes through the - /// and the . When the lines are parallel, then null will be returned, - /// as no intersection point exists. + /// Determines the intersection point of a line which passes through the and + /// the ; and a line which passes through the + /// and the . /// - /// A which the first line passes through. - /// Another which the first line passes through. - /// A which the second line passes through. - /// Another which the second line passes through. + /// A which the first line passes through. + /// Another which the first line passes through. + /// A which the second line passes through. + /// Another which the second line passes through. /// An with coordinates at the point where the lines intersect. Or null when no /// intersection point exists (lines are parallel). /// /// Taken from: https://www.topcoder.com/community/data-science/data-science-tutorials/geometry-concepts-line-intersection-and-its-applications/ /// Based on https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection /// - public static Point2D LineIntersectionWithLine(Point2D firstPoint, Point2D secondPoint, Point2D thirdPoint, Point2D fourthPoint) + /// Thrown when equals or + /// equals , which makes it impossible to determine + /// a line through the points. + public static Point2D LineIntersectionWithLine(Point2D line1Point1, Point2D line1Point2, Point2D line2Point1, Point2D line2Point2) { - var aLine = secondPoint.Y - firstPoint.Y; - var bLine = firstPoint.X - secondPoint.X; - var cLine = aLine*firstPoint.X + bLine*firstPoint.Y; + if (line1Point1.Equals(line1Point2) || line2Point1.Equals(line2Point2)) + { + throw new ArgumentException(Resources.Math2D_LineIntersectionWithLine_Line_points_are_equal); + } - var aOtherLine = fourthPoint.Y - thirdPoint.Y; - var bOtherLine = thirdPoint.X - fourthPoint.X; - var cOtherLine = aOtherLine*thirdPoint.X + bOtherLine*thirdPoint.Y; + var aLine = line1Point2.Y - line1Point1.Y; + var bLine = line1Point1.X - line1Point2.X; + var cLine = aLine*line1Point1.X + bLine*line1Point1.Y; + var aOtherLine = line2Point2.Y - line2Point1.Y; + var bOtherLine = line2Point1.X - line2Point2.X; + var cOtherLine = aOtherLine*line2Point1.X + bOtherLine*line2Point1.Y; + var determinant = aLine*bOtherLine - aOtherLine*bLine; - if (Math.Abs(determinant) < EpsilonForComparisons) + if (Math.Abs(determinant) < epsilonForComparisons) { return null; }