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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* File: gtypes.h
* --------------
* This file defines classes for representing points, dimensions, and
* rectangles.
*/
#ifndef _gtypes_h
#define _gtypes_h
#include <iostream>
#include <string>
/*
* Class: GPoint
* -------------
* This class contains real-valued x and y fields. It is used to
* represent a location on the graphics plane.
*/
class GPoint {
public:
/*
* Constructor: GPoint
* Usage: GPoint origin;
* GPoint pt(x, y);
* -----------------------
* Creates a <code>GPoint</code> object with the specified <code>x</code>
* and <code>y</code> coordinates. If the coordinates are not supplied,
* the default constructor sets these fields to 0.
*/
GPoint();
GPoint(double x, double y);
/*
* Method: getX
* Usage: double x = pt.getX();
* ----------------------------
* Returns the x component of the point.
*/
double getX() const;
/*
* Method: getY
* Usage: double y = pt.getY();
* ----------------------------
* Returns the y component of the point.
*/
double getY() const;
/*
* Method: toString
* Usage: string str = pt.toString();
* ----------------------------------
* Converts the <code>GPoint</code> to a string in the form
* <code>"(</code><i>x</i><code>,</code> <i>y</i><code>)"</code>.
*/
std::string toString() const;
/* Private section */
/**********************************************************************/
/* Note: Everything below this point in this class is logically part */
/* of the implementation and should not be of interest to clients. */
/**********************************************************************/
private:
/* Instance variables */
double x; /* The x-coordinate of the point */
double y; /* The y-coordinate of the point */
/* Friend declarations */
friend bool operator==(const GPoint & p1, const GPoint & p2);
friend bool operator!=(const GPoint & p1, const GPoint & p2);
friend int hashCode(const GPoint & pt);
};
/*
* Class: GDimension
* -----------------
* This class contains real-valued width and height fields. It is used
* to indicate the size of a graphical object.
*/
class GDimension {
public:
/*
* Constructor: GDimension
* Usage: GDimension empty;
* GDimension dim(width, height);
* -------------------------------------
* Creates a <code>GDimension</code> object with the specified
* <code>width</code> and <code>height</code> coordinates. If the
* coordinates are not supplied, the default constructor sets these
* fields to 0.
*/
GDimension();
GDimension(double width, double height);
/*
* Method: getWidth
* Usage: double width = dim.getWidth();
* -------------------------------------
* Returns the width component of the <code>GDimension</code> object.
*/
double getWidth() const;
/*
* Method: getHeight
* Usage: double height = dim.getHeight();
* ---------------------------------------
* Returns the height component of the <code>GDimension</code> object.
*/
double getHeight() const;
/*
* Method: toString
* Usage: string str = dim.toString();
* -----------------------------------
* Converts the <code>GDimension</code> to a string in the form
* <code>"(</code><i>width</i><code>,</code> <i>height</i><code>)"</code>.
*/
std::string toString() const;
/* Private section */
/**********************************************************************/
/* Note: Everything below this point in this class is logically part */
/* of the implementation and should not be of interest to clients. */
/**********************************************************************/
private:
/* Instance variables */
double width; /* The width of the GDimension */
double height; /* The height of the GDimension */
/* Friend declarations */
friend bool operator==(const GDimension & d1, const GDimension & d2);
friend bool operator!=(const GDimension & d1, const GDimension & d2);
friend int hashCode(const GDimension & dim);
};
/*
* Class: GRectangle
* -----------------
* This type contains real-valued x, y, width, and height fields. It is
* used to represent the bounding box of a graphical object.
*/
class GRectangle {
public:
/*
* Constructor: GRectangle
* Usage: GRectangle empty;
* GRectangle r(x, y, width, height);
* -----------------------------------------
* Creates a <code>GRectangle</code> object with the specified components.
* If these parameters are not supplied, the default constructor sets
* these fields to 0.
*/
GRectangle();
GRectangle(double x, double y, double width, double height);
/*
* Method: getX
* Usage: double x = r.getX();
* ---------------------------
* Returns the x component of the rectangle.
*/
double getX() const;
/*
* Method: getY
* Usage: double y = pt.getY();
* ----------------------------
* Returns the y component of the rectangle.
*/
double getY() const;
/*
* Method: getWidth
* Usage: double width = r.getWidth();
* -----------------------------------
* Returns the width component of the rectangle.
*/
double getWidth() const;
/*
* Method: getHeight
* Usage: double height = pt.getHeight();
* --------------------------------------
* Returns the height component of the rectangle.
*/
double getHeight() const;
/*
* Method: isEmpty
* Usage: if (r.isEmpty()) ...
* ---------------------------
* Returns <code>true</code> if the rectangle is empty.
*/
bool isEmpty() const;
/*
* Method: contains
* Usage: if (r.contains(pt)) ...
* if (r.contains(x, y)) ...
* --------------------------------
* Returns <code>true</code> if the rectangle contains the given point,
* which may be specified either as a point or as distinct coordinates.
*/
bool contains(GPoint pt) const;
bool contains(double x, double y) const;
/*
* Method: toString
* Usage: string str = r.toString();
* ---------------------------------
* Converts the <code>GRectangle</code> to a string in the form
* <code>"(</code><i>x</i><code>,</code> <i>y</i><code>,</code>
* <i>width</i><code>,</code> <i>height</i><code>)"</code>.
*/
std::string toString() const;
/* Private section */
/**********************************************************************/
/* Note: Everything below this point in this class is logically part */
/* of the implementation and should not be of interest to clients. */
/**********************************************************************/
private:
/* Instance variables */
double x; /* The x-coordinate of the rectangle */
double y; /* The y-coordinate of the rectangle */
double width; /* The width of the rectangle */
double height; /* The height of the rectangle */
/* Friend declarations */
friend bool operator==(const GRectangle & r1, const GRectangle & r2);
friend bool operator!=(const GRectangle & r1, const GRectangle & r2);
friend int hashCode(const GRectangle & r);
};
/*
* Free functions
* --------------
* This section of the interface defines the insertion, comparison,
* and hashCode functions for the geometric types.
*/
std::ostream & operator<<(std::ostream & os, const GPoint & pt);
bool operator==(const GPoint & p1, const GPoint & p2);
bool operator!=(const GPoint & p1, const GPoint & p2);
int hashCode(const GPoint & pt);
std::ostream & operator<<(std::ostream & os, const GDimension & dim);
bool operator==(const GDimension & d1, const GDimension & d2);
bool operator!=(const GDimension & d1, const GDimension & d2);
int hashCode(const GDimension & dim);
std::ostream & operator<<(std::ostream & os, const GRectangle & rect);
bool operator==(const GRectangle & r1, const GRectangle & r2);
bool operator!=(const GRectangle & r1, const GRectangle & r2);
int hashCode(const GRectangle & r);
#endif
|