-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.cpp
executable file
·329 lines (276 loc) · 9.88 KB
/
field.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "header.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <memory>
//Fills Field's arrays with needed values of current modality
//It's called when you add or remove a charge and when you switch modality
void Field::update(){
if(isEqPotLines) {
mapPotential();
std::cout<<"Potential mapped"<<std::endl;
findEquipotentialPoints();
std::cout<<"Found " << eqPotPoints.size() << " equipotential points"<<std::endl;
findEquipotentialLines();
std::cout<<"Found " << eqPotLines.size() << " equipotential lines"<<std::endl;
} else if(isElFieldLines){
findElFieldLines();
std::cout<<"Found " << elFieldLines.size() << " electric field lines"<<std::endl;
} else if(isElFieldColor){
mapElFieldIntensity();
std::cout<<"Field Intensity mapped"<<std::endl;
}
}
//Sort vector eqPotPoints by potential intensity and fill vector eqPotLines with Lines of Points at same potential intensity
void Field::findEquipotentialLines(){
std::sort(eqPotPoints.begin(), eqPotPoints.end());
double delta;
for(int i=0; i<eqPotPoints.size(); i++){
Line temp;
delta = std::abs( int(eqPotPoints[i].value/LINES_DEEP_REDUCTION) );
for(int y=i+1; y<eqPotPoints.size(); y++){
if( (eqPotPoints[i].value+delta)>=(eqPotPoints[y].value-delta) && (eqPotPoints[i].value-delta)<=(eqPotPoints[y].value+delta) ){
temp.pointsCoord.push_back(eqPotPoints[y]);
} else {
eqPotLines.push_back(temp);
i=y-1;
break;
}
}
}
}
//Fills vector eqPotPoints with Points at same potential
void Field::findEquipotentialPoints(){
double delta;
for (int x=0; x<windowWidth; x++){
for (int y=0; y<windowHeight; y++){
delta = std::abs( int(potIntensityMap[x][y]/LINES_DEEP_REDUCTION) );
Point point = Point(x, y, potIntensityMap[x][y]);
for (int X=x; X<windowWidth; X++){
for (int Y=y+1; Y<windowHeight; Y++){
if( (potIntensityMap[x][y]+delta)>=(potIntensityMap[X][Y]-delta) && (potIntensityMap[x][y]-delta)<=(potIntensityMap[X][Y]+delta) ) {
eqPotPoints.push_back(point);
//Using goto because in C++ doesn't exist "break" for nested loops
goto end;
}
}
}
end:;
}
}
}
//Fills vector potIntensityMap with potential value of every visible point of the window
void Field::mapPotential(){
bool isPointInsideCharge=false;
bool isPointInsideButtons=false;
for (int x=0; x<windowWidth; x++){
for (int y=0; y<windowHeight; y++){
isPointInsideCharge=false;
for(int i=0; i<qCharges.size(); i++){
isPointInsideCharge = qCharges[i]->isPointInside(x, y);
if(isPointInsideCharge)
break;
}
for(int i=0; i<buttons.size(); i++){
isPointInsideButtons = buttons[i].isPointInside(x, y);
if(isPointInsideButtons)
break;
}
if(!isPointInsideCharge && !isPointInsideButtons){
potIntensityMap[x][y] = potValueInPoint(x,y);
}
}
}
}
//Returns potential in the point (x,y) given by all charges in the field
double Field::potValueInPoint(int x, int y){
double totPotValue = 0;
for(int i=0; i<qCharges.size(); i++)
totPotValue += qCharges[i]->potInPoint(x, y);
return totPotValue;
}
//Fills vector elFieldLines
void Field::findElFieldLines(){
double totChargeAbs = 0;
for(int i=0; i<qCharges.size(); i++)
totChargeAbs += std::abs(qCharges[i]->value);
for(int i=0; i<qCharges.size(); i++){
double propFactor = totChargeAbs/std::abs(qCharges[i]->value);
for(double alpha=0; alpha<PI*2; alpha+=(((PI*2)/EL_FIELD_LINE_PER_CHARGE)*propFactor)){
elFieldLines.push_back(buildElFieldLine(qCharges[i]->externalPoint(alpha), i));
}
}
}
//Builds one electric field line starting from startPoint with direction startPoint.value
Line Field::buildElFieldLine(Point startPoint, int startCharge){
Line temp;
temp.pointsCoord.push_back(startPoint);
double tx = startPoint.x;
double ty = startPoint.y;
double talpha = startPoint.value;
for(int i=0; tx<windowWidth && tx>0 && ty<windowHeight && ty>0 && !insideCharges(tx, ty, startCharge); i++ ){
tx+=cos(talpha);
ty+=sin(talpha);
talpha = elFieldValueInPoint(round(tx), round(ty)).alpha;
if(i%ARROWS_REDUCTION==0 && i!=0)
arrows.push_back(buildElFieldArrowHead(tx, ty, talpha));
if(qCharges[startCharge]->value<0){
talpha-=PI;
if(talpha<0)
talpha+=2*PI;
}
Point loopPoint(tx, ty, talpha);
temp.pointsCoord.push_back(loopPoint);
if(i==EL_FIELD_LINE_LENGHT_LIMIT){
temp.pointsCoord.clear();
for(int j=0; j<i/ARROWS_REDUCTION; j++)
arrows.pop_back();
break;
}
}
return temp;
}
//Returns Field intensity and direction in (x, y)
//Direction=0 mean field with right direction, direction=PI mean field left directed
Vector Field::elFieldValueInPoint(int x, int y){
double totFieldValueX = 0, totFieldValueY = 0;
for(int i=0; i<qCharges.size(); i++){
Vector elFieldByOneCharge = qCharges[i]->elFieldInPoint(x, y);
totFieldValueX += elFieldByOneCharge.x;
totFieldValueY += elFieldByOneCharge.y;
}
Vector v( x, y, sqrt(pow(totFieldValueX, 2) + pow(totFieldValueY, 2)), angleFromPoints(totFieldValueX, totFieldValueY,0,0) );
return v;
}
//Returns an angle between the rects:
//1. Passing through (x1, y1) and (x2, y2)
//2. Horizontal directed
//The angle is measured counterclock wise
double Field::angleFromPoints(double x1, double y1, double x2, double y2){
double alpha=0;
if(x1!=x2){
alpha = atan((y1-y2)/(x1-x2));
if(y1<y2 && x1>x2)
alpha = std::abs(alpha);
else if (y1<y2 && x1<x2)
alpha = PI - alpha;
else if (y1>y2 && x1<x2)
alpha = PI + std::abs(alpha);
else if (y1>y2 && x1>x2)
alpha = 2*PI - alpha;
} else if(y1<y2){
alpha = PI/2;
} else if(y1>y2){
alpha = (3*PI)/2;
}
if(y1==y2 && x1<x2)
alpha = PI;
return alpha;
}
//Draws arrow head: a little triangle rotated and positioned on the electic field line
//Triangle is isosceles with vertex_angle=arrowAngle/2 and side=arrowSize
//Arrows Head is being drawn here and not in draws.cpp because I want to do the rotation and translation only when updated and not every FPS
std::vector<sf::Vertex> Field::buildElFieldArrowHead(double tx, double ty, double talpha){
std::vector<sf::Vertex> temp;
double px = -(cos(ARROW_ANGLE)*ARROW_SIZE);
double py = sin(ARROW_ANGLE)*ARROW_SIZE;
double px2 = px;
double py2 = -py;
//Rotation equations
double rotPX = px*cos(talpha)-py*sin(talpha);
double rotPY = px*sin(talpha)+py*cos(talpha);
double rotPX2 = px2*cos(talpha)-py2*sin(talpha);
double rotPY2 = px2*sin(talpha)+py2*cos(talpha);
temp.push_back(sf::Vertex(sf::Vector2f(tx, ty), sf::Color::White));
temp.push_back(sf::Vertex(sf::Vector2f(tx+rotPX, ty+rotPY), sf::Color::White));
temp.push_back(sf::Vertex(sf::Vector2f(tx+rotPX2, ty+rotPY2), sf::Color::White));
return temp;
}
//Decides if the point (tx, ty) collides with any charge in qCharges array
bool Field::insideCharges(double tx, double ty, int startCharge){
for(int i=0; i<qCharges.size(); i++)
if(i!=startCharge && qCharges[i]->isPointInside(tx, ty) )
return true;
return false;
}
//Fills vector elFieldIntensityMap and finds max and min field intensity in screen
void Field::mapElFieldIntensity(){
bool isPointInsideCharge=false;
bool isPointInsideButtons=false;
bool max_min_toInit = true;
for (int x=0; x<windowWidth; x++){
for (int y=0; y<windowHeight; y++){
isPointInsideCharge=false;
for(int i=0; i<qCharges.size(); i++){
isPointInsideCharge = qCharges[i]->isPointInside(x, y);
if(isPointInsideCharge)
break;
}
for(int i=0; i<buttons.size(); i++){
isPointInsideButtons = buttons[i].isPointInside(x, y);
if(isPointInsideButtons)
break;
}
if(!isPointInsideCharge && !isPointInsideButtons){
elFieldIntensityMap[x][y] = elFieldValueInPoint(x,y).intensity;
if(max_min_toInit){
max_min_toInit = false;
maxElFieldIntensity = elFieldIntensityMap[x][y];
minElFieldIntensity = elFieldIntensityMap[x][y];
}
if(elFieldIntensityMap[x][y]>maxElFieldIntensity)
maxElFieldIntensity = elFieldIntensityMap[x][y];
if(elFieldIntensityMap[x][y]<minElFieldIntensity)
minElFieldIntensity = elFieldIntensityMap[x][y];
}
}
}
}
void Field::addCharge(){
clear();
update();
}
void Field::removeCharge(int x, int y){
for(int i=0; i<qCharges.size(); i++)
if(qCharges[i]->isPointInside(x,y)){
qCharges.erase(qCharges.begin() + i);
break;
}
clear();
update();
}
void Field::selectCharge(int x, int y){
for(int i=0; i<qCharges.size(); i++)
if(qCharges[i]->isPointInside(x,y)){
showing_pop_up = true;
selected_index = i;
break;
}
}
//Clears Field and return to an initial stage
//It's called whenever you change modality or add or remove charge
void Field::clear(){
elFieldLines.clear();
eqPotLines.clear();
eqPotPoints.clear();
for(int i=0; i<potIntensityMap.size(); i++){
potIntensityMap[i].clear();
}
potIntensityMap.clear();
potIntensityMap = std::vector< std::vector<double> >(windowWidth, std::vector<double>(windowHeight));
for(int i=0; i<elFieldIntensityMap.size(); i++){
elFieldIntensityMap[i].clear();
}
elFieldIntensityMap.clear();
elFieldIntensityMap = std::vector< std::vector<double> >(windowWidth, std::vector<double>(windowHeight));
for(int i=0; i<arrows.size(); i++){
arrows[i].clear();
}
arrows.clear();
}
//Constructor initializes vectors of vectors to prevent segmentation fault
Field::Field(){
potIntensityMap = std::vector< std::vector<double> >(windowWidth, std::vector<double>(windowHeight));
elFieldIntensityMap = std::vector< std::vector<double> >(windowWidth, std::vector<double>(windowHeight));
}