-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.h
382 lines (320 loc) · 11.5 KB
/
graphics.h
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* File: graphics.h
* ----------------
* This interface provides access to a simple graphics library that makes
* it possible to draw lines, rectangles, ovals, arcs, polygons, images,
* and strings on a graphical window.
*/
#ifndef _graphics_h
#define _graphics_h
#include <string>
#include "gtypes.h"
#include "vector.h"
/*
* Function: initGraphics
* Usage: initGraphics();
* initGraphics(width, height);
* -----------------------------------
* Creates the graphics window on the screen. The first form creates a
* window with a default size of 500x300; the second allows the client to
* specify the size of the window. The call to initGraphics must precede
* any console output or calls to other functions in this interface.
*/
void initGraphics();
void initGraphics(int width, int height);
/*
* Function: drawArc
* Usage: drawArc(bounds, start, sweep);
* drawArc(x, y, width, height, start, sweep);
* --------------------------------------------------
* Draws an elliptical arc inscribed in a rectangle. The parameters x, y,
* width, and height (or, equivalently, the GRectangle bounds) specify the
* coordinates and dimensions of the bounding rectangle. The start
* parameter indicates the angle at which the arc begins and is measured in
* degrees counterclockwise from the +x axis. Thus, a start angle of 0
* indicates an arc that begins along the line running eastward from the
* center, a start angle of 135 begins along the line running northwest,
* and a start angle of -90 begins along the line running south. The sweep
* parameter indicates the extent of the arc and is also measured in
* degrees counterclockwise. A sweep angle of 90 defines a quarter circle
* extending counterclockwise from the start angle, and a sweep angle of
* -180 defines a semicircle extending clockwise.
*/
void drawArc(const GRectangle & bounds, double start, double sweep);
void drawArc(double x, double y, double width, double height,
double start, double sweep);
/*
* Function: fillArc
* Usage: fillArc(bounds, start, sweep);
* fillArc(x, y, width, height, start, sweep);
* --------------------------------------------------
* Fills a wedge-shaped area of an elliptical arc. The parameters are
* interpreted in the same way as those for drawArc.
*/
void fillArc(const GRectangle & bounds, double start, double sweep);
void fillArc(double x, double y, double width, double height,
double start, double sweep);
/*
* Function: drawImage
* Usage: drawImage(filename, pt);
* drawImage(filename, x, y);
* drawImage(filename, bounds);
* drawImage(filename, x, y, width, height);
* ------------------------------------------------
* Draws the image from the specified file with its upper left corner at
* the specified point. The forms of the call that include the bounds
* scale the image so that it fits inside the specified rectangle.
*/
void drawImage(std::string filename, const GPoint & pt);
void drawImage(std::string filename, double x, double y);
void drawImage(std::string filename, const GRectangle & bounds);
void drawImage(std::string filename, double x, double y,
double width, double height);
/*
* Function: getImageBounds
* Usage: GRectangle bounds = getImageBounds(filename);
* ----------------------------------------------------
* Returns the bounds of the image contained in the specified file. Only
* the width and height components of the rectangle are of interest; the x
* and y components are always 0.
*/
GRectangle getImageBounds(std::string filename);
/*
* Function: drawLine
* Usage: drawLine(p0, p1);
* drawLine(x0, y0, x1, y1);
* --------------------------------
* Draws a line connecting the specified points.
*/
void drawLine(const GPoint & p0, const GPoint & p1);
void drawLine(double x0, double y0, double x1, double y1);
/*
* Function: drawPolarLine
* Usage: GPoint p1 = drawPolarLine(p0, r, theta);
* GPoint p1 = drawPolarLine(x0, y0, r, theta);
* ---------------------------------------------------
* Draws a line of length r in the direction theta from the initial point.
* The angle theta is measured in degrees counterclockwise from the +x
* axis. The function returns the end point of the line.
*/
GPoint drawPolarLine(const GPoint & p0, double r, double theta);
GPoint drawPolarLine(double x0, double y0, double r, double theta);
/*
* Function: drawOval
* Usage: drawOval(bounds);
* drawOval(x, y, width, height);
* -------------------------------------
* Draws the frame of a oval with the specified bounds.
*/
void drawOval(const GRectangle & bounds);
void drawOval(double x, double y, double width, double height);
/*
* Function: fillOval
* Usage: fillOval(bounds);
* fillOval(x, y, width, height);
* -------------------------------------
* Fills the frame of a oval with the specified bounds.
*/
void fillOval(const GRectangle & bounds);
void fillOval(double x, double y, double width, double height);
/*
* Function: drawRect
* Usage: drawRect(bounds);
* drawRect(x, y, width, height);
* -------------------------------------
* Draws the frame of a rectangle with the specified bounds.
*/
void drawRect(const GRectangle & bounds);
void drawRect(double x, double y, double width, double height);
/*
* Function: fillRect
* Usage: fillRect(bounds);
* fillRect(x, y, width, height);
* -------------------------------------
* Fills the frame of a rectangle with the specified bounds.
*/
void fillRect(const GRectangle & bounds);
void fillRect(double x, double y, double width, double height);
/*
* Function: drawPolygon
* Usage: drawPolygon(polygon);
* drawPolygon(polygon, pt);
* drawPolygon(polygon, x, y);
* ----------------------------------
* Draws the outline of the specified polygon. The optional pt or x and y
* parameters shift the origin of the polygon to the specified point.
*/
void drawPolygon(const Vector<GPoint> & polygon);
void drawPolygon(const Vector<GPoint> & polygon, const GPoint & pt);
void drawPolygon(const Vector<GPoint> & polygon, double x, double y);
/*
* Function: fillPolygon
* Usage: fillPolygon(polygon);
* fillPolygon(polygon, pt);
* fillPolygon(polygon, x, y);
* ----------------------------------
* Fills the frame of the specified polygon. The optional pt or x and y
* parameters shift the origin of the polygon to the specified point.
*/
void fillPolygon(const Vector<GPoint> & polygon);
void fillPolygon(const Vector<GPoint> & polygon, const GPoint & pt);
void fillPolygon(const Vector<GPoint> & polygon, double x, double y);
/*
* Function: drawString
* Usage: drawString(str, pt);
* drawString(str, x, y);
* -----------------------------
* Draws the string str so that its baseline origin appears at the
* specified point. The text appears in the current font and color.
*/
void drawString(std::string str, const GPoint & pt);
void drawString(std::string str, double x, double y);
/*
* Function: getStringWidth
* Usage: double width = getStringWidth(str);
* ------------------------------------------
* Returns the width of the string str when displayed in the current font.
*/
double getStringWidth(std::string str);
/*
* Function: setFont
* Usage: setFont(font);
* ---------------------
* Sets a new font. The font parameter is a string in the form
* family-style-size. In this string, family is the name of the font
* family; style is either missing (indicating a plain font) or one of the
* strings Bold, Italic, or BoldItalic; and size is an integer indicating
* the point size. If any of these components is specified as an asterisk,
* the existing value is retained. The font parameter can also be a
* sequence of such specifications separated by semicolons, in which the
* first available font on the system is used.
*/
void setFont(std::string font);
/*
* Function: getFont
* Usage: string font = getFont();
* -------------------------------
* Returns the current font.
*/
std::string getFont();
/*
* Function: setColor
* Usage: setColor(color);
* -----------------------
* Sets the color used for drawing. The color parameter is usually one of
* the predefined color names from Java:
*
* BLACK,
* BLUE,
* CYAN,
* DARK_GRAY,
* GRAY,
* GREEN,
* LIGHT_GRAY,
* MAGENTA,
* ORANGE,
* PINK,
* RED,
* WHITE, or
* YELLOW.
*
* The case of the individual letters in the color name is ignored, as are
* spaces and underscores, so that the Java color DARK_GRAY could be
* written as "Dark Gray".
*
* The color can also be specified as a string in the form "#rrggbb" where
* rr, gg, and bb are pairs of hexadecimal digits indicating the red,
* green, and blue components of the color.
*/
void setColor(std::string color);
/*
* Function: getColor
* Usage: string color = getColor();
* ---------------------------------
* Returns the current color as a string in the form "#rrggbb". In this
* string, the values rr, gg, and bb are two-digit hexadecimal values
* representing the red, green, and blue components of the color,
* respectively.
*/
std::string getColor();
/*
* Function: saveGraphicsState
* Usage: saveGraphicsState();
* ---------------------------
* Saves the state of the graphics context. This function is used in
* conjunction with restoreGraphicsState() to avoid changing the state set
* up by the client.
*/
void saveGraphicsState();
/*
* Function: restoreGraphicsState
* Usage: restoreGraphicsState();
* ------------------------------
* Restores the graphics state from the most recent call to
* saveGraphicsState().
*/
void restoreGraphicsState();
/*
* Function: getWindowWidth
* Usage: double width = getWindowWidth();
* ---------------------------------------
* Returns the width of the graphics window in pixels.
*/
double getWindowWidth();
/*
* Function: getWindowHeight
* Usage: double height = getWindowHeight();
* -----------------------------------------
* Returns the height of the graphics window in pixels.
*/
double getWindowHeight();
/*
* Function: repaint
* Usage: repaint();
* -----------------
* Issues a request to update the graphics window. This function is called
* automatically when the program pauses, waits for an event, waits for
* user input on the console, or terminates. As a result, most clients
* will never need to call repaint explicitly.
*/
void repaint();
/*
* Function: pause
* Usage: pause(milliseconds);
* ---------------------------
* Pauses for the indicated number of milliseconds. This function is
* useful for animation where the motion would otherwise be too fast.
*/
void pause(double milliseconds);
/*
* Function: waitForClick
* Usage: waitForClick();
* ----------------------
* Waits for a mouse click to occur anywhere in the window.
*/
void waitForClick();
/*
* Function: setWindowTitle
* Usage: setWindowTitle(title);
* -----------------------------
* Sets the title of the primary graphics window.
*/
void setWindowTitle(std::string title);
/*
* Function: getWindowTitle
* Usage: string title = getWindowTitle();
* ---------------------------------------
* Returns the title of the primary graphics window.
*/
std::string getWindowTitle();
/*
* Function: exitGraphics
* Usage: exitGraphics();
* ----------------------
* Closes the graphics window and exits from the application without
* waiting for any additional user interaction.
*/
void exitGraphics();
#include "console.h"
#include "private/main.h"
#endif