Skip to content

Commit

Permalink
16.3: Fixing intersection issue on parallel lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Gayle McDowell committed Nov 13, 2018
1 parent f88b754 commit 59018cf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 52 deletions.
21 changes: 6 additions & 15 deletions Java/Ch 16. Moderate/Q16_03_Intersection/Line.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
}
8 changes: 1 addition & 7 deletions Java/Ch 16. Moderate/Q16_03_Intersection/Point.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
44 changes: 16 additions & 28 deletions Java/Ch 16. Moderate/Q16_03_Intersection/Question.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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. */
Expand All @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions Java/Ch 16. Moderate/Q16_03_Intersection/Tester.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

public class Tester {



public static boolean equalish(double a, double b) {
return Math.abs(a - b) < .001;
}
Expand Down

0 comments on commit 59018cf

Please sign in to comment.