Interval2D.java
5.84 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/******************************************************************************
* Compilation: javac Interval2D.java
* Execution: java Interval2D
* Dependencies: StdOut.java Interval1D.java StdDraw.java
*
* 2-dimensional interval data type.
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code Interval2D} class represents a closed two-dimensional interval,
* which represents all points (x, y) with both {@code xmin <= x <= xmax} and
* {@code ymin <= y <= ymax}.
* Two-dimensional intervals are immutable: their values cannot be changed
* after they are created.
* The class {@code Interval2D} includes methods for checking whether
* a two-dimensional interval contains a point and determining whether
* two two-dimensional intervals intersect.
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/12oop">Section 1.2</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Interval2D {
private final Interval1D x;
private final Interval1D y;
/**
* Initializes a two-dimensional interval.
* @param x the one-dimensional interval of x-coordinates
* @param y the one-dimensional interval of y-coordinates
*/
public Interval2D(Interval1D x, Interval1D y) {
this.x = x;
this.y = y;
}
/**
* Does this two-dimensional interval intersect that two-dimensional interval?
* @param that the other two-dimensional interval
* @return true if this two-dimensional interval intersects
* that two-dimensional interval; false otherwise
*/
public boolean intersects(Interval2D that) {
if (!this.x.intersects(that.x)) return false;
if (!this.y.intersects(that.y)) return false;
return true;
}
/**
* Does this two-dimensional interval contain the point p?
* @param p the two-dimensional point
* @return true if this two-dimensional interval contains the point p; false otherwise
*/
public boolean contains(Point2D p) {
return x.contains(p.x()) && y.contains(p.y());
}
/**
* Returns the area of this two-dimensional interval.
* @return the area of this two-dimensional interval
*/
public double area() {
return x.length() * y.length();
}
/**
* Returns a string representation of this two-dimensional interval.
* @return a string representation of this two-dimensional interval
* in the form [xmin, xmax] x [ymin, ymax]
*/
public String toString() {
return x + " x " + y;
}
/**
* Does this interval equal the other interval?
* @param other the other interval
* @return true if this interval equals the other interval; false otherwise
*/
public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (other.getClass() != this.getClass()) return false;
Interval2D that = (Interval2D) other;
return this.x.equals(that.x) && this.y.equals(that.y);
}
/**
* Returns an integer hash code for this interval.
* @return an integer hash code for this interval
*/
public int hashCode() {
int hash1 = x.hashCode();
int hash2 = y.hashCode();
return 31*hash1 + hash2;
}
/**
* Draws this two-dimensional interval to standard draw.
*/
public void draw() {
double xc = (x.min() + x.max()) / 2.0;
double yc = (y.min() + y.max()) / 2.0;
StdDraw.rectangle(xc, yc, x.length() / 2.0, y.length() / 2.0);
}
/**
* Unit tests the {@code Interval2D} data type.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
double xmin = Double.parseDouble(args[0]);
double xmax = Double.parseDouble(args[1]);
double ymin = Double.parseDouble(args[2]);
double ymax = Double.parseDouble(args[3]);
int trials = Integer.parseInt(args[4]);
Interval1D xInterval = new Interval1D(xmin, xmax);
Interval1D yInterval = new Interval1D(ymin, ymax);
Interval2D box = new Interval2D(xInterval, yInterval);
box.draw();
Counter counter = new Counter("hits");
for (int t = 0; t < trials; t++) {
double x = StdRandom.uniform(0.0, 1.0);
double y = StdRandom.uniform(0.0, 1.0);
Point2D point = new Point2D(x, y);
if (box.contains(point)) counter.increment();
else point.draw();
}
StdOut.println(counter);
StdOut.printf("box area = %.2f\n", box.area());
}
}
/******************************************************************************
* Copyright 2002-2016, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/