-
Notifications
You must be signed in to change notification settings - Fork 857
Expand file tree
/
Copy pathSegment.java
More file actions
39 lines (33 loc) · 914 Bytes
/
Segment.java
File metadata and controls
39 lines (33 loc) · 914 Bytes
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
// Implement Segment class
public class Segment {
// Data fields
private MyPoint p1;
private MyPoint p2;
// Constructors
/** Creates a default segment with end points
* (0,0) and (1,0) */
public Segment() {
this(new MyPoint(0,0), new MyPoint(1,0));
}
/** Creates a segment with the specified end points */
public Segment(MyPoint p1, MyPoint p2) {
this.p1 = p1;
this.p2 = p2;
}
/** Returns end point p1 */
public MyPoint getP1() {
return p1;
}
/** Returns end point p2 */
public MyPoint getP2() {
return p2;
}
/** Returns true if the specified segment overlaps
* with this segment */
public boolean overlaps(Segment s) {
MyPoint q1 = s.getP1();
MyPoint q2 = s.getP2();
return new Triangle2D(p1,p2,q1).getSignedArea()*new Triangle2D(p1,p2,q2).getSignedArea() < 0 &&
new Triangle2D(q1,q2,p1).getSignedArea()*new Triangle2D(q1,q2,p2).getSignedArea() < 0;
}
}