forked from af3ld/ComputerGraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalexfeldman_arrows.c
124 lines (91 loc) · 3 KB
/
alexfeldman_arrows.c
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
/*
1. Download the following :
sftp student@simpson1
mget arrows3.exe
mget arrows3student.c (which is an electronic version of this file)
mget D2d_matrix.h
mget D2d_matrixJeff.c (if you don't trust your own)
quit
2. Run the downloaded executable, via :
./arrows3.exe
Press and hold the spacebar to see how the program behaves.
3. Your task is to enhance this file, using the matrix tools in
D2d_matrix, with an eye to duplicating the behavior of the executable,
arrows3.exe. For full credit, aim for run-time efficiency
by judicious use of the matrix tools.
4. In order for Jeff to grade this, before you start working, close
this file (if you are in emacs) and rename it from the command line
to a name that reflects your name :
mv arrows3student.c yournameARROWS.c
Make sure the ARROWS is captialized.
5. To compile your code :
acom -I. D2d_matrixJeff.c yournameARROWS.c
(assuming you use Jeff's D2d_matrix file)
*/
#include <FPT.h>
#include <D2d_matrix.h>
int HEIGHT = 700; int WIDTH = 700;
//arrow
double ax[4] = {0, -20, 20, -20} ;
double ay[4] = {0, -15, 0, 15} ;
void copy_array(double *a, double *b, int size) {
int i;
for (i = 0; i < size; i++) {
b[i] = a[i];
}
}
void duplicate_n_arrange(double mat[3][3], double inv[3][3]) {
int i, j;
double bx[4]; double by[4];
double radians = ((360 / 13) * M_PI / 180);
copy_array(ax, bx, 4);
copy_array(ay, by, 4);
D2d_make_identity(mat); D2d_make_identity(inv);
D2d_translate(mat, inv, WIDTH / -2, HEIGHT / -2);
D2d_rotate(mat, inv, radians);
D2d_translate(mat, inv, WIDTH / 2, HEIGHT / 2);
for (i = 0; i < 13; i++) {
D2d_mat_mult_points(bx, by, mat, bx, by, 4) ;
G_fill_polygon(bx, by, 4);
}
}
//moves the arrow away from the origin
//and controls the scaling; Currently it increases by 10%
void move_n_scale(double scalefactor, double mat[3][3], double inv[3][3]) {
D2d_make_identity(mat); D2d_make_identity(inv);
D2d_translate(mat, inv, -WIDTH / 2, -HEIGHT / 2);
D2d_scale(mat, inv, scalefactor, scalefactor);
D2d_translate(mat, inv, WIDTH / 2, HEIGHT / 2);
D2d_translate(mat, inv, 5, 0);
}
int main()
{
double mat[3][3], inv[3][3], radians ;
double r = 0;
double b = 1;
int q, spacebar; int running = 1;
G_init_graphics(WIDTH, HEIGHT) ; //WIDTH x HEIGHT = 700x700
D2d_make_identity(mat); D2d_make_identity(inv);
D2d_translate(mat, inv, WIDTH /2, HEIGHT /2);
D2d_mat_mult_points(ax, ay, mat, ax, ay, 4) ;
while (running) { //loops the program
G_rgb(0, 0, 0) ;
G_clear() ;
G_rgb(1, 1, 1) ;
G_draw_string("Press and hold the spacebar.", 10, 10) ;
G_rgb(r, 0, b);
duplicate_n_arrange(mat, inv);
move_n_scale(1.03, mat, inv);
spacebar = G_wait_key();
while (spacebar - '0' == -16) { //ascii char value for spacebar
D2d_mat_mult_points(ax, ay, mat, ax, ay, 4) ;
r += .03;
b -= .03;
spacebar = -1;
}
if (spacebar == 'q' || ax[3] > WIDTH - 20) {
running = 0;
}
}
G_wait_key();
}