From 59018cfcb90292209275db1c4b3ed306d4b07d7f Mon Sep 17 00:00:00 2001 From: Gayle McDowell Date: Mon, 12 Nov 2018 16:25:11 -0800 Subject: [PATCH] 16.3: Fixing intersection issue on parallel lines --- .../Q16_03_Intersection/Line.java | 21 +++------ .../Q16_03_Intersection/Point.java | 8 +--- .../Q16_03_Intersection/Question.java | 44 +++++++------------ .../Q16_03_Intersection/Tester.java | 2 - 4 files changed, 23 insertions(+), 52 deletions(-) diff --git a/Java/Ch 16. Moderate/Q16_03_Intersection/Line.java b/Java/Ch 16. Moderate/Q16_03_Intersection/Line.java index fe814874..b701677f 100644 --- a/Java/Ch 16. Moderate/Q16_03_Intersection/Line.java +++ b/Java/Ch 16. Moderate/Q16_03_Intersection/Line.java @@ -1,26 +1,24 @@ package Q16_03_Intersection; + public class Line { - public double slope; - public double yintercept; - public Point start; - public Point end; + public double slope, yintercept; + public Point start, end; public Line(Point start, Point end) { this.start = start; this.end = end; if (start.x == end.x) { slope = Double.POSITIVE_INFINITY; + yintercept = Double.POSITIVE_INFINITY; } else { - double deltaY = end.y - start.y; - double deltaX = end.x - start.x; - slope = deltaY / deltaX; + slope = (end.y - start.y) / (end.x - start.x); yintercept = end.y - slope * end.x; } } public boolean isVertical() { - return slope == Double.POSITIVE_INFINITY || slope == Double.NEGATIVE_INFINITY; + return slope == Double.POSITIVE_INFINITY; } @Override @@ -34,11 +32,4 @@ public double getYFromX(double x) { } return slope * x + yintercept; } - - public double getXFromY(double y) { - if (isVertical()) { - return start.x; - } - return (y - yintercept) / slope; - } } \ No newline at end of file diff --git a/Java/Ch 16. Moderate/Q16_03_Intersection/Point.java b/Java/Ch 16. Moderate/Q16_03_Intersection/Point.java index a07439bd..2cd3d810 100644 --- a/Java/Ch 16. Moderate/Q16_03_Intersection/Point.java +++ b/Java/Ch 16. Moderate/Q16_03_Intersection/Point.java @@ -1,17 +1,11 @@ package Q16_03_Intersection; public class Point { - public double x; - public double y; + public double x, y; public Point(double x, double y) { this.x = x; this.y = y; } - - public void setLocation(double x, double y) { - this.x = x; - this.y = y; - } @Override public String toString() { diff --git a/Java/Ch 16. Moderate/Q16_03_Intersection/Question.java b/Java/Ch 16. Moderate/Q16_03_Intersection/Question.java index b389498a..2b286b22 100644 --- a/Java/Ch 16. Moderate/Q16_03_Intersection/Question.java +++ b/Java/Ch 16. Moderate/Q16_03_Intersection/Question.java @@ -1,11 +1,6 @@ package Q16_03_Intersection; -import java.util.ArrayList; - public class Question { - public static Point createPoint(int[] coordinates) { - return new Point(coordinates[0], coordinates[1]); - } /* Checks if middle is between start and end. */ 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 Line line1 = new Line(start1, end1); Line line2 = new Line(start2, end2); - /* If the lines are parallel, then their extended lines must be both vertical OR have same y-intercept. + /* If the lines are parallel, then their extended lines must have same y-intercept. * If so, check that the start or end of one point is on the other line. */ if (line1.slope == line2.slope) { - /* Parallel lines can't intersect (unless they're the same line. */ - if (line1.yintercept != line2.yintercept && !line1.isVertical()) { + if (line1.yintercept != line2.yintercept) { return null; } - /* These are the same line. Check if the start or end of one lines up in the other.*/ - if (isBetween(start1, start2, end1)) { - return start2; - } else if (isBetween(start1, end2, end1)) { - return end2; - } else if (isBetween(start2, start1, end2)) { - return start1; - } else if (isBetween(start2, end1, end2)) { - return end1; - } else { - return null; - } + /* Check if the start or end of one line is in the other. If so, return that point*/ + if (isBetween(start1, start2, end1)) return start2; + else if (isBetween(start1, end2, end1)) return end2; + else if (isBetween(start2, start1, end2)) return start1; + else if (isBetween(start2, end1, end2)) return end1; + else return null; } /* Compute the intersection of the infinite lines, and then check if this falls within the * boundary of the line segments. Note that at most one line is vertical. */ /* Get intersection's x coordinate. If one is vertical, always use its x coordinate. - * Otherwise, compute the x coordinate based on setting each line's y = mx + b equation + * Otherwise, compute the intersection's x coordinate based on setting each line's y = mx + b equation * equal and solving for x. */ double x; 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 x = (line2.yintercept - line1.yintercept) / (line1.slope - line2.slope); } - /* Get insection's y coordinate using the non-vertical line. Note that if line1 is vertical - * then line 2 is not vertical. */ + /* Get insection's y coordinate using a non-vertical line. Note that if line1 is vertical + * then line 2 is not vertical (else it would have been caught earlier). */ double y = line1.isVertical() ? line2.getYFromX(x) : line1.getYFromX(x); /* We now have the intersection of the infinite lines. Check if it's within the boundaries - * of each line. */ + * of each line segment. */ Point intersection = new Point(x, y); if (isBetween(start1, intersection, end1) && isBetween(start2, intersection, end2)) { return intersection; @@ -77,10 +65,10 @@ public static Point intersection(Point start1, Point end1, Point start2, Point e public static void main(String[] args) { - Point s1 = new Point(5, 3); - Point e1 = new Point(5, 5); - Point s2 = new Point(5, 0); - Point e2 = new Point(5, 4); + Point s1 = new Point(2147000000, 1); + Point e1 = new Point(-2147000000, -1); + Point s2 = new Point(-10, 0); + Point e2 = new Point(0, 0); Point intersection = intersection(s1, e1, s2, e2); System.out.println("Line Segment 1: " + s1 + " to " + e1); System.out.println("Line Segment 2: " + s2 + " to " + e2); diff --git a/Java/Ch 16. Moderate/Q16_03_Intersection/Tester.java b/Java/Ch 16. Moderate/Q16_03_Intersection/Tester.java index 346eb048..72da4742 100644 --- a/Java/Ch 16. Moderate/Q16_03_Intersection/Tester.java +++ b/Java/Ch 16. Moderate/Q16_03_Intersection/Tester.java @@ -4,8 +4,6 @@ public class Tester { - - public static boolean equalish(double a, double b) { return Math.abs(a - b) < .001; }