-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElectricField.cpp
91 lines (81 loc) · 1.89 KB
/
ElectricField.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
#include "ElectricField.h"
// Constructors
ElectricField::ElectricField(Mesh& mesh)
{
field.resize(mesh.nodes.size(), sf::Vector2f(0.f, 0.f));
}
ElectricField::ElectricField(sf::Vector2i centre, bool polarity)
{
this -> x0 = static_cast<float>(centre.x);
this -> y0 = static_cast<float>(centre.y);
if(polarity)
{
// Positive charge
this -> flux = 0;
}
else
{
// Negative charge
this -> flux = +180;
}
}
// Destructor
ElectricField::~ElectricField(){}
// Accessors
// Mutators
void ElectricField::scale()
{
float A = 10.f, B = 100.f;
std::vector<float> scaled_rho;
// Extract the magnitude only from the field values
for(int j=0; j < this -> field.size(); j++)
{
scaled_rho.push_back(this -> field.at(j).x);
}
// Extract minimum(=a) and maximum(=b) values of the field
float a = *std::min_element(scaled_rho.begin(), scaled_rho.end());
if(a==0.f)
{
a = EPS;
}
float b = *std::max_element(scaled_rho.begin(), scaled_rho.end());
float c = -b/a;
// Build the affine map [a,b] -> [A,B]
float beta = (B-c*A)/(1.f-c);
float alpha = (A-beta)/a;
// Scale field values
for(int j=0; j < this -> field.size(); j++)
{
this -> field.at(j).x = alpha*scaled_rho.at(j) + beta;
}
}
// Other Methods
sf::Vector2f ElectricField::getFieldValues(float x, float y)
{
float rho =0.f, theta=0.f;
if(sqrt(pow(this -> x0 - x, 2) + pow(this -> y0 - y, 2)) > 40)
{
rho = 1/(pow(this -> x0 - x, 2) + pow(this -> y0 - y, 2));
if(this -> x0 - x > 0)
{
theta = this -> flux + atan((this -> y0 - y)/(this -> x0 - x))*(180/PI);
}
else
{
theta = this -> flux + atan((this -> y0 - y)/(this -> x0 - x))*(180/PI) - 180;
}
}
return sf::Vector2f(rho, theta);
}
void ElectricField::update(Mesh& mesh)
{
field.clear();
field.resize(mesh.nodes.size(), sf::Vector2f(0.f, 0.f));
}
void ElectricField::render(sf::RenderTarget* target)
{
for(auto* arrow: this -> arrows)
{
arrow -> render(target);
}
}