-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
220 lines (191 loc) · 4.4 KB
/
sketch.js
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
let res = 20;
let cantLineas = 2000;
let cargaMuestra;
let cargas;
// Controles
let cargaSlider;
let lineasSlider;
let resSlider;
function setup() {
let canvas = createCanvas(windowWidth, windowHeight);
canvas.mousePressed(mousePressedOnCanvas);
cargas = [
new Carga(createVector(width / 2 - 50, height / 2), 30),
new Carga(createVector(width / 2 + 50, height / 2), -30),
new Carga(createVector(width / 2, height / 2 - 50), -1),
];
// Inicializacion de controles
lineasSlider = createSlider(0, 5000, cantLineas, 100);
lineasSlider.position(10, height - 50);
resSlider = createSlider(0, 50, res, 1);
resSlider.position(lineasSlider.width * 2 - 25, height - 50);
cargaSlider = createSlider(-50, 50, cargas[1].charge, 1);
cargaSlider.position(10, 50);
cargaSlider.hide();
}
function draw() {
background(0);
cantLineas = lineasSlider.value();
res = resSlider.value();
mostrarTextoControles();
// Dibujo lineas
let cargasNoCero = cargas.some((q) => {
return q.charge != 0;
});
if (cargas.length && cargasNoCero) {
drawLines();
}
// Dibujo las cargas
cargas.forEach((q) => {
q.update();
});
}
// Calcula el vector campo electrico correspondiente en la posicion de q0
// generado por la presencia de las cargas pasadas por parametro
const campoElectrico = (q0, cargas) => {
let k = 9 * 10 ** 9;
let sum = createVector(0, 0, 0);
cargas.forEach((q) => {
let v = p5.Vector.sub(q0, q.pos);
let vCubed = p5.Vector.mag(v) ** 3;
sum.add(v.mult(q.charge / vCubed));
});
return sum.mult(k);
};
class Carga {
constructor(pos, charge) {
this.dragging = false;
this.selected = false;
this.charge = charge;
this.pos = pos;
this.diam = 20;
this.color = 'white';
}
update() {
if (this.dragging) {
this.move(mouseX, mouseY);
}
if (this.selected) {
this.updateFromControls();
this.mostrarTexto();
}
this.show();
}
show() {
push();
strokeWeight(this.diam);
if (this.charge == 0) {
this.color = 'white';
} else {
this.color = this.charge < 0 ? 'blue' : 'red';
}
stroke(this.color);
point(this.pos);
pop();
}
isPressed() {
return dist(this.pos.x, this.pos.y, mouseX, mouseY) <= this.diam / 2;
}
move(x, y) {
this.pos = createVector(x, y);
}
updateControls() {
cargaSlider.value(this.charge);
}
mostrarTexto() {
push();
textSize(26);
fill(255);
text(
this.charge,
cargaSlider.x + cargaSlider.width + 10,
cargaSlider.y + 20
);
pop();
push();
textSize(15);
fill(255);
text('Carga', cargaSlider.x, cargaSlider.y - 10);
pop();
}
updateFromControls() {
this.charge = cargaSlider.value();
}
}
const drawLines = () => {
push();
for (let i = 0; i < cantLineas; i++) {
let x = random(width);
let y = random(height);
let v = createVector(x, y);
let ceVec = campoElectrico(v, cargas);
resul = p5.Vector.div(ceVec, campoElectrico(v, cargas).mag()).mult(res);
stroke(255, 70);
line(v.x, v.y, v.x + resul.x, v.y + resul.y);
}
pop();
};
const showControls = () => {
cargaSlider.show();
};
const hideControls = () => {
cargaSlider.hide();
};
const mostrarTextoControles = () => {
push();
textSize(26);
fill(255);
text(res, resSlider.x + resSlider.width + 10, resSlider.y + 20);
text(
cantLineas,
lineasSlider.x + lineasSlider.width + 10,
lineasSlider.y + 20
);
pop();
push();
textSize(15);
fill(255);
text('Cantidad de lineas', lineasSlider.x, lineasSlider.y - 10);
text('Largo de lineas', resSlider.x, resSlider.y - 10);
pop();
};
// Eventos
function mousePressedOnCanvas() {
cargas.forEach((q) => {
if (q.isPressed()) {
q.updateControls();
q.dragging = true;
q.selected = true;
} else {
q.selected = false;
}
});
let ningunoSeleccionado = cargas.some((q) => {
return q.selected;
});
if (!ningunoSeleccionado) {
hideControls();
} else {
showControls();
}
}
function mouseReleased() {
cargas.forEach((q) => {
q.dragging = false;
});
}
function doubleClicked() {
let clickeado = cargas.some((q, i) => {
if (q.isPressed()) {
cargas.splice(i, 1);
return true;
}
return false;
});
if (!clickeado) {
cargas.push(new Carga(createVector(mouseX, mouseY), 1));
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}