11using System ;
2- using System . Collections . Generic ;
3- using System . Linq ;
4- using System . Text ;
5- using System . Threading . Tasks ;
62
73namespace Advanced . Algorithms . Geometry
84{
@@ -28,8 +24,9 @@ public class LineIntersection
2824 /// </summary>
2925 /// <param name="lineA"></param>
3026 /// <param name="lineB"></param>
31- /// <returns></returns>
32- public static Point FindIntersection ( Line lineA , Line lineB )
27+ /// <param name="tolerance">precision tolerance.</param>
28+ /// <returns>The point of intersection.</returns>
29+ public static Point FindIntersection ( Line lineA , Line lineB , double tolerance = 0.001 )
3330 {
3431 double x1 = lineA . x1 , y1 = lineA . y1 ;
3532 double x2 = lineA . x2 , y2 = lineA . y2 ;
@@ -38,25 +35,25 @@ public static Point FindIntersection(Line lineA, Line lineB)
3835 double x4 = lineB . x2 , y4 = lineB . y2 ;
3936
4037 //equations of the form x=c (two vertical lines)
41- if ( x1 == x2 && x3 == x4 && x1 == x3 )
38+ if ( Math . Abs ( x1 - x2 ) < tolerance && Math . Abs ( x3 - x4 ) < tolerance && Math . Abs ( x1 - x3 ) < tolerance )
4239 {
4340 throw new Exception ( "Both lines overlap vertically, ambiguous intersection points." ) ;
4441 }
4542
4643 //equations of the form y=c (two horizontal lines)
47- if ( y1 == y2 && y3 == y4 && y1 == y3 )
44+ if ( Math . Abs ( y1 - y2 ) < tolerance && Math . Abs ( y3 - y4 ) < tolerance && Math . Abs ( y1 - y3 ) < tolerance )
4845 {
4946 throw new Exception ( "Both lines overlap horizontally, ambiguous intersection points." ) ;
5047 }
5148
5249 //equations of the form x=c (two vertical lines)
53- if ( x1 == x2 && x3 == x4 )
50+ if ( Math . Abs ( x1 - x2 ) < tolerance && Math . Abs ( x3 - x4 ) < tolerance )
5451 {
5552 return default ( Point ) ;
5653 }
5754
5855 //equations of the form y=c (two horizontal lines)
59- if ( y1 == y2 && y3 == y4 )
56+ if ( Math . Abs ( y1 - y2 ) < tolerance && Math . Abs ( y3 - y4 ) < tolerance )
6057 {
6158 return default ( Point ) ;
6259 }
@@ -72,11 +69,11 @@ public static Point FindIntersection(Line lineA, Line lineB)
7269 //-m2x + y = c2 --------(4)
7370
7471 double x , y ;
75-
72+
7673 //lineA is vertical x1 = x2
7774 //slope will be infinity
7875 //so lets derive another solution
79- if ( x1 == x2 )
76+ if ( Math . Abs ( x1 - x2 ) < tolerance )
8077 {
8178 //compute slope of line 2 (m2) and c2
8279 double m2 = ( y4 - y3 ) / ( x4 - x3 ) ;
@@ -92,7 +89,7 @@ public static Point FindIntersection(Line lineA, Line lineB)
9289 //lineB is vertical x3 = x4
9390 //slope will be infinity
9491 //so lets derive another solution
95- else if ( x3 == x4 )
92+ else if ( Math . Abs ( x3 - x4 ) < tolerance )
9693 {
9794 //compute slope of line 1 (m1) and c2
9895 double m1 = ( y2 - y1 ) / ( x2 - x1 ) ;
@@ -125,8 +122,8 @@ public static Point FindIntersection(Line lineA, Line lineB)
125122 //verify by plugging intersection point (x, y)
126123 //in orginal equations (1) & (2) to see if they intersect
127124 //otherwise x,y values will not be finite and will fail this check
128- if ( ! ( - m1 * x + y == c1
129- && - m2 * x + y == c2 ) )
125+ if ( ! ( Math . Abs ( - m1 * x + y - c1 ) < tolerance
126+ && Math . Abs ( - m2 * x + y - c2 ) < tolerance ) )
130127 {
131128 return default ( Point ) ;
132129 }
@@ -137,7 +134,7 @@ public static Point FindIntersection(Line lineA, Line lineB)
137134 if ( IsInsideLine ( lineA , x , y ) &&
138135 IsInsideLine ( lineB , x , y ) )
139136 {
140- return new Point ( ) { x = x , y = y } ;
137+ return new Point { x = x , y = y } ;
141138 }
142139
143140 //return default null (no intersection)
@@ -154,10 +151,10 @@ public static Point FindIntersection(Line lineA, Line lineB)
154151 /// <returns></returns>
155152 private static bool IsInsideLine ( Line line , double x , double y )
156153 {
157- return ( ( x >= line . x1 && x <= line . x2 )
158- || ( x >= line . x2 && x <= line . x1 ) )
159- && ( ( y >= line . y1 && y <= line . y2 )
160- || ( y >= line . y2 && y <= line . y1 ) ) ;
154+ return ( x >= line . x1 && x <= line . x2
155+ || x >= line . x2 && x <= line . x1 )
156+ && ( y >= line . y1 && y <= line . y2
157+ || y >= line . y2 && y <= line . y1 ) ;
161158 }
162159 }
163160}
0 commit comments