-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas4.c
217 lines (190 loc) · 5.05 KB
/
canvas4.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
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
/*
canvas4.c
Fourth attempt at a very simple canvas drawing app
Add open and filled circles, and filled quads
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
typedef struct {
int r;
int g;
int b;
} colour;
colour white = {255, 255, 255};
colour black = {0, 0, 0};
colour red = {255, 0, 0};
colour green = {0, 255, 0};
colour blue = {0, 0, 255};
typedef struct {
int w;
int h;
colour *pixels;
} image;
image * image_alloc(int, int);
void image_free(image *);
colour image_get(image *, int, int);
void image_set(image *, int, int, colour);
void image_write(image *, char *);
void image_line(image *, int, int, int, int, colour);
void image_line_thick(image *, int, int, int, int, double, colour);
void image_tri(image *, int, int, int, int, int, int, colour);
void image_circle(image *, int, int, int, colour);
void image_circle_fill(image *, int, int, int, colour);
void image_quad(image *, int, int, int, int, int, int, int, int, colour);
int main(int argc, char *argv[]) {
image *im;
int i;
im = image_alloc(500, 200);
image_circle(im, 100, 100, 80, red);
image_circle_fill(im, 100, 100, 60, green);
image_tri(im, 225, 10, 250, 50, 200, 50, red);
image_quad(im, 250, 50, 300, 50, 300, 150, 250, 150, blue);
image_quad(im, 350, 20, 450, 20, 450, 40, 350, 40, black);
image_quad(im, 350, 50, 400, 70, 450, 150, 370, 130, red);
image_line_thick(im, 300, 20, 350, 180, 12.0, green);
image_write(im, "test4.ppm");
free(im);
}
void image_line_thick(image * im, int x0, int y0, int x1, int y1, double th, colour c) {
double gr, ang;
int xd, yd;
gr = (y1-y0)/(x1-x0);
gr = -1.0/gr;
ang = atan(gr);
xd = th*cos(ang)/2.0;
yd = th*sin(ang)/2.0;
image_quad(im, x0+xd, y0+yd, x1+xd, y1+yd, x1-xd, y1-yd, x0-xd, y0-yd, c);
}
void image_quad(image * im, int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3, colour c) {
// assume coords in cyclic order
image_tri(im, x0, y0, x1, y1, x2, y2, c);
image_tri(im, x0, y0, x2, y2, x3, y3, c);
}
void image_circle(image * im, int x, int y, int r, colour c) {
int i,j;
for (i=0;i<r/sqrt(2);i++) {
j = sqrt(r*r - i*i);
image_set(im, x+j, y+i, c);
image_set(im, x+j, y-i, c);
image_set(im, x-j, y+i, c);
image_set(im, x-j, y-i, c);
image_set(im, x+i, y+j, c);
image_set(im, x+i, y-j, c);
image_set(im, x-i, y+j, c);
image_set(im, x-i, y-j, c);
}
}
void image_circle_fill(image * im, int x, int y, int r, colour c) {
int i,j;
for (i=0;i<r/sqrt(2);i++) {
j = sqrt(r*r - i*i);
image_line(im, x-j, y+i, x+j, y+i, c);
image_line(im, x-j, y-i, x+j, y-i, c);
image_line(im, x-i, y+j, x+i, y+j, c);
image_line(im, x-i, y-j, x+i, y-j, c);
}
}
void image_line(image *im, int x0, int y0, int x1, int y1, colour c) {
int i, xd, yd, tmp;
image_set(im, x0, y0, c); // make sure at least one pixel set
xd = abs(x1 - x0);
yd = abs(y1 - y0);
if (xd > yd) { // walk in x direction
if (x1 < x0) {
tmp=x0; x0=x1; x1=tmp;
tmp=y0; y0=y1; y1=tmp;
}
if (xd > 0) {
for (i=x0;i<=x1;i++) {
tmp = y0 + (i-x0)*(y1-y0)/xd;
image_set(im, i, tmp, c);
}
}
} else { // walk in y direction
if (y1 < y0) {
tmp=x0; x0=x1; x1=tmp;
tmp=y0; y0=y1; y1=tmp;
}
if (yd > 0) {
for (i=y0;i<=y1;i++) {
tmp = x0 + (i-y0)*(x1-x0)/yd;
image_set(im, tmp, i, c);
}
}
}
}
void image_tri(image *im, int x0, int y0, int x1, int y1,
int x2, int y2, colour c) {
int x, xp, y, tmp;
// manual bubble-sort by y value
if (y1<y0) {
tmp=x0; x0=x1; x1=tmp;
tmp=y0; y0=y1; y1=tmp;
}
if (y2<y1) {
tmp=x1; x1=x2; x2=tmp;
tmp=y1; y1=y2; y2=tmp;
}
if (y1<y0) {
tmp=x0; x0=x1; x1=tmp;
tmp=y0; y0=y1; y1=tmp;
}
// fill in with horizontal lines
if (y1>y0) {
for (y=y0;y<=y1;y++) {
x = x0 + (x1-x0)*(y-y0)/(y1-y0);
xp = x0 + (x2-x0)*(y-y0)/(y2-y0);
image_line(im, x, y, xp, y, c);
}
}
if (y2>y1) {
for (y=y1;y<=y2;y++) {
x = x1 + (x2-x1)*(y-y1)/(y2-y1);
xp = x0 + (x2-x0)*(y-y0)/(y2-y0);
image_line(im, x, y, xp, y, c);
}
}
}
image * image_alloc(int width, int height) {
image * im;
int i, j;
im = malloc(sizeof(image));
im->w = width;
im->h = height;
im->pixels = malloc(width*height*sizeof(colour));
for (i=0;i<width;i++) {
for (j=0;j<height;j++) {
image_set(im, i, j, white);
}
}
return(im);
}
void image_free(image * im) {
free(im->pixels);
free(im);
}
colour image_get(image * im, int x, int y) {
return(im->pixels[y*(im->w) + x]);
}
void image_set(image * im, int x, int y, colour c) {
//printf("%d %d : %d %d %d\n", x, y, c.r, c.g, c.b);
im->pixels[y*(im->w) + x] = c;
}
// Output in the plain ASCII "P3" PPM format
// https://netpbm.sourceforge.net/doc/ppm.html
void image_write(image * im, char * fileName) {
FILE *s;
int i,j;
colour c;
s = fopen(fileName, "w");
fprintf(s, "P3 %d %d 255\n", im->w, im->h);
for (j=0;j<(im->h);j++) {
for (i=0;i<(im->w);i++) {
c = image_get(im, i, j);
fprintf(s, "%d %d %d\n", c.r, c.g, c.b);
}
}
fclose(s);
}
// eof