forked from querry43/rgbdisplay-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathballs.cpp
More file actions
90 lines (73 loc) · 1.84 KB
/
balls.cpp
File metadata and controls
90 lines (73 loc) · 1.84 KB
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
#include <math.h>
#include "display.h"
// settings for teensy 2.0
const int nLEDs = 32;
const int dataPin = 22;
const int clockPin = 10;
const int powerPin = 11;
const int power = 20;
#define GRID_SIZE 6
#define BALL_DELAY (100ul)
DisplayWrapper display = DisplayWrapper(nLEDs, dataPin, clockPin, powerPin, power);
struct color_t {
double r, g, b;
};
const color_t black = { 0.0, 0.0, 0.0 };
struct ball_t {
double x, y;
double dx, dy;
color_t color;
};
ball_t balls[] = {
{ 0.0, 0.0, 0.30, 0.20, { 1.0, 0.0, 0.0 } },
{ 2.0, 5.0, 0.37, -0.15, { 0.0, 1.0, 0.0 } },
{ 5.0, 2.0, -0.15, 0.37, { 0.0, 0.0, 1.0 } }
};
#define NUM_BALLS (sizeof(balls) / sizeof(balls[0]))
void move_through_dimension(double & xy, double & dxy) {
xy += dxy;
if (xy < 0) {
xy = -xy;
dxy = -dxy;
}
if (xy >= GRID_SIZE) {
xy = GRID_SIZE - (xy - GRID_SIZE);
dxy = -dxy;
}
}
void move_balls() {
for (int i = 0; i < NUM_BALLS; ++i) {
ball_t & b = balls[i];
move_through_dimension(b.x, b.dx);
move_through_dimension(b.y, b.dy);
}
}
void show_balls() {
color_t colors[GRID_SIZE][GRID_SIZE];
for (int x = 0; x < GRID_SIZE; ++x) {
for (int y = 0; y < GRID_SIZE; ++y) {
color_t c = black;
for (int i = 0; i < NUM_BALLS; ++i) {
ball_t & b = balls[i];
double distance = pow(pow(b.x - x, 2) + pow(b.y - y, 2), 0.5);
double f = 1.0 / (pow(distance, 2) + 1.0);
c.r += b.color.r * f;
c.g += b.color.g * f;
c.b += b.color.b * f;
}
display.setPixelColor(x, y, int(c.r / NUM_BALLS * 255) + (int(c.g / NUM_BALLS * 255) << 8) + (int(c.b / NUM_BALLS * 255) << 16));
}
}
display.show();
}
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
display.begin();
display.show();
}
void loop() {
move_balls();
show_balls();
delay(BALL_DELAY);
}