-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteor.c
58 lines (50 loc) · 1.64 KB
/
meteor.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
// "meteor", by Ka-Ping Yee
#include "serpent.h"
byte pixels[NUM_PIXELS*3];
int modulo(int div, int mod) {
while (div < 0) div += mod;
return div % mod;
}
void draw_ball(float crow, float ccol, float radius,
byte red, byte green, byte blue) {
for (int r = floor(crow) - radius - 1; r <= ceil(crow) + radius + 1; r++) {
for (int c = floor(ccol) - radius - 1; c <= ceil(ccol) + radius + 1; c++) {
if (r >= 0 && r < NUM_ROWS) {
float d = sqrt((r - crow)*(r - crow) + (c - ccol)*(c - ccol));
int index = pixel_index(r, modulo(c, NUM_COLUMNS));
if (d < radius - 0.5) {
set_rgb(pixels, index, red, green, blue);
} else if (d < radius + 0.5) {
float frac = d - (radius - 0.5);
set_rgb(pixels, index, red*frac, green*frac, blue*frac);
}
}
}
}
}
void erase_ball(float crow, float ccol, float radius) {
for (int r = floor(crow) - radius - 1; r <= ceil(crow) + radius + 1; r++) {
for (int c = floor(ccol) - radius - 1; c <= ceil(ccol) + radius + 1; c++) {
if (r >= 0 && r < NUM_ROWS) {
int index = pixel_index(r, modulo(c, NUM_COLUMNS));
set_rgb(pixels, index, 0, 0, 0);
}
}
}
}
void next_frame(int f) {
if (f == 0) {
for (int r = 0; r < NUM_ROWS; r++) {
for (int c = 0; c < NUM_COLUMNS; c++) {
set_rgb(pixels, pixel_index(r, c), (r+c)%2, (r+c)%2, 0);
}
}
}
float x = f; //sin(f/100.0)*50 + 60;
float y = f; //sin(f/100.0)*50 + 60;
draw_ball(x, y, 0.5, 240, 120, 0);
for (int s = 0; s < NUM_SEGS; s++) {
put_pixels(s, pixels + s*SEG_PIXELS*3, SEG_PIXELS);
}
erase_ball(x, y, 3);
}