Skip to content

Commit 59018cf

Browse files
author
Gayle McDowell
committed
16.3: Fixing intersection issue on parallel lines
1 parent f88b754 commit 59018cf

File tree

4 files changed

+23
-52
lines changed

4 files changed

+23
-52
lines changed
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
package Q16_03_Intersection;
22

3+
34
public class Line {
4-
public double slope;
5-
public double yintercept;
6-
public Point start;
7-
public Point end;
5+
public double slope, yintercept;
6+
public Point start, end;
87

98
public Line(Point start, Point end) {
109
this.start = start;
1110
this.end = end;
1211
if (start.x == end.x) {
1312
slope = Double.POSITIVE_INFINITY;
13+
yintercept = Double.POSITIVE_INFINITY;
1414
} else {
15-
double deltaY = end.y - start.y;
16-
double deltaX = end.x - start.x;
17-
slope = deltaY / deltaX;
15+
slope = (end.y - start.y) / (end.x - start.x);
1816
yintercept = end.y - slope * end.x;
1917
}
2018
}
2119

2220
public boolean isVertical() {
23-
return slope == Double.POSITIVE_INFINITY || slope == Double.NEGATIVE_INFINITY;
21+
return slope == Double.POSITIVE_INFINITY;
2422
}
2523

2624
@Override
@@ -34,11 +32,4 @@ public double getYFromX(double x) {
3432
}
3533
return slope * x + yintercept;
3634
}
37-
38-
public double getXFromY(double y) {
39-
if (isVertical()) {
40-
return start.x;
41-
}
42-
return (y - yintercept) / slope;
43-
}
4435
}

Java/Ch 16. Moderate/Q16_03_Intersection/Point.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
package Q16_03_Intersection;
22

33
public class Point {
4-
public double x;
5-
public double y;
4+
public double x, y;
65
public Point(double x, double y) {
76
this.x = x;
87
this.y = y;
98
}
10-
11-
public void setLocation(double x, double y) {
12-
this.x = x;
13-
this.y = y;
14-
}
159

1610
@Override
1711
public String toString() {

Java/Ch 16. Moderate/Q16_03_Intersection/Question.java

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package Q16_03_Intersection;
22

3-
import java.util.ArrayList;
4-
53
public class Question {
6-
public static Point createPoint(int[] coordinates) {
7-
return new Point(coordinates[0], coordinates[1]);
8-
}
94

105
/* Checks if middle is between start and end. */
116
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
2621
Line line1 = new Line(start1, end1);
2722
Line line2 = new Line(start2, end2);
2823

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.
3025
* If so, check that the start or end of one point is on the other line. */
3126
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) {
3428
return null;
3529
}
3630

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;
4937
}
5038

5139
/* Compute the intersection of the infinite lines, and then check if this falls within the
5240
* boundary of the line segments. Note that at most one line is vertical. */
5341

5442
/* 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
5644
* equal and solving for x. */
5745
double x;
5846
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
6149
x = (line2.yintercept - line1.yintercept) / (line1.slope - line2.slope);
6250
}
6351

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). */
6654
double y = line1.isVertical() ? line2.getYFromX(x) : line1.getYFromX(x);
6755

6856
/* 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. */
7058
Point intersection = new Point(x, y);
7159
if (isBetween(start1, intersection, end1) && isBetween(start2, intersection, end2)) {
7260
return intersection;
@@ -77,10 +65,10 @@ public static Point intersection(Point start1, Point end1, Point start2, Point e
7765

7866
public static void main(String[] args) {
7967

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);
8472
Point intersection = intersection(s1, e1, s2, e2);
8573
System.out.println("Line Segment 1: " + s1 + " to " + e1);
8674
System.out.println("Line Segment 2: " + s2 + " to " + e2);

Java/Ch 16. Moderate/Q16_03_Intersection/Tester.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
public class Tester {
66

7-
8-
97
public static boolean equalish(double a, double b) {
108
return Math.abs(a - b) < .001;
119
}

0 commit comments

Comments
 (0)