forked from thoralt/esp8266wordclock
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparticle.cpp
More file actions
163 lines (146 loc) · 5.14 KB
/
particle.cpp
File metadata and controls
163 lines (146 loc) · 5.14 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
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
// ESP8266 Wordclock
// Copyright (C) 2016 Thoralt Franz, https://github.com/thoralt
//
// This module encapsulates a single particle used for the exploding letters effect.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <math.h>
#include "particle.h"
#include "ledfunctions.h"
//---------------------------------------------------------------------------------------
// brightness gradient for moving particle
//---------------------------------------------------------------------------------------
const float Particle::ParticleGradient[MAX_PARTICLE_DISTANCE] = {
1, 0.75, 0.5, 0.25, 0.125, 0.06, 0.03, 0.01 };
//---------------------------------------------------------------------------------------
// Particle
//
// Constructor. Initializes coordinates and speed with given values.
//
// -> x: x of start coordinate
// y: y of start coordinate
// vx: x velocity
// vy: y velocity
// delay: time to wait until the particle starts moving
// <- --
//---------------------------------------------------------------------------------------
Particle::Particle(float x, float y, float vx, float vy, int delay)
{
this->x = x;
this->y = y;
this->vx = vx;
this->vy = vy;
this->x0 = x;
this->y0 = y;
this->delay = delay;
this->alive = true;
}
//---------------------------------------------------------------------------------------
// ~Particle
//
// Destructor
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
Particle::~Particle()
{
}
//---------------------------------------------------------------------------------------
// distance
//
// Calculates the distance to the starting point of the particle
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
float Particle::distance()
{
return this->distanceTo(this->x0, this->y0);
}
//---------------------------------------------------------------------------------------
// distanceTo
//
// Calculates the distance to a given point
//
// -> x, y: Point for distance test
// <- --
//---------------------------------------------------------------------------------------
float Particle::distanceTo(float x, float y)
{
float dx = this->x - x;
float dy = this->y - y;
return sqrt(dx*dx + dy*dy);
}
//---------------------------------------------------------------------------------------
// move
//
// Moves the particle according to the current speed.
//
// -> --
// <- distance to starting point
//---------------------------------------------------------------------------------------
float Particle::move()
{
// do not move until given delay has expired
if(this->delay)
{
this->delay--;
return 0;
}
this->x += this->vx;
this->y += this->vy;
// mark movement as finished if distance has reached maximum
float d = this->distance();
if(d > MAX_PARTICLE_DISTANCE) this->alive = false;
return d;
}
//---------------------------------------------------------------------------------------
// render
//
// Moves the particle, renders it to the given buffer.
//
// -> target: RGB target buffer (i. e. LEDFunctions::currentValues)
// palette: palette with background color, foreground color
// <- --
//---------------------------------------------------------------------------------------
void Particle::render(uint8_t *target, palette_entry palette[])
{
// move particle and save traveled distance
int d = (int)this->move();
// check boundaries
if(this->x<0 || this->x >= LEDFunctionsClass::width) return;
if(this->y<0 || this->y >= LEDFunctionsClass::height) return;
// limit distance
if(d >= MAX_PARTICLE_DISTANCE) d = MAX_PARTICLE_DISTANCE - 1;
// get palette color for foreground
float pr = (float)palette[1].r;
float pg = (float)palette[1].g;
float pb = (float)palette[1].b;
// calculate offset in buffer from given coordinates
int ofs = LEDFunctionsClass::getOffset(this->x, this->y);
// calculate fading color depending on distance from starting point and add it
// to the previous value of the pixel corresponding to the particle
float r = (float)target[ofs + 0] + pr * ParticleGradient[d];
float g = (float)target[ofs + 1] + pg * ParticleGradient[d];
float b = (float)target[ofs + 2] + pb * ParticleGradient[d];
// limit brightness value of each component to foreground color values
if(r > pr) r = pr;
if(g > pg) g = pg;
if(b > pb) b = pb;
// write back pixel color
target[ofs + 0] = r;
target[ofs + 1] = g;
target[ofs + 2] = b;
}