-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathADDAC_SuperFormula.cpp
executable file
·260 lines (170 loc) · 5.57 KB
/
ADDAC_SuperFormula.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Some hints about what this Class does!
*
*/
#include "ADDAC_SuperFormula.h"
//-----------------------------------------------------------------------ADDAC SuperFormula-----------------
/*! \brief Default constructor for ADDAC_SuperFormula. */
ADDAC_SuperFormula::ADDAC_SuperFormula(){ // INITIALIZE CLASS
pos=0;
pX=0;
pY=0;
numPoints = 360;
loopMin=0;
inc=1;
mirror = false;
startUp=true;
//Serial.println("ADDAC_SuperFormula INITIALIZED");
}
// --------------------------------------------------------------------------- UPDATE -------------------------
//
/*! \brief update SuperFormula calcs
\param m Superformula number of petals : 0 - 1
\param n2 Superformula shape variable 1 : 0 - 1
\param n3 Superformula shape variable 2 : 0 - 1
\param _speed Superformula speed calculation
*/
// newValue - value to smooth, smoothFactor - smooth intensity 0-1.
void ADDAC_SuperFormula::update(float m, float n2, float n3, long _speed) {
long oldTime=millis();
m*=1000.0f;
m*2;
n2*=100.0f;
n3*=100.0f;
//values checked on processing for default min and max values//
n2=constrain(n2,0,100);
n3=constrain(n3,0,100);
n2=mapfloat(n2,0,100,-50,100);
n3=mapfloat(n3,0,100,-50,100);
///////////////////////////////////////////////////////////////
float phi = TWO_PI / 360;
if(startUp){
metroTime=1;
startUp=false;
}
else metroTime=_speed;
if(metro.set(metroTime)){
if (mirror) {
if(pos>numPoints){
pos=numPoints;
inc=-1;
}
if(pos<loopMin){
pos=loopMin;
inc=1;
}
pos+=inc;
}
else{
pos++;
if(pos>numPoints) pos = loopMin;
}
oldPos=pos;
superformulaPoint(m, n2, n3, phi);
}
long timeCpu=fabs(actualTime-oldTime);
cpuPos++;
if(cpuPos>9) cpuPos=0;
cpuTime[cpuPos]=timeCpu;
actualTime=millis();
}
void ADDAC_SuperFormula::superformulaPoint(float m, float n2, float n3, float phi) {
//actual Point
float r;
float t1, t2;
float a=1, b=1;
x = 0;
y = 0;
int i =pos;
float n1=50;
t1 = cos(m * (phi*(i)) / 4) / a;
t1 = fabs(t1-0);
t1 = pow(t1, n2);
t2 = sin(m * (phi*(i)) / 4) / b;
t2 = fabs(t2);
t2 = pow(t2, n3);
r = pow(t1+t2, 1/n1);
if (fabs(r) == 0) {
x = 0;
y = 0;
}
else {
r = 1 / r;
x = r * cos((phi*(i)));
y = r * sin((phi*(i)));
}
//MIN and MAX
//max and min values checked on processing if n1 value set to 50 //???
x=constrain(x,minValueX,maxValueX);
y=constrain(y,minValueY,maxValueY);
float difX=(float)fabs(oldX-x);
float difY=(float)fabs(oldY-y);
interpolationX = difX / ((float)(metroTime+CPUtime())/(float)CPUtime());
interpolationY = difY / ((float)(metroTime+CPUtime())/(float)CPUtime());
}
/*! \Get SuperFormula X position Stream
*/
float ADDAC_SuperFormula::getX(){
if(oldX<=x)oldX+=interpolationX;
if(oldX>=x)oldX-=interpolationX;
//map max and min values checked on processing if n1 value set to 50 //???
pX = mapfloat(oldX, minValueX, maxValueX, 0.0f, 1.0f);
pX = constrain(pX,0.0f,1.0f);
// Serial.print("OldX : ");
// Serial.print(oldX);
// Serial.print(" X : ");
// Serial.print(x);
// Serial.print(" interpolationX : ");
// Serial.print(interpolationX);
return pX;
}
/*! \Get SuperFormula Y position Stream
*/
float ADDAC_SuperFormula::getY(){
if(oldY<=y)oldY+=interpolationY;
if(oldY>=y)oldY-=interpolationY;
//map max and min values checked on processing if n1 value set to 50 //???
pY=mapfloat(oldY,minValueY,maxValueY,0.0f,1.0f);
pY=constrain(pY,0.0f,1.0f);
// Serial.print("OldY : ");
// Serial.print(oldY);
// Serial.print(" Y : ");
// Serial.print(y);
// Serial.print(" interpolationY : ");
// Serial.print(interpolationY,DEC);
return pY;
}
/*! \Set the loop Max value
\param _max max value of the loop : 0 - 360 | Default 360
*/
void ADDAC_SuperFormula::setLoopMax(int _max){
numPoints=_max;
}
/*! \Set the loop Min value
\param _min min value of the loop : 0 - 360 | Defalut 0
*/
void ADDAC_SuperFormula::setLoopMin(int _min){
loopMin=_min;
}
/*! \Turn On/Off the PingPong Mode | Default false
\param _max max value of the loop : 0 - 360
*/
void ADDAC_SuperFormula::setPingPongMode(bool _mirror){
mirror=_mirror;
}
float ADDAC_SuperFormula::mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min;
}
//CPU AVERAGE
float ADDAC_SuperFormula::CPUtime()
{
float timeAverage;
for(int i=0;i<9;i++){
timeAverage+=cpuTime[i];
}
timeAverage/=10;
return timeAverage;
}
// --------------------------------------------------------------------------- END ----------------------------------
//