forked from svn2github/led-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculate.cpp
182 lines (157 loc) · 3.66 KB
/
Calculate.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
/*
* Calculate.cpp - A library for calculations not available in the standard Arduino language.
*
* Copyright (c) 2011 Remco Magielse, Intelligent Lighting Institute (ILI), TU/e.
* All rights reserved.
*/
#include "Calculate.h"
static const float PII = 3.14159265;
/** A linear transition **/
uint16_t CalculateClass::linear( uint32_t t, uint16_t b, int c, uint32_t d )
{
uint16_t calculation = c * ((float)t/(float)d) + b;
return calculation;
}
/** A quadratic easing equation **/
float CalculateClass::quadratic( float t, float b, float c, float d, boolean easeIn, boolean easeOut )
{
if(easeIn && !easeOut)
{
/* Checked: CORRECT*/
t /= d;
float calculation = c*t*t + b;
return calculation;
}
else if(!easeIn && easeOut)
{
/* Checked: CORRECT */
t /= d;
float calculation = -c*t*(t-2) + b;
return calculation;
}
else
{
/* Checked: CORRECT */
t /= d/2;
if(t < 1)
{
float calculation = (c/2)*t*t + b;
return calculation;
}
else
{
t--;
float calculation = (-c/2) * (t*(t-2) - 1) + b;
return calculation;
}
}
}
/** An exponential easing equation. !!! Not functional at the moment!!! **/
float CalculateClass::exponential( float t, float b, float c, float d, boolean easeIn, boolean easeOut )
{
if(easeIn && !easeOut)
{
t /= d;
float calculation = c * sq(10 *(t/d - 1)) + b;
/* Serial.print("calc c:");
Serial.print(c);
Serial.print(", t:");
Serial.print(t);
Serial.print(", d:");
Serial.print(d);
Serial.print(", b:");
Serial.print(b);
Serial.print( " = " );
Serial.println(calculation); */
return calculation;
}
else if(!easeIn && easeOut)
{
t /= d;
float calculation = c * (-sq(-10 * t/d) + 1) +b;
return calculation;
}
else
{
t /= d/2;
if(t < 1)
{
float calculation = c/2 * sq(10 * (t-1) ) + b;
return calculation;
}
else
{
t--;
float calculation = c/2 * (-sq( -10 * t) + 2) + b;
return calculation;
}
}
}
/** An circular easing equation. !!! Not functional at the moment!!! **/
float CalculateClass::circular( float t, float b, float c, float d, boolean easeIn, boolean easeOut )
{
if(easeIn && !easeOut)
{
t /= d;
float calculation = -c * (sqrt(1 - t*t) - 1) + b;
return calculation;
}
else if(!easeIn && easeOut)
{
t /= d;
t--;
float calculation = c * sqrt(1 - t*t) + b;
return calculation;
}
else
{
t /= d/2;
if(t < 1)
{
float calculation = -c/2 * (sqrt(1 - t*t) - 1) + b;
return calculation;
}
else
{
t-=2;
float calculation = c/2 * (sqrt(1 - t*t) + 1) + b;
return calculation;
}
}
}
/** A sinus easing equation. !!! Not functional at the moment!!! **/
float CalculateClass::sinus( float t, float b, float c, float d, boolean easeIn, boolean easeOut )
{
if(easeIn && !easeOut)
{
float calculation = -c * cos(t/d * (PII/2)) + c + b;
return calculation;
}
else if(!easeIn && easeOut)
{
t /= d;
float calculation = c * sin(t/d * (PII/2)) + b;
return calculation;
}
else
{
float calculation = -c/2 * (cos(PII * t/d) - 1) + b;
return calculation;
}
}
float CalculateClass::distance( float x1, float y1, float x2, float y2 )
{
float x = sq( x1 - x2 ); //square the x-coordinates
float y = sq( y1 - y2 ); //square the y-coordinates
float distance = sqrt( x + y ); //square-root the sum of x and y
return distance;
}
float CalculateClass::radian( float degrees )
{
return (degrees/180) * PII;
}
float CalculateClass::degree( float radians )
{
return (radians/PII) * 180;
}
CalculateClass Calculate;