-
Notifications
You must be signed in to change notification settings - Fork 1
/
classes.js
547 lines (487 loc) · 18.7 KB
/
classes.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
class Arrow{
//class currently only works for 2d arrows drawn at z = 0 on a 3d plot.
//need to edit GetDrawData if you want 3d arrows.
constructor(x1, y1, x2, y2, HeadSize){
//x1 and y1 are coords of tail of arrow
//x2 and y2 are coords of head of arrow
//HeadSize sets the size of the arrowhead.
this.TailPos = [x1, y1]; //tail coords of arrow
this.HeadPos = [x2, y2]; //head coords of arrow
this.HeadSize = HeadSize; //length of lines that makes arrowheads
this.HeadAngle = Math.PI/4; //angle between arrowhead lines and main body of arrow
//length of arrow
this.r = this.GetLength(this.HeadPos, this.TailPos);
//angle of arrow
this.theta = this.GetTheta(this.HeadPos, this.TailPos);
}
GetHeadPos(){ //gets head position
return this.HeadPos;
}
GetTailPos(){ //gets tail position
return this.TailPos;
}
GetLength(){ //get length of arrow
return Math.sqrt((this.HeadPos[0] - this.TailPos[0])**2 + (this.HeadPos[1] - this.TailPos[1])**2);
}
GetTheta(){ //get angle arrow makes with horizontal
return Math.atan2((this.HeadPos[1] - this.TailPos[1]), (this.HeadPos[0] - this.TailPos[0]));
}
GetDrawData3D(){
//gets coordinates for drawing lines to make arrows for when using a 3D plot.
//need arrays of x values and arrays of y values
//first line is main body of arrow
//let FirstLine = [[this.TailPos[0], this.HeadPos[0]], [this.TailPos[1], this.HeadPos[1]]];
let Ax = this.HeadPos[0] - this.HeadSize*Math.sin((Math.PI/2) - this.theta + this.HeadAngle);
let Ay = this.HeadPos[1] - this.HeadSize*Math.cos((Math.PI/2) - this.theta + this.HeadAngle);
//let PointA = [Ax, Ay];
let Bx = this.HeadPos[0] - this.HeadSize*Math.sin((Math.PI/2) - this.theta - this.HeadAngle);
let By = this.HeadPos[1] - this.HeadSize*Math.cos((Math.PI/2) - this.theta - this.HeadAngle);
//let PointB = [Bx, By];
let FirstLine = {}; //main line that makes up arrow
let SecondLine = {}; //one of the lines that makes arrowhead
let ThirdLine = {}; //one of lines that makes arrowhead
if (this.r <= 0.000001){ //pointless section - was trying to make tiny/negligible arrows invisible
FirstLine = {
type: "scatter3d",
mode: "lines",
x: [this.TailPos[0], this.HeadPos[0]],
y: [this.TailPos[1], this.HeadPos[1]],
z: [0,0],
line: {color: "blue", width: 3},
hoverinfo: "skip"
};
SecondLine = {
type: "scatter3d",
mode: "lines",
x: [this.HeadPos[0], Ax],
y: [this.HeadPos[1], Ay],
z: [0,0],
line: {color: "blue", width: 3},
hoverinfo: "skip"
};
ThirdLine = {
type: "scatter3d",
mode: "lines",
x: [this.HeadPos[0], Bx],
y: [this.HeadPos[1], By],
z: [0,0],
line: {color: "blue", width: 3},
hoverinfo: "skip"
};
}else{ //section that draws arrows based on the coordinates calculated previously
FirstLine = {
type: "scatter3d",
mode: "lines",
x: [this.TailPos[0], this.HeadPos[0]],
y: [this.TailPos[1], this.HeadPos[1]],
z: [0,0],
line: {color: "blue", width: 3},
hoverinfo: "skip"
};
SecondLine = {
type: "scatter3d",
mode: "lines",
x: [this.HeadPos[0], Ax],
y: [this.HeadPos[1], Ay],
z: [0,0],
line: {color: "blue", width: 3},
hoverinfo: "skip"
};
ThirdLine = {
type: "scatter3d",
mode: "lines",
x: [this.HeadPos[0], Bx],
y: [this.HeadPos[1], By],
z: [0,0],
line: {color: "blue", width: 3},
hoverinfo: "skip"
};
}
return [FirstLine, SecondLine, ThirdLine];
}
GetDrawData2D(){
//gets coordinates for drawing arrows when using a 2D plot
//need arrays of x values and arrays of y values
//first line is main body of arrow
//let FirstLine = [[this.TailPos[0], this.HeadPos[0]], [this.TailPos[1], this.HeadPos[1]]];
let Ax = this.HeadPos[0] - this.HeadSize*Math.sin((Math.PI/2) - this.theta + this.HeadAngle);
let Ay = this.HeadPos[1] - this.HeadSize*Math.cos((Math.PI/2) - this.theta + this.HeadAngle);
//let PointA = [Ax, Ay];
let Bx = this.HeadPos[0] - this.HeadSize*Math.sin((Math.PI/2) - this.theta - this.HeadAngle);
let By = this.HeadPos[1] - this.HeadSize*Math.cos((Math.PI/2) - this.theta - this.HeadAngle);
//let PointB = [Bx, By];
//let SecondLine = [[this.HeadPos[0], Ax], [this.HeadPos[1], Ay]];
//let ThirdLine = [[this.HeadPos[0], Bx], [this.HeadPos[1], By]];
let FirstLine = {};
let SecondLine = {};
let ThirdLine = {};
if (this.r == 0){ //should make small arrows invisible
FirstLine = {
type: "scatter",
mode: "lines",
x: [this.TailPos[0], this.HeadPos[0]],
y: [this.TailPos[1], this.HeadPos[1]],
line: {color: "blue", width: 0},
hoverinfo: "skip"
};
SecondLine = {
type: "scatter",
mode: "lines",
x: [this.HeadPos[0], Ax],
y: [this.HeadPos[1], Ay],
line: {color: "blue", width: 0},
hoverinfo: "skip"
};
ThirdLine = {
type: "scatter",
mode: "lines",
x: [this.HeadPos[0], Bx],
y: [this.HeadPos[1], By],
line: {color: "blue", width: 0},
hoverinfo: "skip"
};
}else{ //larger arrows are visible (compare the widths)
FirstLine = {
type: "scatter",
mode: "lines",
x: [this.TailPos[0], this.HeadPos[0]],
y: [this.TailPos[1], this.HeadPos[1]],
line: {color: "blue", width: 2},
hoverinfo: "skip"
};
SecondLine = {
type: "scatter",
mode: "lines",
x: [this.HeadPos[0], Ax],
y: [this.HeadPos[1], Ay],
line: {color: "blue", width: 1},
hoverinfo: "skip"
};
ThirdLine = {
type: "scatter",
mode: "lines",
x: [this.HeadPos[0], Bx],
y: [this.HeadPos[1], By],
line: {color: "blue", width: 1},
hoverinfo: "skip"
};
}
return [FirstLine, SecondLine, ThirdLine];
}
}
class volume_element {
constructor(x, y, w, l) {
this.y = y;
this.x = x;
this.w = w;
this.l = l;
}
}
class charge {
constructor(q, x, y){
this.q = q;
this.x = x;
this.y = y;
this.r = R;
this.clicked = false;
//Colour of charge in relation to magnitude and polarity
if (q > 0){
let tune1 = Math.round(180 - 120*(1-Math.exp(-Math.abs(q))));
let tune2 = Math.round(90*(Math.exp(-Math.abs(q))));
this.color = "rgb(255," + tune1.toString() + "," + tune2.toString() + ")";
} else if (q < 0){
let tune1 = Math.round(120*(Math.exp(-Math.abs(q))));
let tune2 = Math.round((180 - 120*(1-Math.exp(-Math.abs(q)))));
this.color = "rgb(" + tune1.toString() + "," + tune2.toString() + ",255)";
} else {
this.color = "#00FF00";
}
//Relate the number of field lines to the magnitude of the charge
this.n_lines = 3 + 10*Math.abs(q);
}
//Cursor interactivity
pressed(){
if (dist(mouseX, mouseY, this.x, this.y) < this.r){
this.clicked = true;
}
}
dragposition(){
this.x = mouseX;
this.y = mouseY;
}
intersect(){
let areintersecting = false;
for (let i = 0; i < allpoints.length; i++) {
if(allpoints[i] != this){
if (parseFloat(dist(mouseX, mouseY, allpoints[i].x, allpoints[i].y)) <= R*2){
areintersecting = true;
}
}
}
if (parseFloat(Math.abs(mouseX-v1.x)) <= R && v1.y - R <= mouseY && mouseY <= v1.y + v1.l + R){
areintersecting = true;
}
if (parseFloat(Math.abs(mouseX-v1.x - v1.w)) <= R && v1.y - R <= mouseY && mouseY <= v1.y + v1.l + R){
areintersecting = true;
}
if (parseFloat(Math.abs(mouseY-v1.y - v1.l)) <= R && v1.x - R <= mouseX && mouseX <= v1.x + v1.w + R){
areintersecting = true;
}
if (parseFloat(Math.abs(mouseY - v1.y)) <= R && v1.x - R <= mouseX && mouseX <= v1.x + v1.w + R){
areintersecting = true;
}
return areintersecting;
}
}
class charge_selector{
constructor(q, x, y){
this.q = q;
this.x = x;
this.y = y;
this.r = R;
this.clicked = false;
//Colour of charge in relation to magnitude and polarity
if (q == 0){
this.color = "#00FF00";
} else if (q > 0){
let tune1 = Math.round(180 - 120*(1-Math.exp(-Math.abs(q))));
let tune2 = Math.round(90*(Math.exp(-Math.abs(q))));
this.color = "rgb(255," + tune1.toString() + "," + tune2.toString() + ")";
} else {
let tune1 = Math.round(120*(Math.exp(-Math.abs(q))));
let tune2 = Math.round((180 - 120*(1-Math.exp(-Math.abs(q)))));
this.color = "rgb(" + tune1.toString() + "," + tune2.toString() + ",255)";
}
}
//God's hand to pull charge out of thin air
pressed(){
if (dist(mouseX, mouseY, this.x, this.y) < this.r){
if (this.q != 0) {
let q = new charge(this.q, this.x, this.y);
allpoints.push(q);
}
}
}
}
class line_element {
constructor(x, y){
this.x = x;
this.y = y;
this.clicked = false;
this.r = 50;
}
//Cursor interactivity
pressed(){
if (dist(mouseX, mouseY, this.x, this.y) < this.r){
this.clicked = true;
}
}
dragposition(){
this.x = mouseX;
this.y = mouseY;
}
}
class Circuit {
constructor(x, y, type, args){
this.x=x;
this.y=y;
this.type=type;
if (type=== "circle"){
this.diam = args.diam;
this.diam1=args.diam;
} else if (type === "arcs" ){
this.args = {
diam:args.diam, //each of them is an array of the same length containing the starting angle and diameter of the arcs
theta:args.theta.sort(),
diam1:args.diam.splice(0),
};
for (let i=0; i<this.args.theta.length; i++){
if (this.args.theta[i]>=Math.PI){
this.args.theta[i] -= 2*Math.PI
}
}
this.args.theta.sort((a, b) => a - b);
}
else if (type === "rectangle"){
this.h = args.height;
this.w= args.width;
this.h1=args.height;
this.w1=args.width;
}
};
//if want to change the shape of the circuit, this is to alter
drawCircuit() {
push();
stroke('red');
strokeWeight(3);
noFill();
translate(this.x, this.y);
if (this.type==="circle"){
ellipse(0, 0, this.diam, this.diam);
} else if (this.type==="arcs"){
let index = this.args.diam.length;
for (let k=0; k<index; k++){
let diam0, diam1, theta0, theta1;
if (k<index-1) {
diam0 = this.args.diam[k], diam1 = this.args.diam[k + 1], theta0 = this.args.theta[k] + 2 * Math.PI, theta1 = 2 * Math.PI + this.args.theta[k + 1];
}
else {diam0= this.args.diam[index-1], diam1=this.args.diam[0]; theta0= this.args.theta[index-1], theta1 = this.args.theta[0];}
arc(0, 0, diam0, diam0, theta0, theta1);
line(diam0/2*Math.cos(theta1) , diam0/2*Math.sin(theta1), diam1/2*Math.cos(theta1), diam1/2*Math.sin(theta1));
}
} else if (this.type==="rectangle"){
rectMode(RADIUS);
rect(0, 0, this.w/2, this.h/2);
}
stroke (200);
line (-3, 0, 3, 0);
line(0, -3, 0, 3);
pop();
};
//this as well so that we follow the circuit
drawPath() {
//have arc being bolder as we go on it--> from angle -PI/2 to angle
push();
strokeWeight(3);
stroke('green');
noFill();
translate(this.x, this.y);
if (this.type ==="circle") {
arc(0, 0, this.diam, this.diam, 3 * Math.PI / 2, theta);
} else if (this.type==="arcs") {
/*let maxM = -1; //draw further than maxM
for (let m = 0; m < this.args.theta.length; m++) {
if (theta >= this.args.theta[m]) {
maxM = m;
}
}
if (this.args.theta[maxM] >= - Math.PI / 2 && theta>= -Math.PI/2) {
for (let m = 0; m < maxM; m++) {
arc(0, 0, this.args.diam[m], this.args.diam[m], Math.max(this.args.theta[m], -Math.PI / 2), this.args.theta[m + 1]);
}
arc(0, 0, this.args.diam[maxM], this.args.diam[maxM], this.args.theta[maxM], theta);
} else if (theta<-Math.PI/2){
let m=0;
for (m=0; m<this.args.theta.length-1; m++){
arc(0, 0, this.args.diam[m], this.args.diam[m], Math.max(this.args.theta[m], -Math.PI / 2), this.args.theta[m + 1]);
}
arc(0,0, this.args.diam[m], this.args.diam[m], this.args.theta[m], Math.min(theta, this.args.theta[0]) );
for (m=0; m<maxM; m++){
arc(0,0, this.args.diam[m], this.args.diam[m], Math.max(this.args.theta[m], -Math.PI), this.args.theta[m + 1] );
}
arc(0, 0, this.args.diam[maxM], this.args.diam[maxM], this.args.theta[maxM], theta);
}
else if (theta>=-Math.PI/2 && this.args.theta[maxM]<=-Math.PI/2){
arc(0, 0, this.args.diam[maxM], this.args.diam[maxM], -Math.PI/2, theta);
}*/
} else if (this.type ==="rectangle"){
let h = this.h, w = this.w
let alpha = atan2(h,w);
if (theta>= -Math.PI/2 &&theta< -alpha ){
line(0, -h/2, -h/2/Math.tan(theta), -h/2);
} else if (theta>=-alpha&&theta<=alpha){
line(0, -h/2, w/2, -h/2);
line(w/2, -h/2, w/2, w*Math.tan(theta)/2);
} else if (theta>=alpha && theta<=Math.PI-alpha){
line(0, -h/2, w/2, -h/2);
line(w/2, -h/2, w/2, h/2);
line (w/2, h/2, h/2/Math.tan(theta), h/2);
} else if (theta>= Math.PI-alpha || theta<= alpha- Math.PI){
line(0, -h/2, w/2, -h/2);
line(w/2, -h/2, w/2, h/2);
line (w/2, h/2, -w/2, h/2);
line(-w/2, h/2, -w/2, -w*Math.tan(theta)/2);
} else if (theta> alpha-Math.PI &&theta< -Math.PI/2){
line(0, -h/2, w/2, -h/2);
line(w/2, -h/2, w/2, h/2);
line (w/2, h/2, -w/2, h/2);
line(-w/2, h/2, -w/2, -h/2);
line(-w/2, -h/2, -h/2/Math.tan(theta), -h/2);
}
}
pop();
}
};
class Wire {
constructor(x, y, A, index) {
this.x = x;
this.y = y;
this.value = A;
if (A >= 0) {
this.valueSign = 1
}
else {
this.valueSign = -1
}
this.widthInner = 3;
this.widthOuter = 12;
this.index = index;
this.clicked = false;
this.color = 0;
}
intersect() { //check if we are close to a forbidden area
let areintersecting = false;
for (let i = 0; i < currentContainer.length; i++) {
if (currentContainer[i] != this) {
if (parseInt(dist(mouseX, mouseY, currentContainer[i].x, currentContainer[i].y)) <= this.widthOuter + 4) {
areintersecting = true
}
}
}
//also case to avoid putting it out of the canvas
if (mouseIsPressed && (mouseX <= 4 || mouseY <= 4 || mouseX >= width || mouseY >= height - 10)) {
areintersecting = true;
}
return areintersecting;
}
pressed() {
let distance = dist(mouseX, mouseY, this.x, this.y);
if (distance <= this.widthOuter + 4 && !mouseWasPressed) {
someWireClose = true;
if (mouseIsPressed) {
this.clicked = true;
mouseWasPressed = true;
}
}
if (this.clicked && !mouseIsPressed) {
this.clicked = false;
mouseWasPressed = false;
}
}
updateWirePos() {
this.pressed();
if ((!this.intersect()) && this.clicked) {
this.x = mouseX;
this.y = mouseY;
}
}
selectingWire() {
if (this.clicked) {
wireSelected = this.index;
$('#currentSlider').val(this.value.toString());
}
}
drawWire() {
push();
stroke(this.color);
noFill();
ellipse(this.x, this.y, this.widthOuter, this.widthOuter);
fill(0);
strokeWeight(1);
//change for cross if current is positive, dot if negative
if (this.valueSign >= 0) {
line(this.x - this.widthInner, this.y - this.widthInner, this.x + this.widthInner, this.y + this.widthInner);
line(this.x + this.widthInner, this.y - this.widthInner, this.x - this.widthInner, this.y + this.widthInner);
} else {
//negative current represented by vector 3D notation (small dot in the middle)
strokeWeight(3);
ellipse(this.x, this.y, this.widthInner, this.widthInner);
}
strokeWeight(1);
textSize(15);
let textI = this.value;
fill(255,100,10);
text(textI, this.x + 10, this.y + 10);
pop();
}
};