/
ClosedInterval.java
57 lines (42 loc) · 1.43 KB
/
ClosedInterval.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
package usantatecla.closedInterval.v2.crossTest;
public class ClosedInterval {
private double min;
private double max;
public ClosedInterval(double min, double max) {
this.min = min;
this.max = max;
}
public double getLength() {
return this.max - this.min;
}
public double getMiddlePoint() {
return this.min + this.getLength() / 2;
}
public ClosedInterval shifted(double value) {
return new ClosedInterval(this.min + value, this.max + value);
}
public boolean includes(double value) {
return this.min <= value && value <= this.max;
}
public boolean includes(ClosedInterval closedInterval) {
assert closedInterval != null;
return this.includes(closedInterval.min)
&& this.includes(closedInterval.max);
}
public boolean intersected(ClosedInterval closedInterval) {
assert closedInterval != null;
return this.includes(closedInterval.min)
|| this.includes(closedInterval.max)
|| closedInterval.includes(this);
}
public ClosedInterval intersection(ClosedInterval closedInterval) {
assert closedInterval != null;
assert this.intersected(closedInterval);
return new ClosedInterval(Math.max(min, closedInterval.min), Math.min(max, closedInterval.max));
}
public ClosedInterval union(ClosedInterval closedInterval) {
assert closedInterval != null;
assert this.intersected(closedInterval);
return new ClosedInterval(Math.min(min, closedInterval.min), Math.max(max, closedInterval.max));
}
}