-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_charge.cpp
130 lines (102 loc) · 3.46 KB
/
custom_charge.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
#include "header.h"
#include <cmath>
CustomCharge::CustomCharge(int x, int y, int size, double value) : Charge(x, y, size, value){}
bool CustomCharge::isPointInside(int tx, int ty){
for(int i=0; i<shape_map.size(); i++)
if(((shape_map[i].x+x) == tx) && ((shape_map[i].y+y) == ty))
return true;
return false;
}
//The potential value is calculated as if the custom charge was composed of circle charge of size 1
double CustomCharge::potInPoint(int tx, int ty){
double totPotValue = 0;
double distance;
double dividedValue = value / shape_map.size();
for(int i=0; i<shape_map.size(); i++){
distance = sqrt( pow(tx-(shape_map[i].x+x), 2) + pow(ty-(shape_map[i].y+y), 2) );
totPotValue += (k0*(dividedValue)) / distance;
}
return totPotValue;
}
void CustomCharge::drawCharge(sf::RenderWindow& window){
std::vector<sf::Vertex> shapePixels;
for(int i=0; i<shape_map.size(); i++){
sf::Vertex pixel(sf::Vector2f(shape_map[i].x+x, shape_map[i].y+y), GREY_CHARGE);
shapePixels.push_back(pixel);
}
window.draw(&shapePixels[0], shapePixels.size(), sf::Points);
}
Vector CustomCharge::elFieldInPoint(int tx, int ty){
double distance, alpha;
double totElFieldValueX = 0, totElFieldValueY = 0;
double dividedValue = value / shape_map.size();
for(int i=0; i<shape_map.size(); i++){
distance = sqrt( pow(tx-(shape_map[i].x+x), 2) + pow(ty-(shape_map[i].y+y), 2) );
alpha = mainField.angleFromPoints(tx,ty,shape_map[i].x+x,shape_map[i].y+y);
totElFieldValueX += cos(alpha)*((k0*dividedValue)/pow(distance,2));
totElFieldValueY += sin(alpha)*((k0*dividedValue)/pow(distance,2));
}
return Vector(totElFieldValueX,totElFieldValueY,0,0);
}
//Finds the external point with closer alpha direction
Point CustomCharge::externalPoint(double alpha){
if (external_points.size()==0 && shape_map.size()!=0)
findExternalPoints();
double deltaMin=std::abs(mainField.angleFromPoints(external_points[0].x,external_points[0].y,size/2,size/2)-alpha);
int minPoint = 0;
for(int i=1; i<external_points.size(); i++){
double alphaPoint = mainField.angleFromPoints(external_points[i].x,external_points[i].y,size/2,size/2);
if(std::abs(alphaPoint-alpha)<deltaMin){
deltaMin=std::abs(alphaPoint-alpha);
minPoint=i;
}
}
return Point(external_points[minPoint].x+x, external_points[minPoint].y+y, alpha);
}
//Finds the external points of the shape
void CustomCharge::findExternalPoints(){
std::vector< std::vector<bool> > shape_map_xy(size, std::vector<bool>(size));
for(int i=0; i<shape_map.size(); i++)
shape_map_xy[shape_map[i].x][shape_map[i].y]=true;
bool firstFound = false;
bool lastFound = false;
int xlast, ylast;
for(int x=0; x<shape_map_xy.size(); x++){
for(int y=0; y<shape_map_xy[x].size(); y++){
if(shape_map_xy[x][y]){
if(!firstFound){
firstFound=true;
external_points.push_back(Point(x, y));
continue;
}
xlast=x;
ylast=y;
lastFound=true;
}
}
firstFound=false;
if(lastFound){
external_points.push_back(Point(xlast, ylast));
lastFound=false;
}
}
for(int y=0; x<shape_map_xy.size(); y++){
for(int x=0; y<shape_map_xy[x].size(); x++){
if(shape_map_xy[x][y]){
if(!firstFound){
firstFound=true;
external_points.push_back(Point(x, y));
continue;
}
xlast=x;
ylast=y;
lastFound=true;
}
}
firstFound=false;
if(lastFound){
external_points.push_back(Point(xlast, ylast));
lastFound=false;
}
}
}