-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.c
184 lines (148 loc) · 3.85 KB
/
display.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
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
/*====================== display.c ========================
Contains functions for basic manipulation of a screen
represented as a 2 dimensional array of colors.
A color is an ordered triple of ints, with each value standing
for red, g and b respectively
==================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include "ml6.h"
#include "display.h"
const color BG = {0, 0, 0};
color new_color(uint8_t r, uint8_t g, uint8_t b) {
color c;
c.r = r;
c.g = g;
c.b = b;
return c;
}
color change_color( int i ) {
i %= 7;
switch( i ) {
case 0:
return new_color(255, 255, 255);
case 1:
return new_color(255, 0, 0);
case 2:
return new_color(0, 255, 0);
case 3:
return new_color(0, 0, 255);
case 4:
return new_color(255, 255, 0);
case 5:
return new_color(255, 0, 255);
case 6:
return new_color(0, 255, 255);
}
}
/*======== void plot() ==========
Inputs: struct screen *s
color c
int x
int y
Returns:
Sets the color at pixel x, y to the color represented by c
Note that s->xy[0][0] will be the upper left hand corner
of the screen.
If you wish to change this behavior, you can change the indicies
of s that get set. For example, using s->xy[x][YRES-1-y] will have
pixel 0, 0 located at the lower left corner of the screen
02/12/10 09:09:00
jdyrlandweaver
====================*/
void plot(struct screen *s, color c, struct vector *v) {
int x, newy;
double z;
x = v->x;
newy = YRES - 1 - v->y;
z = v->z;
if ( x >= 0 && x < XRES
&& newy >=0 && newy < YRES
&& z > s->z[x][newy]) {
s->xy[x][newy] = c;
s->z[x][newy] = z;
}
}
/*======== void clear_screen() ==========
Inputs: struct screen *s
Returns:
Sets every color in struct screen *s to black
02/12/10 09:13:40
jdyrlandweaver
====================*/
void clear_screen(struct screen *s, color ambient) {
int x, y;
color c;
for ( y=0; y < YRES; y++ )
for ( x=0; x < XRES; x++) {
s->xy[x][y] = ambient;
s->z[x][y] = -DBL_MAX;
}
}
/*======== void save_ppm() ==========
Inputs: struct screen *s
char *file
Returns:
Saves struct screen *s as a valid ppm file using the
settings in ml6.h
02/12/10 09:14:07
jdyrlandweaver
====================*/
void save_ppm( struct screen *s, char *file) {
int x, y;
FILE *f;
f = fopen(file, "w");
fprintf(f, "P3\n%d %d\n%d\n", XRES, YRES, MAX_COLOR);
for ( y=0; y < YRES; y++ ) {
for ( x=0; x < XRES; x++)
fprintf(f, "%d %d %d ", s->xy[x][y].r, s->xy[x][y].g, s->xy[x][y].b);
fprintf(f, "\n");
}
fclose(f);
}
/*======== void save_extension() ==========
Inputs: struct screen *s
char *file
Returns:
Saves the struct screen *stor in s to the filename represented
by file.
If the extension for file is an image format supported
by the "convert" command, the image will be saved in
that format.
02/12/10 09:14:46
jdyrlandweaver
====================*/
void save_extension( struct screen *s, char *file) {
int x, y;
FILE *f;
char line[256];
sprintf(line, "convert - %s", file);
f = popen(line, "w");
fprintf(f, "P3\n%d %d\n%d\n", XRES, YRES, MAX_COLOR);
for ( y=0; y < YRES; y++ ) {
for ( x=0; x < XRES; x++)
fprintf(f, "%d %d %d ", s->xy[x][y].r, s->xy[x][y].g, s->xy[x][y].b);
fprintf(f, "\n");
}
pclose(f);
}
/*======== void display() ==========
Inputs: struct screen *s
Returns:
Will display the struct screen *s on your monitor
02/12/10 09:16:30
jdyrlandweaver
====================*/
void display( struct screen *s) {
int x, y;
FILE *f;
f = popen("display", "w");
fprintf(f, "P3\n%d %d\n%d\n", XRES, YRES, MAX_COLOR);
for ( y=0; y < YRES; y++ ) {
for ( x=0; x < XRES; x++)
fprintf(f, "%d %d %d ", s->xy[x][y].r, s->xy[x][y].g, s->xy[x][y].b);
fprintf(f, "\n");
}
pclose(f);
}