-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.cpp
More file actions
191 lines (149 loc) · 3.45 KB
/
function.cpp
File metadata and controls
191 lines (149 loc) · 3.45 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
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
#include "function.h"
using namespace std;
Function::Function() : m_name(""), m_expr(""), m_latex("")
{
}
string Function::getName() const
{
return m_name;
}
string Function::getExpr() const
{
return "f(x,y) = (" + m_expr + ")";
}
string Function::getLatex() const
{
return "f(x,y) = ("+ m_latex + ")";
}
Identity::Identity() : Function()
{
m_name="Identity";
}
void Identity::apply(double &x, double &y)
{
x = x;
y = y;
}
void Affinity::apply(double &x, double &y)
{
double temp = x;
x = m_fX[0] * x + m_fX[1] * y + m_fX[2];
y = m_fY[0] * temp + m_fY[1] * y + m_fY[2];
}
Affinity::Affinity(double a1, double a2, double b1, double b2, double c1, double c2) : Function()
{
setCoeff(a1, a2, b1, b2, c1, c2);
m_name = "Affinity";
//m_expr = a1 + "*x+" + b1 + "*y+" + c1 +"," + a2 + "*x+" + b2 + "*y+" + c2 ;
}
const double* Affinity::getFX() const
{
return m_fX;
}
const double * Affinity::getFY() const
{
return m_fY;
}
void Affinity::setCoeff(double a1, double a2, double b1, double b2, double c1, double c2)
{
m_fX[0] = a1;
m_fX[1] = b1;
m_fX[2] = c1;
m_fY[0] = a2;
m_fY[1] = b2;
m_fY[2] = c2;
}
Sinusoidal::Sinusoidal() : Function()
{
m_name = "Sinusoïdal";
m_expr = "sin(x),sin(y)";
m_latex = "\\sin(x) , \\sin(y)";
}
void Sinusoidal::apply(double &x, double &y)
{
x = sin(x);
y = sin(y);
}
GenFunction::GenFunction(unsigned int nbFun) : Function(),m_variations(nbFun,0),m_functions(nbFun,new Identity()),m_affinity(0),m_color(0)
{
m_affinity = new Affinity(1,0,0,1,0,0);
m_postTransform = new Affinity(1,0,0,1,0,0);
}
void GenFunction::apply(double &x, double &y)
{
double a(0),b(0);
for(unsigned int i(0); i <m_variations.size() ; i++)
{
double x1(x),y1(y);
// We calculate for the affinity
m_affinity->apply(x1, y1);
//Then we "compose" the result
m_functions[i]->apply(x1, y1);
a += m_variations[i] * x1;
b += m_variations[i] * y1;
}
x = a;
y = b;
//Finally, all that passes through the post transform ( to change the coordinate system, for instance
m_postTransform->apply(x,y);
}
int GenFunction::getColor() const
{
return m_color;
}
void GenFunction::setColor(int color)
{
m_color = color;
}
void GenFunction::setAffinity(Affinity affinity)
{
m_affinity = new Affinity(affinity);
}
void GenFunction::setPostTransform(Affinity affinity)
{
m_postTransform = new Affinity(affinity); // Need to destroy the post transform et the pre transform ?
}
void GenFunction::setFunction(unsigned int num, Function *function)
{
if( num < m_functions.size())
{
m_functions[num] = function;
}
}
GenFunction::~GenFunction()
{
delete m_affinity;
delete m_postTransform;
}
char GenFunction::toAlpha(int color)
{
return (color >> 24) & 0xff;
}
char GenFunction::toRed(int color)
{
return (color >> 16) & 0xff;
}
char GenFunction::toGreen(int color)
{
return (color >> 8) & 0xff;
}
char GenFunction::toBlue(int color)
{
return color & 0xff;
}
int GenFunction::toColor(char alpha, char red, char green, char blue)
{
return (alpha << 24) & (red << 16) & (green << 8) & blue;
}
Spherical::Spherical() : Function()
{
m_name="Spherical";
m_expr="1/r^2 * x, 1/ r^2 * y";
m_latex=" \frac{x}{r^2}, \frac{y}{r^2}";
}
void Spherical::apply(double &x, double &y)
{
double rCarre = x*x + y*y;
x = x / rCarre;
y = y / rCarre;
}