-
Notifications
You must be signed in to change notification settings - Fork 3
/
meclib.js.bak
2198 lines (2124 loc) · 98.8 KB
/
meclib.js.bak
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// https://github.com/mkraska/meclib/wiki
// version info
const versionText= "JXG "+JXG.version+" iMeclib 2024 09 24";
const highlightColor = "orange";
const movableLineColor = "blue";
const loadColor = "blue";
const defaultMecLayer = 6;
let pxunit = 1/40; // is reset by "grid"
let a = 16*pxunit; // is reset by "grid"
const deg2rad = Math.PI/180, rad2deg = 180/Math.PI;
const tolPointLine = 0.001;
let xscale = 1, yscale = 1; // default scale for infobox, can be modified by "grid"
let dpx = 1, dpy = 1; // default decimal precision for infobox, can be modified by "grid"
// infobox settings, further settings after board initiation
JXG.Options.infobox.layer = defaultMecLayer+5;
JXG.Options.infobox.strokeColor = 'black';
JXG.Options.infobox.cssStyle = 'background-color: #ffffffdd;'
// snap settings. Interactive objects are handled explicity
JXG.Options.point.snapToGrid = false; // grid snap spoils rotated static objects
JXG.Options.point.snapSizeX = 0.1;
JXG.Options.point.snapSizeY = 0.1;
// interactive objects are released explicitly
JXG.Options.point.fixed = true;
JXG.Options.line.fixed = true;
JXG.Options.circle.fixed = true;
// label settings
JXG.Options.text.useMathJax = true;
JXG.Options.text.parse = false;
JXG.Options.label.useMathJax = true;
JXG.Options.label.offset = [0, 0];
JXG.Options.label.anchorY = 'middle';
// suppress automatic labels
JXG.Options.point.name = "";
// highlighting is activated explicitly for interactive objects
JXG.Options.curve.highlight = false;
JXG.Options.label.highlight = false;
JXG.Options.text.highlight = false;
JXG.Options.circle.highlight = false;
JXG.Options.line.highlight = false;
JXG.Options.polygon.highlight = false;
JXG.Options.polygon.borders.highlight = false;
JXG.Options.point.highlight = false;
// grid control
JXG.Options.axis.ticks.insertTicks = false;
JXG.Options.grid.drawZero = true;
// Styles
const [nodeStyle, pointStyle, silentPStyle, controlSnapStyle, barStyle, normalStyle, thinStyle, hatchStyle] = [
// nodes (hinges)
{ fillcolor: 'white', strokeColor: 'black', size: 2, strokeWidth: 1.5 },
// points (black dots)
{ fillcolor: 'black', strokeColor: 'black', size: 1, strokeWidth: 1 },
// invisible points with infobox
{ size: 0, withLabel: false },
// grid snap for control points
{ snapToGrid: true, snapToPoints: true, attractorDistance: 0.2, fixed: false, layer: 11 },
// Style for bars
{ strokewidth: 4, strokecolor: "black" },
// Normal line (body outline)
{ strokeWidth: 2, strokeColor: 'black', lineCap: 'round' },
// helper line
{ strokeWidth: 1, strokeColor: 'black', lineCap: 'round' },
// hatch style, must be a function because depending on pxunit
() => ({ fixed: true, width: 5 * pxunit, frequency: 5 * pxunit, angle: 45 * deg2rad, layer: 8, strokeColor: 'black' })
];
const board = JXG.JSXGraph.initBoard(divid, {
boundingbox: [-5, 5, 5, -5], //default values, use "grid" to customize
axis: false, grid:true, showNavigation:false, showCopyright:false,
keepAspectRatio:false, resize: {enabled: false, throttle: 200},
pan: {enabled:false}, //suppress uninteded pan on touchscreens
keyboard:{enabled:false} //would spoil textinput in momentGen and forceGen
});
let state;
let stateInput;
// make infobox optionally relative to a given point (define p.ref to [xref, yref])
board.infobox.distanceY = 20;
//board.infobox.setAttribute({highlight:false});
board.highlightInfobox = function(x, y , el) {
let ref = [0,0];
let scale = [xscale,yscale];
let dp = [dpx,dpy];
let lbl = '';
if (typeof (el.ref) == 'function') {ref = el.ref()}
else if (typeof(el.ref) != 'undefined') {ref = el.ref}
if (typeof (el.scale) != 'undefined') {scale = el.scale}
if (typeof (el.dp) != 'undefined') {dp = el.dp}
if (typeof (el.infoboxlabel) == 'string') {lbl = el.infoboxlabel}
this.infobox.setText(
lbl+'('+((parseFloat(x)-ref[0])*scale[0]).toFixed(dp[0]) + ', ' + ((parseFloat(y)-ref[1])*scale[1]).toFixed(dp[1])+ ')')
};
// angular dimension with a single or double arrow (handles arrow, arrow1 and arrow2)
class angle {
constructor(data) {
this.d = data.slice(0); //copy
// base line
this.p1 = board.create('point', data[2], {size:3, name:'p1', fixed:false, visible:false});
this.p3 = board.create('point', data[3], {size:3, name:'p3', fixed:false, visible:false});
this.c2 = board.create('circle', [this.p1, this.p3], {opacity:0.5, visible:false});
this.l1 = board.create('segment', [this.p1, this.p3], {withlabel:false, ...thinStyle});
// second line
const a0 = this.l1.getAngle();
console.log('here is a0:' + a0);
const le = this.l1.L();
const a1 = a0+data[5]*deg2rad;
this.ln = board.create('line', [this.p1, plus(XY(this.p1), rect(le,a1))], {withlabel:false, ...thinStyle, straightFirst:true, visible:false});
this.in2 = board.create('intersection', [this.ln, this.c2], {visible:false});
this.p2 = board.create('glider', [this.in2.X(), this.in2.Y(), this.c2], {fixed:false, name:'p2', visible:false});
this.l2 = board.create('segment', [this.p1, this.p2], {withlabel:false, ...thinStyle});
// arc with arrows
this.p4 = board.create('point', plus( XY(this.p1), rect(data[4],a0) ), {visible:false, name:'p4'});
this.c1 = board.create('circle', [this.p1, this.p4], {opacity:0.5, visible:false});
this.int1 = board.create('intersection', [this.c1, this.l1], {name:'int1', visible:false});
this.int2 = board.create('intersection', [this.c1, this.l2], {name:'int2', visible:false});
if (data[0] == "angle" ) {
this.arc = board.create('minorArc', [this.p1, this.int1, this.int2],
{ ...thinStyle } ) }
if (data[0] == "angle1" ) {
this.arc = board.create('minorArc', [this.p1, this.int1, this.int2],
{ ...thinStyle, lastArrow:{type: 1, size: 6}})}
if (data[0] == "angle2" ) {
this.arc = board.create('minorArc', [this.p1, this.int1, this.int2],
{ ...thinStyle, firstArrow:{type: 1, size: 6},lastArrow:{type: 1, size: 6}})}
// label
this.mp = board.create('midpoint', [this.int1, this.int2], {visible:false, name:'mp'});
this.bis = board.create('line', [this.mp, this.p1], {visible:false}); //bisector
const al = (a0+a1)/2; // angular position of label
if (data[1] == ".") {
const rl = data[4]*0.6;
this.pr = board.create('point', plus(XY(this.p1), rect(rl,al)), {visible:false, name:'pr'});
this.cl = board.create('circle', [this.p1, this.pr], {strokeColor:'red', opacity:0.5, visible:false} );
this.p5 = board.create('intersection', [this.bis, this.cl, 1], { name:"" , showInfobox:false, fillcolor:'black',strokeColor:'black',size:0.5, strokeWidth:0});
}
else {
const rl = data[4]+10*pxunit;
this.pr = board.create('point', plus(XY(this.p1), rect(rl,al)), {visible:false, name:'pr'});
this.cl = board.create('circle', [this.p1, this.pr], {strokeColor:'red', opacity:0.5, visible:false} );
this.p5 = board.create('intersection', [this.bis, this.cl, 1], {name:toTEX(data[1]), showInfobox:false, size:0});
}
// Enable object animation
this.p0 = board.create('point', [0,0], {fixed:true, visible:false, name:'p0'});
const diffX = this.p1.X() - this.p0.X();
const diffY = this.p1.Y() - this.p0.Y();
const t1 = board.create('transform', [() => this.p1.X() - diffX, () => this.p1.Y() - diffY], {type: 'translate'});
t1.bindTo([this.in2, this.p3, this.p4, this.pr]);
}
data() {return this.d}
name() {return '"'+this.d[1]+'"'}
}
// Fachwerkstab
class bar {
constructor(data) {
this.state = (typeof data[data.length-1] == 'string') ? data.pop() : "SHOW";
this.d = data.slice(0);
// line
this.p1 = board.create('point',data[2],{withlabel:false, ...nodeStyle, fixed:true});
this.p2 = board.create('point',data[3],{withlabel:false, ...nodeStyle, fixed:true});
this.l = board.create('line', [this.p1, this.p2], {visible:false});
this.mp = board.create('midpoint', [this.p1, this.p2], {name:'mp', visible:false});
this.line = board.create('segment', [this.p1, this.p2], {withlabel:false, ...barStyle});
targets.push(this.line);
// label
const alpha = this.line.getAngle()+90*deg2rad;
this.lp = board.create('point', plus( mult( 0.5, plus( XY(this.p1), XY(this.p2) ) ), rect(11*pxunit, alpha)), {visible:false});
const r = Math.sqrt((this.lp.X()-this.mp.X())**2 + (this.lp.Y()-this.mp.Y())**2);
this.perpl = board.create('perpendicular', [this.mp, this.l], {visible:false});
this.lc = board.create('circle', [this.mp, r], {visible:false});
this.int1 = board.create('intersection', [this.perpl, this.lc], {name:'int1', visible:false});
this.int2 = board.create('otherintersection', [this.perpl, this.lc, this.int1], {visible:false});
let x = () => this.int2.X(), y = () => this.int2.Y();
this.label = board.create('text', [x, y, data[1]], {anchorX:'middle', anchorY:'middle'});
// implement state switching
this.obj = [this.p1, this.p2, this.line, this.label];
switch (this.state) {
case 'show': show(this); makeSwitchable(this.line, this); break;
case 'hide': hide(this); makeSwitchable(this.line, this); break;
case 'SHOW': SHOW(this); break;
case 'HIDE': HIDE(this); break;
}
// state init
this.loads = [];
}
hasPoint(pt) {
return isOn(pt,this.line) && JXG.Math.Geometry.distPointLine([1,pt.X(),pt.Y()], this.line.stdform) < tolPointLine}
data(){ let a = this.d.slice(0); a.push(this.state); return a}
name(){ return targetName(this) }
}
// Rectangle with centerline given by pair of points. Even number of points generates multiple rectangles which are merged if they overlap.
// [ "beam", "color", [x1,y1], [x2,y2] ..., radius, state ]
class beam {
constructor(data){
this.state = (typeof data[data.length-1] == 'string') ? data.pop() : "SHOW";
this.d = data.slice(0); //make a copy
this.r = data.pop(); // radius
data.shift(); // drop the type string
if (typeof data[1] === 'string') {
this.col = [data.shift(),data.shift()]; //droping the attributes for fillcolor and gradientcolor into an array
} else {
this.col = [ 'lightgrey', 'lightgrey']; data.shift(); // drop the name and use default uniform color
}
this.p = data; // end points of center line
// loop over pairs of points
this.angle = -Math.atan2(this.p[1][1]-this.p[0][1],this.p[1][0]-this.p[0][0])+90*deg2rad;
this.attr = {opacity: true, layer:defaultMecLayer, fillcolor:this.col[0],
gradient:'linear', gradientSecondColor:this.col[1], gradientAngle:this.angle, hasInnerPoints:true,
...normalStyle};
this.b = board.create('curve', [[],[]], {...normalStyle, hasInnerPoints:true});
let beams = [], beamUnions = [];
let x, y, dx, dy, l , c;
console.log('this.p.length is here: ' + this.p.length);
// variable names plays a big role, has to be different in every iteration to prevent overriding
// previous problem with only 1 beam appearing is due to overwriting of variables
// need to use IIFE
// Initialize arrays to store function values
let xArray = [], yArray = [], dxArray = [], dyArray = [], lArray = [], cArray = [];
for (let i = 0; i < this.p.length; i+=2) {
(function () {
let pointName1 = `p${i+1}`, pointName2 = `p${i+2}`, beamName1 = `b${i+1}`;
this[pointName1] = board.create('point', this.p[i], {visible:false, fixed:true, name:pointName1});
this[pointName2] = board.create('point', this.p[i+1], {visible:false, fixed:true, name:pointName2});
// Store the function values in arrays
xArray[i] = () => this[pointName1].X();
yArray[i] = () => this[pointName1].Y();
dxArray[i] = () => this[pointName2].X() - this[pointName1].X();
dyArray[i] = () => this[pointName2].Y() - this[pointName1].Y();
lArray[i] = () => Math.sqrt(dxArray[i]()**2 + dyArray[i]()**2);
cArray[i] = () => this.r/lArray[i]();
let bneu = board.create('curve', [[],[]], {strokeWidth:0, hasInnerPoints:true});
bneu.updateDataArray = function () {
this.dataX = [xArray[i]()+dyArray[i]()*cArray[i](), xArray[i]()+dxArray[i]()+dyArray[i]()*cArray[i](), xArray[i]()+dxArray[i]()-dyArray[i]()*cArray[i](),
xArray[i]()-dyArray[i]()*cArray[i](), xArray[i]()+dyArray[i]()*cArray[i]()];
this.dataY = [yArray[i]()-dxArray[i]()*cArray[i](), yArray[i]()+dyArray[i]()-dxArray[i]()*cArray[i](), yArray[i]()+dyArray[i]()+dxArray[i]()*cArray[i](),
yArray[i]()+dxArray[i]()*cArray[i](), yArray[i]()-dxArray[i]()*cArray[i]()];}
beams.push(bneu);
}).call(this);
}
if (this.p.length === 2) {
this.b = beams[0];
this.b.setAttribute(this.attr);
} else if (this.p.length === 4) {
this.b = board.create('curveunion', [beams[0], beams[1]], this.attr);
} else {
for (let i = 0; i < beams.length; i += 2) {
if (i + 1 < beams.length) {
let newBeamUnion = board.create('curveunion', [beams[i], beams[i + 1]], this.attr);
beamUnions.push(newBeamUnion);
} else {beamUnions.push(beams[i]);} // If there's an odd number of beams, add the last beam as is
}
// Combine the curve unions if there are more than one
this.b = board.create('curveunion', beamUnions, this.attr);
}
this.b.rendNode.setAttributeNS(null, 'fill-rule', 'evenodd'); //Workaround for correct fill, see https://github.com/jsxgraph/jsxgraph/issues/362
// group points
this.b.obj = [this.b];
this.b.parent = this;
//const pointgroup = board.create('group', pointsArray);
//const pointgroup = board.create('group', pointsArray).setRotationCenter('centroid').setRotationPoints(pointsArray);
// implement state switching
this.obj = [this.b];
// state init
switch (this.state) {
case 'show': show(this); makeSwitchable(this.b, this); break;
case 'hide': hide(this); makeSwitchable(this.b, this); break;
case 'SHOW': SHOW(this); break;
case 'HIDE': HIDE(this); break;
}
this.loads = [];
}
hasPoint(pt) {return isOn(pt,this.b)}
data(){ let a = this.d.slice(0); a.push(this.state); return a}
name(){ return targetName(this) }
}
// Circle with centerpoint, point on perimeter, optional: use name as radius indicator
// [ "circle", "name", [xc, yc], [xp,yp] , angle]
// [ "circle", "name", [xc, yc], radius , angle]
class circle {
constructor(data){
this.d = data.slice(0); //make a copy
this.state = data[5] ?? "SHOW";
this.angle = data[4] ? data[4] * deg2rad : 0; // pop the angle for the label
this.p1 = board.create('point', data[2], {visible:true, size:0});
// specify circle radius - check if data[3] is an array of coordinates or radius from midpoint
const theta = Math.atan2(this.p1.Y(),this.p1.X());
if (typeof(data[3]) == 'number') {this.p2 = board.create('point', [this.p1.X() + data[3] * Math.cos(theta), this.p1.Y() + data[3] * Math.sin(theta)], {visible:false})}
else {this.p2 = board.create('point', data[3], {visible:false});}
// circle
this.c = board.create('circle', [this.p1,this.p2], {opacity: true, fillcolor:'lightgray', hasInnerPoints:true, strokeWidth: normalStyle.strokeWidth, strokeColor: normalStyle.strokeColor});
this.obj = [this.c];
this.p0 = board.create('point', [0,0], {fixed:true, visible:false});
const diffX = this.p1.X() - this.p0.X();
const diffY = this.p1.Y() - this.p0.Y();
const t1 = board.create('transform', [() => this.p1.X() - diffX, () => this.p1.Y() - diffY], {type: 'translate'});
// arrow and label if name is not empty or does not contain whitespaces
if (data[1].trim() !== "") {
let dir = 1;
if (this.angle < 0) {dir = -1}
const r = this.c.Radius();
// console.log(dir);
this.p3 = board.create('point',plus(XY(this.p1), rect(r+dir*16*pxunit, this.angle)), {visible:false} );
this.p4 = board.create('point',plus(XY(this.p1), rect(r, this.angle)), {visible:false} );
this.a = board.create('arrow', [this.p3, this.p4], thinStyle);
// label
this.p = board.create('point', plus( XY(this.p1), rect(r+dir*24*pxunit, this.angle)),
{name:toTEX(data[1]), ...centeredLabelStyle});
this.obj.push( this.a, this.p.label );
t1.bindTo([this.p2, this.p3, this.p4, this.a, this.c, this.p]);
} else {
t1.bindTo([this.p2, this.c]);
}
// state init
switch (this.state) {
case 'show': show(this); makeSwitchable(this.c, this); break;
case 'hide': hide(this); makeSwitchable(this.c, this); break;
case 'SHOW': SHOW(this); break;
case 'HIDE': HIDE(this); break;
}
this.loads = []
}
hasPoint(pt) {return isOn(pt,this.c)}
data(){ return ["circle", this.d[1], XY(this.p1), this.c.Radius(), this.angle/deg2rad, this.state]}
name(){ return targetName(this) }
}
//[ "circle2P", "<label1>","<label2>", [x1,y1],[x2,y2], f ]//
class circle2p {
constructor(data){
this.d = data.slice(0); //make a copy
if (this.data[5]) {this.f = data[5]}
else {this.f = xscale};
const lStyle = {fixed:false, strokeColor:movableLineColor, highlightStrokeColor:highlightColor, highlight:true};
const iStyle = { visible: true, size: 0 , label:{visible:false} };
// x-axis for intersection points
this.xaxis = board.create('line', [ [0, 0], [1, 0] ], { visible: false });
// circle
this.A = board.create('point', mult( 1/this.f, data[3] ), {
name: data[1], ...controlSnapStyle, snapToPoints:false,label:{offset:[5,5]}});
this.AS = board.create('point', mult( 1/this.f, data[4] ), {
name: data[2], ...controlSnapStyle, snapToPoints:false,label:{offset:[5,5]} });
this.MSK1 = board.create('semicircle', [this.A, this.AS], lStyle );
this.MSK2 = board.create('semicircle', [this.AS, this.A], lStyle );
// the intersection signature has changed between 1.2.1 and 1.4.4
if (isNewerVersion ('1.2.1', JXG.version)) {
this.int1 = board.create('intersection', [this.MSK1, this.xaxis,0], iStyle );
this.int2 = board.create('intersection', [this.MSK2, this.xaxis,1], iStyle );
} else {
this.int1 = board.create('intersection', [this.MSK1, this.xaxis], iStyle );
this.int2 = board.create('intersection', [this.MSK2, this.xaxis], iStyle );
}
for (let pt of [this.A, this.AS, this.int1, this.int2]) {
pt.scale = [this.f,this.f] }
this.A.on("up", update );
this.AS.on("up", update );
}
data(){
return [this.d[0], this.d[1], this.d[2],
mult( this.f, XY(this.A) ), mult( this.f, XY(this.AS) ), this.f] }
name(){ return "[["+this.data()[3].toString() + "],[" + this.data()[4].toString() + "]]" }
}
// crosshair for reading off co-ordinates from graphs
// [ "crosshair", "", [x0, y0], [xref, yref], [xscale, yscale], [dpx, dpy] ]
class crosshair {
constructor(data) {
this.d = data;
const f = 2, r = 7;
this.p = board.create('point', data[2], {
name: '',
fixed: false,
size: r,
fillOpacity: 0,
highlightFillOpacity: 0,
strokeWidth: 1,
color: movableLineColor,
snapToGrid: false,
attractors: targets,
attractorDistance: 0.2
});
// set properties of infobox
if (data[3]) { this.p.ref = data[3] } else {this.p.ref = [0,0]}
if (data[4]) { this.p.scale = data[4] } else {this.p.scale = [1,1]}
if (data[5]) { this.p.dp = data[5] }
// cross
const that = this;
this.v = board.create('curve', [ [],[] ], { strokeWidth: 1, strokeColor: movableLineColor });
this.v.updateDataArray = function() {
this.dataX = [that.p.X() - f * r * pxunit, that.p.X() + f * r * pxunit, NaN, that.p.X(), that.p.X()];
this.dataY = [that.p.Y(), that.p.Y(), NaN, that.p.Y() - f * r * pxunit, that.p.Y() + f * r * pxunit]
};
this.v.fullUpdate();
// this doesn't work in JSXGraph version 1.2.1
//this.p1 = board.create('point', [ ()=>that.p.X(), ()=>that.p.Y() ),
// {size:2*r, face: 'plus',strokeWidth:1 , strokeColor: movableLineColor });
this.p.on("up", update);
}
data() {
let d = this.d;
d[2] = XY(this.p);
return d
}
name() {
return "[" +
((this.p.X() - this.p.ref[0]) * this.p.scale[0]).toString() + "," +
((this.p.Y() - this.p.ref[1]) * this.p.scale[1]).toString() +"]"
}
}
// damper
// [ "dashpot", "name", [x1,y1], [x2,y2], r, offset ]
class dashpot {
constructor(data){
// Parameter handling
this.state = (typeof data[data.length-1] == 'string') ? data.pop() : "SHOW";
this.d = data.slice(0); //make a copy
this.p1 = board.create('point', this.d[2], {name:'p1', fixed:true, visible:false});
this.p2 = board.create('point', this.d[3], {name:'p2', fixed:true, visible:false});
let x = () => this.p1.X(), y = () => this.p1.Y();
let dx = () => (this.p2.X()-x()), dy = () => (this.p2.Y()-y());
let l = () => Math.sqrt(dx()**2+dy()**2);
if (data.length >4 ) {this.r = data[4]} else {this.r = 6*pxunit}
if (data.length >5 ) {this.off = data[5]} else {this.off = (this.r)+10*pxunit} // check data[7]
let c = () => this.r/l();
let xc = () => x()+0.5*dx(), yc = () => y()+0.5*dy();
let dlx = () => c()*dx(), dly = () => c()*dy(), dqx = () => -c()*dy(), dqy = () => c()*dx();
this.c = board.create('curve',[[0],[0]], {hasInnerPoints:true, ...normalStyle} );
this.c.updateDataArray = function() {
this.dataX = [x(), xc(), NaN, xc()+dqx(), xc()-dqx(), NaN,
xc()+dqx()-dlx(), xc()+dqx()+dlx(), xc()-dqx()+dlx(), xc()-dqx()-dlx(), NaN,
xc()+dlx(), x()+dx()];
this.dataY = [y(), yc(), NaN, yc()+dqy(), yc()-dqy(), NaN,
yc()+dqy()-dly(), yc()+dqy()+dly(), yc()-dqy()+dly(), yc()-dqy()-dly(), NaN,
yc()+dly(), y()+dy()];
};
// snap points
this.s = board.create('segment', [this.p1,this.p2],{strokeWidth:0});
targets.push(this.s);
// label
let labelX = () => xc()-dy()/l()*this.off, labelY = () => yc()+dx()/l()*this.off;
this.l = board.create('point',[labelX,labelY], {name:toTEX(data[1]), ...centeredLabelStyle});
// logging
console.log("dasphot", data[1], data[2], data[3], this.r, this.off);
// implement state switching
this.obj = [ this.c, this.l.label ];
// state init
switch (this.state) {
case 'show': show(this); makeSwitchable(this.c, this); break;
case 'hide': hide(this); makeSwitchable(this.c, this); break;
case 'SHOW': SHOW(this); break;
case 'HIDE': HIDE(this); break;
}
this.loads = []
}
data(){ let a = this.d.slice(0); a.push(this.state); return a}
name(){ return targetName(this) }
hasPoint(pt) {return (isOn(pt,this.s) || isOn(pt,this.p1)) &&
JXG.Math.Geometry.distPointLine(
[1,pt.X(),pt.Y()], this.s.stdform) < tolPointLine}
}
// linear dimension ["dim", "name", [x1,y1], [x2,y2], d]
class dim {
constructor(data) {
this.d = data;
const d = data[4];
const vd = minus(data[3], data[2]);
const [le, a0] = polar(vd);
const vn = rect(1, a0+90*deg2rad); // ao is used here
const vmult = mult(d, vn);
this.p01 = board.create('point', [data[2][0] + vmult[0], data[2][1] + vmult[1]], {fixed:true, visible:false, name:'p01'});
this.p02 = board.create('point', [data[2][0] + vmult[0] + vd[0], data[2][1] + vmult[1] + vd[1]], {fixed:false, visible:false, name:'p02'});
this.mp1 = board.create('midpoint', [this.p01, this.p02], {name:'mp1', visible:false});
// p01, p02 are the initial points, p1 and p2 are based on these 2 points, in turn, pv1 and pv2 are based on p1 and p2
let p01x = this.p01.X(), p01y = this.p01.Y(), p02x = this.p02.X(), p02y = this.p02.Y();
// perpendicular lines
let da = 5*pxunit;
let di = da;
if (d !=0 ) {di=d}
if (d<0) {di=d;da=-da}
let negdivn = mult(-di,vn), posdavn = mult(da, vn);
this.p1 = board.create('point', [p01x + negdivn[0], p01y + negdivn[1]], {visible:false, name:'p1', fixed:false});
this.p2 = board.create('point', [p02x + negdivn[0], p02y + negdivn[1]], {visible:false, name:'p2', fixed:false});
this.l = board.create('line', [this.p1, this.p2], {visible:false});
let p1x = () => this.p1.X(), p1y = () => this.p1.Y(), p2x = () => this.p2.X(), p2y = () => this.p2.Y();
this.perpl1 = board.create('perpendicular', [this.l, this.p1], {visible:false});
this.perpl2 = board.create('perpendicular', [this.l, this.p2], {visible:false});
const vd1 = minus(XY(this.p2), XY(this.p1));
const [le1, a01] = polar(vd1);
const vn1 = rect(1, a01+90*deg2rad); // ao is used here
const vmult1 = mult( d, vn1 );
// perpendicular lines
this.p3 = board.create('point', [p01x + posdavn[0], p01y + posdavn[1]], {visible:false, name:'p3', fixed:false});
this.p4 = board.create('point', [p02x + posdavn[0], p02y + posdavn[1]], {visible:false, name:'p4', fixed:false});
this.hl1 = board.create('segment', [this.p1, this.p3], {visible:false});
this.hl2 = board.create('segment', [this.p2, this.p4], {visible:false});
this.pv1 = board.create('glider', [p01x, p01y, this.hl1], {fixed:true, visible:false, name:'pv1'});
this.pv2 = board.create('glider', [p02x, p02y, this.hl2], {fixed:true, visible:false, name:'pv2'});
let pv1x = () => this.pv1.X(), pv1y = () => this.pv1.Y(), pv2x = () => this.pv2.X(), pv2y = () => this.pv2.Y();
this.l2 = board.create('line', [this.pv1, this.pv2], {visible:false});
let dx = pv1x() - p1x(), dy = pv1y() - p1y();
let dist = Math.sqrt(dx*dx + dy*dy);
// use circles to maintain constant offset
let circle1 = board.create('circle', [this.p1, dist], {visible:false});
this.int3 = board.create('intersection', [circle1, this.perpl1], {name:'int3', visible:false});
this.int4 = board.create('otherintersection', [circle1, this.perpl1, this.int3], {name:'int4', visible:false});
let circle2 = board.create('circle', [this.p2, dist], {visible:false});
this.int5 = board.create('intersection', [circle2, this.perpl2], {name:'int5', visible:false});
this.int6 = board.create('otherintersection', [circle2, this.perpl2, this.int5], {name:'int6', visible:false});
let circle3 = board.create('circle', [this.p1, this.p3], {visible:false});
this.int7 = board.create('intersection', [circle3, this.perpl1], {name:'int7', visible:false});
this.int8 = board.create('otherintersection', [circle3, this.perpl1, this.int7], {name:'int8', visible:false});
let circle4 = board.create('circle', [this.p2, this.p4], {visible:false});
this.int9 = board.create('intersection', [circle4, this.perpl2], {name:'int9', visible:false});
this.int10 = board.create('otherintersection', [circle4, this.perpl2, this.int9], {name:'int10', visible:false});
if (d >= 0) {
this.hl3 = board.create('segment', [this.p1, this.int8], {name:'', ...thinStyle});
this.hl4 = board.create('segment', [this.p2, this.int10], {name:'', ...thinStyle});
// midpoint used in label positioning
this.mp0 = board.create('midpoint', [this.int4, this.int6], {name:'mp0', visible:false});
// baseline
this.bl = board.create('arrow', [this.int4, this.int6],{name:'', ...thinStyle, firstArrow:{type:1,size:6}, lastArrow:{type:1,size:6}});}
else {
this.hl3 = board.create('segment', [this.p1, this.int7], {name:'', ...thinStyle});
this.hl4 = board.create('segment', [this.p2, this.int9], {name:'', ...thinStyle});
// midpoint used in label positioning
this.mp0 = board.create('midpoint', [this.int3, this.int5], {name:'mp0', visible:false});
// baseline
this.bl = board.create('arrow', [this.int3, this.int5],{name:'', ...thinStyle, firstArrow:{type:1,size:6}, lastArrow:{type:1,size:6}});
}
this.gp1 = board.create('group', [this.p1, this.p3]).setTranslationPoints(this.p1);
this.gp2 = board.create('group', [this.p2, this.p4]).setTranslationPoints(this.p2);
// label
const vd2 = minus(XY(this.pv2), XY(this.pv1));
const [le2, a02] = polar(vd2);
const vn2 = rect( 1, a02+90*deg2rad );
const lmult = mult(8*pxunit, vn2);
let lcoords = plus(XY(this.mp0), lmult);
this.pl = board.create('point', lcoords, {name:'pl', visible:false});
this.perpl3 = board.create('perpendicular', [this.bl, this.mp0], {visible:false});
let circle5 = board.create('circle', [this.mp0, this.pl], {visible:false});
this.int11 = board.create('intersection', [circle5, this.perpl3], {name:'int11', visible:false});
this.int12 = board.create('otherintersection', [circle5, this.perpl3, this.int11], {name:toTEX(data[1]), ...centeredLabelStyle});
const tp03 = board.create('point', [0,0], {size:0, visible:false});
const diffX1 = this.mp0.X() - tp03.X();
const diffY1 = this.mp0.Y() - tp03.Y();
let t1 = board.create('transform', [() => (this.mp0.X()-diffX1), () => (this.mp0.Y()-diffY1)], {type:'translate'});
t1.bindTo(this.pl);
}
data() { return this.d }
name() { return '"'+this.d[1]+'"' }
}
// co-ordinate arrow with arrow with label
// ["dir", "name", [x1,y1], angle]
// ["dir", "name", [x1,y1], angle, offset]
// ["dir", "name", [x1,y1], angle, offset, length]
class dir {
constructor(data) {
this.label = data[1];
this.d = data;
let le = 24*pxunit;
this.dist = data[4] || 10;
data[5] && (le = data[5]);
this.dist >= 0 ? (this.name1 = "", this.name2 = toTEX(data[1])) : (this.name2 = "", this.name1 = toTEX(data[1]));
// Arrow
const off = data[3];
const v = rect( le, data[3]*deg2rad );
const pAttr = {fixed:true, size: 0, showInfobox:false, label:{offset:[0,this.dist], autoPosition:true}};
this.p1 = board.create('point', data[2], {name: this.name1, ...pAttr});
this.p2 = board.create('point', plus(data[2], v), {name: this.name2, ...pAttr});
this.vec = board.create('arrow', [this.p1, this.p2], {lastArrow: { type: 1, size: 6 }, ...thinStyle });
}
data() { return this.d }
name() { return '"'+this.d[1]+'"' }
}
//co-ordinate arrow with red arrow with label
// [ "disp", "name", [x,y], angle, offset, length]
class disp {
constructor(data) {
this.label = data[1];
this.d = data;
let le = 24*pxunit;
this.dist = data[4] || 10;
data[5] && (le = data[5]);
this.dist >= 0 ? (this.name1 = "", this.name2 = toTEX(data[1])) : (this.name2 = "", this.name1 = toTEX(data[1]));
// Arrow
const off = data[4];
const v = rect( le, data[3]*deg2rad );
this.p1 = board.create('point', data[2], {name: this.name1, fixed:true, size:0, showInfobox:false, label:{offset:[0,this.dist], autoPosition:true, color:"red"}});
this.p2 = board.create('point', plus(data[2], v), {name: this.name2, fixed:true, size:0, showInfobox:false, label:{offset:[0,this.dist], autoPosition:true, color:"red"}});
this.vec = board.create('arrow', [this.p1, this.p2], {lastArrow: { type: 1, size: 6 }, ...thinStyle, strokeColor:"red" });
}
data() { return this.d }
name() { return '"'+this.d[1]+'"' }
}
// Loslager
class fix1 {
constructor(data) {
this.state = (typeof data[data.length-1] == 'string') ? data.pop() : "SHOW";
this.d = data.slice(0);
// base points
const coords = [
[0, 0],
[-a / 2, -0.8*a],
[+a / 2, -0.8*a],
[ - 0.8 * a, -0.8*a],
[ + 0.8 * a, -0.8*a],
[ - 0.8 * a, - 1*a],
[ + 0.8 * a, - 1*a],
[ 0, - 1.9*a] //label
];
let p = [], c;
for (c of coords) { p.push(board.create('point', c, {visible: false})); }
const t1 = board.create('transform', [data[3]*deg2rad], {type:'rotate'});
const t2 = board.create('transform', data[2], {type:'translate'});
t1.applyOnce(p);
t2.applyOnce(p);
// dependent objects
// pivot
const pointConfigs = {fixed: true, visible: false};
const points = p.map((coord, index) => board.create('point', XY(coord), index === 0 ?
{name: '', ...nodeStyle} : pointConfigs));
[this.p1, this.p2, this.p3, this.p4, this.p5, this.p6, this.p7, this.p8] = points;
// label
this.label = board.create('point', XY(this.p8), {name:toTEX(data[1]), ...centeredLabelStyle });
// body
this.t = board.create('polygon', [this.p1, this.p2, this.p3], {name: '',fillColor: "white", Opacity: true, layer: 7,
borders: {...normalStyle, layer:8}, vertices: {fixed:true, size:0}});
// baseline with hatch
this.bl = board.create('segment', [this.p6,this.p7], {name: '', ...normalStyle});
this.c = board.create("comb", [this.p7,this.p6], hatchStyle() );
// Enable object animation
this.p0 = board.create('point', [0,0], {fixed:true, visible:false});
const diffX = this.p1.X() - this.p0.X(), diffY = this.p1.Y() - this.p0.Y();
const t3 = board.create('transform', [() => this.p1.X() - diffX, () => this.p1.Y() - diffY], {type:'translate'});
t3.bindTo([this.p2, this.p3, this.p4, this.p5, this.p6, this.p7, this.p8, this.label, this.t, this.bl]);
// implement state switching
this.obj = [ this.p1, this.t, this.bl, this.c, this.label, this.label.label ];
this.obj = this.obj.concat(this.t.borders);
// state init
switch (this.state) {
case 'show': show(this); makeSwitchable(this.c, this); makeSwitchable(this.t, this); break;
case 'hide': hide(this); makeSwitchable(this.c, this); makeSwitchable(this.t, this); break;
case 'SHOW': SHOW(this); break;
case 'HIDE': HIDE(this); break;
}
// proximity
this.loads = []
}
data(){ let a = this.d.slice(0); a.push(this.state); return a}
name(){ return targetName(this) }
hasPoint(pt) {return isOn(pt,this.p1)}
}
// Festlager
// [ "fix12", "name", [x, y], angle, state ]
class fix12 {
constructor(data) {
this.state = (typeof data[data.length-1] == 'string') ? data.pop() : "SHOW";
this.d = data.slice(0);
// base points
const coords = [
[0, 0],
[-a / 2, -a],
[+a / 2, -a],
[ - 0.8 * a, - a],
[ + 0.8 * a, - a],
[ 0, - 1.9*a] // label
];
let p = [], c;
for (c of coords) {p.push(board.create('point', c, {visible: false}));}
const t1 = board.create('transform', [data[3]*deg2rad], {type:'rotate'});
const t2 = board.create('transform', data[2], {type:'translate'});
t1.applyOnce(p);
t2.applyOnce(p);
// dependent objects
// pivot
const pointConfigs = {fixed: true, visible: false};
const points = p.map((coord, index) => board.create('point', XY(coord), index === 0 ?
{name: '', ...nodeStyle} : pointConfigs));
[this.p1, this.p2, this.p3, this.p4, this.p5, this.p6] = points;
this.label = board.create('point', XY(this.p6), {name:toTEX(data[1]), ...centeredLabelStyle});
// body
this.t = board.create('polygon', [this.p1, this.p2, this.p3], {name:'',fillColor:"white", Opacity:true, layer:7,
borders:{...normalStyle, layer:8}, vertices: {fixed:true, size:0}});
// baseline with hatch
this.bl = board.create('segment', [this.p4, this.p5], {name: '',...normalStyle});
this.c = board.create("comb", [this.p5, this.p4], hatchStyle() )
// Enable object animation
this.p0 = board.create('point', [0,0], {fixed:true, visible:false});
const diffX = this.p1.X() - this.p0.X(), diffY = this.p1.Y() - this.p0.Y();
const t3 = board.create('transform', [() => this.p1.X() - diffX, () => this.p1.Y() - diffY], {type:'translate'});
t3.bindTo([this.p2, this.p3, this.p4, this.p5, this.p6, this.label, this.t, this.bl]);
// implement state switching
this.obj = [this.p1, this.t, this.bl, this.c, this.label, this.label.label];
this.obj = this.obj.concat(this.t.borders);
// state init
switch (this.state) {
case 'show': show(this); makeSwitchable(this.c, this); makeSwitchable(this.t, this); break;
case 'hide': hide(this); makeSwitchable(this.c, this); makeSwitchable(this.t, this); break;
case 'SHOW': SHOW(this); break;
case 'HIDE': HIDE(this); break;
}
// proximity
this.loads = []
}
data(){ let a = this.d.slice(0); a.push(this.state); return a}
name(){ return targetName(this) }
hasPoint(pt) {return isOn(pt,this.p1)}
}
// Einspannung
// [ "fix123", "name", [x, y], angle, state ] angle = 0, object faces right
class fix123 {
constructor(data) {
this.state = (typeof data[data.length-1] == 'string') ? data.pop() : "SHOW";
this.d = data.slice(0);
// base points
const coords = [
[0,0], // base point
[0, -0.8*a], // p
[0, +0.8*a], // p
[-0.9*a,0] // label
];
let p = [], c;
for (c of coords) { p.push(board.create('point', c, {visible:false}));}
const t1 = board.create('transform', [data[3]*deg2rad], {type:'rotate'});
const t2 = board.create('transform', data[2], {type:'translate'});
t1.applyOnce(p);
t2.applyOnce(p);
// dependent objects
// base point
const pointConfigs = {fixed:true, visible:false};
const points = p.map((coord, index) => board.create('point', XY(coord), index === 0 ?
{name: '', ...silentPStyle} : pointConfigs));
[this.p1, this.p2, this.p3, this.p4] = points;
// label
this.label = board.create('point', XY(this.p4), {name:toTEX(data[1]), ...centeredLabelStyle});
// baseline with hatch
this.bl = board.create('segment', [this.p2,this.p3], {name: '',...normalStyle});
this.c = board.create("comb", [this.p3, this.p2], { ...hatchStyle(), angle:-45*deg2rad})
// Enable object animation
this.p0 = board.create('point', [0,0], {fixed:true, visible:false});
const diffX = this.p1.X() - this.p0.X(), diffY = this.p1.Y() - this.p0.Y();
const t3 = board.create('transform', [() => this.p1.X() - diffX, () => this.p1.Y() - diffY], {type:'translate'});
t3.bindTo([this.p2, this.p3, this.p4, this.label, this.bl]);
// implement state switching
this.obj = [ this.p1, this.bl, this.c, this.label, this.label.label];
// state init
switch (this.state) {
case 'show': show(this); makeSwitchable(this.c, this); break;
case 'hide': hide(this); makeSwitchable(this.c, this); break;
case 'SHOW': SHOW(this); break;
case 'HIDE': HIDE(this); break;
}
// proximity
this.loads = []
}
data(){ let a = this.d.slice(0); a.push(this.state); return a}
name(){ return targetName(this) }
hasPoint(pt) {return isOn(pt,this.p1)}
}
// Slider
// [ "fix13", "name", [x, y], angle, state ]
class fix13 {
constructor(data) {
this.state = (typeof data[data.length-1] == 'string') ? data.pop() : "SHOW";
this.d = data.slice(0);
// base points
const coords = [
[0,0], // base point
[0, -0.5*a], // p
[0, +0.5*a], // p
[-0.2*a, -0.8*a], // p
[-0.2*a, +0.8*a], // p
[-1.1*a,0] // label
];
let p = [], c;
for (c of coords) { p.push(board.create('point', c, {visible:false}));}
const t1 = board.create('transform', [data[3]*deg2rad], {type:'rotate'});
const t2 = board.create('transform', data[2], {type:'translate'});
t1.applyOnce(p);
t2.applyOnce(p);
// dependent objects
// base point
const pointConfigs = {fixed: true, visible: false};
const points = p.map((coord, index) => board.create('point', XY(coord), index === 0 ?
{name: '', ...silentPStyle} : pointConfigs));
[this.p1, this.p2, this.p3, this.p4, this.p5, this.p6] = points;
// label
this.label = board.create('point', XY(this.p6), {name:toTEX(data[1]), ...centeredLabelStyle});
this.l = board.create('segment', [this.p2,this.p3], {name: '', ...normalStyle});
this.bl = board.create('segment', [this.p4,this.p5], {name: '', ...normalStyle});
this.c = board.create("comb", [this.p5,this.p4], {...hatchStyle(), angle:-45*deg2rad});
// Enable object animation
this.p0 = board.create('point', [0,0], {fixed:true, visible:false});
const diffX = this.p1.X() - this.p0.X(), diffY = this.p1.Y() - this.p0.Y();
const t3 = board.create('transform', [() => this.p1.X() - diffX, () => this.p1.Y() - diffY], {type:'translate'});
t3.bindTo([this.p2, this.p3, this.p4, this.p5, this.p6, this.label, this.bl]);
// switchable objects
this.obj = [ this.p1, this.l, this.bl, this.c, this.label, this.label.label];
// state init
switch (this.state) {
case 'show': show(this); makeSwitchable(this.c, this); makeSwitchable(this.l, this); break;
case 'hide': hide(this); makeSwitchable(this.c, this); makeSwitchable(this.l, this); break;
case 'SHOW': SHOW(this); break;
case 'HIDE': HIDE(this); break;
}
// proximity
this.loads = []
}
data(){ let a = this.d.slice(0); a.push(this.state); return a}
name(){ return targetName(this) }
hasPoint(pt) {return isOn(pt,this.p1)}
}
// [ "force", "name", [x1, y1], [x2,y2], d , state ]
class force {
constructor(data) {
// parameter handling
this.d = data;
this.fname = data[1];
if (data[4]) { this.off = data[4] } else { this.off = 10 }
if (this.off >= 0) {this.name1 = ""; this.name2 = toTEX(data[1]) } else
{this.name2 = ""; this.name1 = toTEX(data[1]) }
if (data[5]) { this.state = data[5] } else { this.state = "locked" }
// snap and appearance depending on state
const labelopts = {offset:[this.off,0], autoPosition:true, color:loadColor};
let pstyle = {snapToGrid:false, size:0, fixed:true, snapToPoints:false, label:labelopts};
let hl = false;
if (this.state == "active") {
pstyle = {snapToGrid:true, fixed:false, size:2, snapToPoints:true,
attractors:targets, attractorDistance: 0.1, snatchdistance:0.2, label:labelopts};
hl = true; }
// start and end point
this.p1 = board.create('point', data[2], { name: this.name1,
...pstyle });
this.p2 = board.create('point', data[3], { name: this.name2,
...pstyle });
// configure infobox
this.p1.dp = [dpx+1,dpy+1];
this.p2.start = this.p1;
this.p2.dp = [dpx+1,dpy+1];
this.p2.ref = function() { return XY(this.start) };
this.p2.infoboxlabel = "Vektor ";
if (this.state == "silent") {this.p2.setAttribute({showInfobox:false})}
// dash
let d = 0; if (this.state == "dotted") d=2
this.vec = board.create('arrow', [this.p1, this.p2], {
touchLastPoint: true, lastArrow:{size:5, type:2}, highlight:hl, snaptogrid:true,
highlightStrokeColor:highlightColor, strokeColor:loadColor, dash:d});
// interactive control
if (this.state == "active") {this.vec.setAttribute({fixed:false, snapToGrid:true})}
this.vec.obj = [this.vec, this.p1, this.p2];
this.vec.parent = this;
// postproceswsing
this.vec.lastclick = Date.now();
let that = this
// remove object on doubleclick
this.vec.on('up', function() {
if (Date.now()-this.lastclick < 500 && that.state == "active") {
console.log("delete force")
that.state = "deleted"; cleanUp();
board.removeObject(this.obj, true); update()
} else {this.lastclick = Date.now(); update()}
})
this.vec.on('drag', function() { board.update(); that.p1.moveTo(XY(that.p1)); that.p2.moveTo(XY(that.p2))
// that.p1.moveTo(XY(that.p1)); that.p2.moveTo(XY(that.p2)) }
})
// avoid zero length of vector
this.p2.on('up', function() {
console.log("length check")
if (that.vec.L() === 0) {
console.log('Force length should not be zero.');
that.p2.moveTo(plus(XY(that.p2),[0.5,0]),0) }
else {board.update(); update()} })
// switch off highlighting if locked
this.obj = [this.vec, this.p1, this.p2, this.p2.label];
if (this.state == "locked") { lock(this) }
// update conditions
this.p1.on("up", function () {board.update(); update()})
// points for position check
this.proximityPoints = [this.p1, this.p2];
}
data() { return [this.d[0], this.fname, XY(this.p1), XY(this.p2), this.off, this.state ] }
name() { return toSTACK(this.fname) }
}
// [ "forceGen", "name", [x,y]]
class forceGen {
constructor(data) {
// input field
this.d = data;
const dy = -20*pxunit, dx = 40*pxunit;
// HTML trick because input.set() doesn't work in the callback
const fid = divid+"_fname"; // unique ID for html object even if multiple widgets on a page
let t = board.create('text', [ data[2][0], data[2][1],
'<input type="text" id='+fid+' value="'+data[1]+'" size="1">'], {fixed: true});
// ref point for checking drag distance
const ref1 = board.create('point', plus(data[2], [0,dy]), {visible:false});
const ref2 = board.create('point', plus(data[2], [dx,dy]), {visible:false});
// arrow
const p1 = board.create('point', plus(data[2], [0,dy]), {
name: '', fixed:false, visible: false });
const p2 = board.create('point', plus(data[2], [dx,dy]), {
name: toTEX(document.getElementById(fid).value), fixed:false, visible:false, label:{offset:[5,0], visible:true, color:'gray'} });
p2.addParents(t);
let vec = board.create('arrow', [p1, p2],
{ fixed:false, color:'gray',lastArrow:{size:5, type:2}, highlight:true,
highlightStrokeColor:highlightColor} );
// callback creates new force object and new name
vec.parent = this;
t.on('out', function(e) {
document.getElementById(fid).value = cleanupName(document.getElementById(fid).value)
p2.setAttribute({name:toTEX(document.getElementById(fid).value) })});
vec.on('up', function(e) {
//only generate force if distance is sufficient to not create overlapping objects
if (ref1.Dist(this.point1)+ref2.Dist(this.point2) >dx) {
objects.push(new force(["force", document.getElementById(fid).value,
XY(p1), XY(p2), 10, "active"] ));
// generate new unique force name
let f = [];
for (let m of objects) {
if (m.data()[0] == 'force') { f = f.concat(m.data()[1]) } }
let i = 1, n = '', found = true;
while (found ) { n = 'F_'+i.toString(); found = f.includes(n);i ++;}
document.getElementById(fid).value = n;
vec.parent.d[1] = n;
update();
}
// whatever happened, move the arrow back
p1.setPositionDirectly(JXG.COORDS_BY_USER, XY(ref1), XY(p1) );
p2.setPositionDirectly(JXG.COORDS_BY_USER, XY(ref2), XY(p2) );
p2.setAttribute({name:toTEX(document.getElementById(fid).value) }) });
}
data(){ return this.d }
name(){ return "0" }
}
// [ "frame", "", [ Array of ccordinates ], tension]
class frame {
constructor(data) {
this.d = data;
if(data[3]){
this.t = data[3];
} else{
this.t = 3;
}
this.fr = board.create('metapostspline', [data[2], {
tension: this.t, // <--- Je höher desto kantiger
isClosed: true
}], {
strokeColor: 'grey',
strokeWidth: 2,
dash: 2,
points: {visible: false}
});
}
data() { return this.d }
name(){ return "0" }
}
// grid control object: [ "grid", "xlabel", "ylabel", xmin, xmax, ymin, ymax, pix ]
// grid control object: [ "grid", "xlabel", "ylabel", xmin, xmax, ymin, ymax, pix, [fx, fy] ]
// newer version of class grid to fix grid lines issue
class grid {
constructor(data) {
this.d = data;
const xmin = data[3];
const xmax = data[4];
const ymin = data [5];
const ymax = data [6];
const pix = data [7];
var fx = 1, fy = 1;
if (data[8]) {fx = data[8][0]; fy = data[8][1]; xscale = fx; yscale = fy };
if (data[9]) {dpx = data[9][0]; dpy = data[9][1] };
var width = pix*Math.abs(xmax-xmin)
var height = pix*Math.abs(ymax-ymin)
// logics of container sizing and grid scaling has changed between 1.2.1 and 1.3.2 and in 2023
try {