This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersection.java
81 lines (64 loc) · 1.55 KB
/
intersection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.*;
public class intersection {
public static final double EPS = 1e-6;
// Point 1.07 2.20 expected
public static void main(String[] args) {
Point p1 = new Point(0, 3);
Point p2 = new Point(4, 0);
Line l1 = new Line(p1, p2);
p1 = new Point(1, 2);
p2 = new Point(2, 5);
Line l2 = new Line(p1, p2);
if (l1.same(l2))
System.out.println("Co-linear");
else if (l1.parallel(l2))
System.out.println("None");
else
System.out.println(l1.intersect(l2));
}
private static class Point {
double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
public String toString() {
return String.format("Point %.2f %.2f", x, y);
}
}
private static class Line {
double a, b, c;
public Line(Point p1, Point p2) {
if (Math.abs(p1.x - p2.x) < EPS) {
this.a = 1;
this.b = 0.0;
this.c = -p1.x;
} else {
this.a = -(double) (p1.y - p2.y) / (p1.x - p2.x);
this.b = 1.0;
this.c = -(double) (this.a * p1.x) - p1.y;
}
}
boolean parallel(Line l) {
return Math.abs(this.a - l.a) < EPS &&
Math.abs(this.b - l.b) < EPS;
}
boolean same(Line l) {
return this.parallel(l) && Math.abs(this.c - l.c) < EPS;
}
Point intersect(Line l) {
if (this.parallel(l)) return null;
Point p = new Point();
p.x = (l.b * this.c - this.b * l.c) /
(l.a * this.b - this.a * l.b);
if (Math.abs(this.b) > EPS)
p.y = -(this.a * p.x + this.c);
else
p.y = -(l.a * p.x + l.c);
return p;
}
}
}