1
1
package Q16_03_Intersection ;
2
2
3
- import java .util .ArrayList ;
4
-
5
3
public class Question {
6
- public static Point createPoint (int [] coordinates ) {
7
- return new Point (coordinates [0 ], coordinates [1 ]);
8
- }
9
4
10
5
/* Checks if middle is between start and end. */
11
6
public static boolean isBetween (double start , double middle , double end ) {
@@ -26,33 +21,26 @@ public static Point intersection(Point start1, Point end1, Point start2, Point e
26
21
Line line1 = new Line (start1 , end1 );
27
22
Line line2 = new Line (start2 , end2 );
28
23
29
- /* If the lines are parallel, then their extended lines must be both vertical OR have same y-intercept.
24
+ /* If the lines are parallel, then their extended lines must have same y-intercept.
30
25
* If so, check that the start or end of one point is on the other line. */
31
26
if (line1 .slope == line2 .slope ) {
32
- /* Parallel lines can't intersect (unless they're the same line. */
33
- if (line1 .yintercept != line2 .yintercept && !line1 .isVertical ()) {
27
+ if (line1 .yintercept != line2 .yintercept ) {
34
28
return null ;
35
29
}
36
30
37
- /* These are the same line. Check if the start or end of one lines up in the other.*/
38
- if (isBetween (start1 , start2 , end1 )) {
39
- return start2 ;
40
- } else if (isBetween (start1 , end2 , end1 )) {
41
- return end2 ;
42
- } else if (isBetween (start2 , start1 , end2 )) {
43
- return start1 ;
44
- } else if (isBetween (start2 , end1 , end2 )) {
45
- return end1 ;
46
- } else {
47
- return null ;
48
- }
31
+ /* Check if the start or end of one line is in the other. If so, return that point*/
32
+ if (isBetween (start1 , start2 , end1 )) return start2 ;
33
+ else if (isBetween (start1 , end2 , end1 )) return end2 ;
34
+ else if (isBetween (start2 , start1 , end2 )) return start1 ;
35
+ else if (isBetween (start2 , end1 , end2 )) return end1 ;
36
+ else return null ;
49
37
}
50
38
51
39
/* Compute the intersection of the infinite lines, and then check if this falls within the
52
40
* boundary of the line segments. Note that at most one line is vertical. */
53
41
54
42
/* Get intersection's x coordinate. If one is vertical, always use its x coordinate.
55
- * Otherwise, compute the x coordinate based on setting each line's y = mx + b equation
43
+ * Otherwise, compute the intersection's x coordinate based on setting each line's y = mx + b equation
56
44
* equal and solving for x. */
57
45
double x ;
58
46
if (line1 .isVertical () || line2 .isVertical ()) { /* If a line is vertical, use its x coordinate. */
@@ -61,12 +49,12 @@ public static Point intersection(Point start1, Point end1, Point start2, Point e
61
49
x = (line2 .yintercept - line1 .yintercept ) / (line1 .slope - line2 .slope );
62
50
}
63
51
64
- /* Get insection's y coordinate using the non-vertical line. Note that if line1 is vertical
65
- * then line 2 is not vertical. */
52
+ /* Get insection's y coordinate using a non-vertical line. Note that if line1 is vertical
53
+ * then line 2 is not vertical (else it would have been caught earlier) . */
66
54
double y = line1 .isVertical () ? line2 .getYFromX (x ) : line1 .getYFromX (x );
67
55
68
56
/* We now have the intersection of the infinite lines. Check if it's within the boundaries
69
- * of each line. */
57
+ * of each line segment . */
70
58
Point intersection = new Point (x , y );
71
59
if (isBetween (start1 , intersection , end1 ) && isBetween (start2 , intersection , end2 )) {
72
60
return intersection ;
@@ -77,10 +65,10 @@ public static Point intersection(Point start1, Point end1, Point start2, Point e
77
65
78
66
public static void main (String [] args ) {
79
67
80
- Point s1 = new Point (5 , 3 );
81
- Point e1 = new Point (5 , 5 );
82
- Point s2 = new Point (5 , 0 );
83
- Point e2 = new Point (5 , 4 );
68
+ Point s1 = new Point (2147000000 , 1 );
69
+ Point e1 = new Point (- 2147000000 , - 1 );
70
+ Point s2 = new Point (- 10 , 0 );
71
+ Point e2 = new Point (0 , 0 );
84
72
Point intersection = intersection (s1 , e1 , s2 , e2 );
85
73
System .out .println ("Line Segment 1: " + s1 + " to " + e1 );
86
74
System .out .println ("Line Segment 2: " + s2 + " to " + e2 );
0 commit comments