-
Notifications
You must be signed in to change notification settings - Fork 6
/
Mechanics.pde
1006 lines (876 loc) · 29.8 KB
/
Mechanics.pde
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
// import java.awt.Toolkit;
interface Channel {
PVector getPosition(float r);
void draw();
void snugTo(Gear moveable, Gear fixed); // position moveable gear on this channel so it is snug to fixed gear, not needed for all channels
}
interface Selectable {
void select();
void unselect();
void nudge(int direction, int keycode);
}
static final float kMPDefaultRadius = inchesToPoints * 12/72.0;
static final float kMPSlideRadius = inchesToPoints * 20/72.0;
static final float kGearMountRadius = inchesToPoints * 12/72.0;
// static final float kGearNotchWidth = inchesToPoints * 16.5/72.0;
static final float kGearNotchWidth = 5 * mmToInches * inchesToPoints;
static final float kGearNotchHeightMaj = 5 * mmToInches * inchesToPoints;
static final float kGearNotchHeightMin = 3.5 * mmToInches * inchesToPoints;
static final float kGearLabelStart = 0.5*inchesToPoints;
static final float kGearLabelIncr = 0.5*inchesToPoints;
static final float kCRLabelIncr = 0.5*inchesToPoints;
static final float kCRNotchIncr = 0.25*inchesToPoints;
static final float kCRNotchStart = 0.75*inchesToPoints;
static final float kCRLabelStart = 1*inchesToPoints;
static final float kPenLabelStart = 4.75*inchesToPoints;
static final float kPenLabelIncr = -0.5*inchesToPoints;
static final float kPenNotchIncr = -0.25*inchesToPoints;
static final float kPaperRad = 4.625*inchesToPoints;
class MountPoint implements Channel, Selectable {
Channel itsChannel = null;
float itsMountLength;
int setupIdx;
float x, y, radius=kMPDefaultRadius;
String typeStr = "MP";
boolean isFixed = false;
boolean selected = false;
MountPoint(String typeStr, float x, float y) {
this.typeStr = typeStr;
this.itsChannel = null;
this.itsMountLength = 0;
this.isFixed = true;
this.setupIdx = -1; // fixed
this.x = x*inchesToPoints;
this.y = y*inchesToPoints;
// println(this.typeStr + " is at " + this.x/inchesToPoints + "," + this.y/inchesToPoints);
}
MountPoint(String typeStr, Channel ch, float mr, int setupIdx) {
this.typeStr = typeStr;
this.itsChannel = ch;
this.itsMountLength = mr;
this.setupIdx = setupIdx;
PVector pt = ch.getPosition(mr);
this.x = pt.x;
this.y = pt.y;
// println(this.typeStr + " is at " + this.x/inchesToPoints + "," + this.y/inchesToPoints);
}
int chooseBestDirection(int direction, int keycode, float incr)
{
PVector pNeg = itsChannel.getPosition(itsMountLength-incr);
PVector pPos = itsChannel.getPosition(itsMountLength+incr);
switch (keycode) {
case RIGHT:
return (pPos.x >= pNeg.x)? 1 : -1;
case LEFT:
return (pPos.x <= pNeg.x)? 1 : -1;
case UP:
return (pPos.y <= pNeg.y)? 1 : -1;
case DOWN:
return (pPos.y >= pNeg.y)? 1 : -1;
default:
return direction;
}
}
void nudge(int direction, int keycode) {
float amt, mn=0, mx=1;
if (itsChannel instanceof ConnectingRod) {
amt = 0.125;
mn = 0.5;
mx = 29;
} else if (itsChannel instanceof Gear) {
amt = 0.125;
mn = 0.75;
mx = ((Gear) itsChannel).radius/(kGearLabelIncr) - 1;
} else {
amt = 0.01;
}
direction = chooseBestDirection(direction, keycode, amt);
amt *= direction;
itsMountLength += amt;
itsMountLength = constrain(itsMountLength, mn, mx);
println("Mount: " + itsMountLength);
if (setupIdx >= 0) {
setupMounts[setupMode][setupIdx] = itsMountLength;
}
}
float getDistance(float v1, float v2) {
PVector src = itsChannel.getPosition(v1);
PVector dst = itsChannel.getPosition(v2);
return dist(src.x, src.y, dst.x, dst.y);
}
void unselect() {
selected = false;
}
void select() {
selected = true;
}
boolean isClicked(int mx, int my) {
PVector p = this.getPosition();
return dist(mx, my, p.x, p.y) <= this.radius;
}
PVector getPosition() {
return getPosition(0.0);
}
PVector getPosition(float r) {
if (itsChannel != null) {
return itsChannel.getPosition(itsMountLength);
} else {
return new PVector(x,y);
}
}
void snugTo(Gear moveable, Gear fixed) { // not meaningful
}
void draw() {
PVector p = getPosition();
if (itsChannel instanceof ConnectingRod) {
itsChannel.draw();
}
if (selected) {
fill(180,192);
stroke(50);
} else {
fill(180,192);
stroke(100);
}
strokeWeight(selected? 4 : 2);
ellipse(p.x, p.y, this.radius, this.radius);
}
}
class ConnectingRod implements Channel, Selectable {
MountPoint itsSlide = null;
MountPoint itsAnchor = null;
float armAngle = 0;
int rodNbr = 0;
boolean selected=false;
boolean isInverted = false;
ConnectingRod(MountPoint itsSlide, MountPoint itsAnchor, int rodNbr)
{
this.rodNbr = rodNbr;
this.itsSlide = itsSlide;
itsSlide.radius = kMPSlideRadius;
this.itsAnchor = itsAnchor;
if (setupInversions[setupMode][rodNbr])
invert();
}
PVector getPosition(float r) {
PVector ap = itsAnchor.getPosition();
PVector sp = itsSlide.getPosition();
armAngle = atan2(sp.y - ap.y, sp.x - ap.x);
float d = notchToDist(r);
return new PVector(ap.x + cos(armAngle)*d, ap.y + sin(armAngle)*d);
}
void snugTo(Gear moveable, Gear fixed) {
// not relevant for connecting rods
}
void unselect() {
selected = false;
}
void select() {
selected = true;
}
void invert() {
this.isInverted = !this.isInverted;
setupInversions[setupMode][rodNbr] = this.isInverted;
MountPoint tmp = itsAnchor;
itsAnchor = itsSlide;
itsSlide = tmp;
itsAnchor.radius = kMPDefaultRadius;
itsSlide.radius = kMPSlideRadius;
if (penRig != null && penRig.itsRod == this) {
penRig.angle += 180;
if (penRig.angle > 360)
penRig.angle -= 360;
setupPens[setupMode][1] = penRig.angle;
}
println("Inverted rod " + rodNbr);
}
void nudge(int direction, int kc) {
if (kc == UP || kc == DOWN) {
this.invert();
}
else {
if (penRig.itsRod == this) {
penRig.itsMP.nudge(direction, kc);
}
}
}
boolean isClicked(int mx, int my)
{
PVector ap = itsAnchor.getPosition();
PVector sp = itsSlide.getPosition();
// mx,my, ap, ep522 293 546.1399 168.98767 492.651 451.97696
int gr = 5;
return (mx > min(ap.x-gr,sp.x-gr) && mx < max(ap.x+gr,sp.x+gr) &&
my > min(ap.y-gr,sp.y-gr) && my < max(ap.y+gr,sp.y+gr) &&
abs(atan2(my-sp.y,mx-sp.x) - atan2(ap.y-sp.y,ap.x-sp.x)) < radians(10));
}
float notchToDist(float n) {
return kCRLabelStart+(n-1)*kCRLabelIncr;
}
float distToNotch(float d) {
return 1 + (d - kCRLabelStart)/kCRLabelIncr;
}
void draw() {
PVector ap = itsAnchor.getPosition();
PVector sp = itsSlide.getPosition();
itsSlide.draw();
itsAnchor.draw();
noFill();
int shade = selected? 100 : 200;
int alfa = selected? 192 : 192;
stroke(shade, alfa);
strokeWeight(.33*inchesToPoints);
armAngle = atan2(sp.y - ap.y, sp.x - ap.x);
float L = 18 * inchesToPoints;
line(ap.x,ap.y, ap.x+cos(armAngle)*L, ap.y+sin(armAngle)*L);
stroke(100,100,100,128);
fill(100,100,100);
strokeWeight(0.5);
// float notchOffset = 0.75*inchesToPoints;
textFont(nFont);
textAlign(CENTER);
pushMatrix();
translate(sp.x,sp.y);
rotate(atan2(ap.y-sp.y,ap.x-sp.x));
float ln = dist(ap.x,ap.y,sp.x,sp.y);
for (int i = 0; i < 29*2; ++i) {
float x = ln-(kCRNotchStart + kCRNotchIncr*i);
line(x, 6, x, -(6+(i % 2 == 1? 2 : 0)));
if (i % 2 == 1) {
text(""+(1+i/2),x,8);
}
}
popMatrix();
}
}
class PenRig implements Selectable {
float len;
float angle; // expressed in degrees
boolean selected = false;
ConnectingRod itsRod;
MountPoint itsMP;
int lastDirection = -1; // these are used to avoid rotational wierdness with manipulations
long lastRotation = -1;
int lastKey = -1;
PenRig(float len, float angle, MountPoint itsMP) {
this.len = len; // in pen notch units
this.angle = angle;
this.itsRod = (ConnectingRod) itsMP.itsChannel;
this.itsMP = itsMP;
PVector ap = itsMP.getPosition();
PVector ep = this.getPosition();
}
float notchToDist(float n) {
return kPenLabelStart+(n-1)*kPenLabelIncr;
}
float distToNotch(float d) {
return 1 + (d - kPenLabelStart)/kPenLabelIncr;
}
PVector getPosition() {
PVector ap = itsMP.getPosition();
float d = notchToDist(this.len);
float rangle = radians(this.angle);
return new PVector(ap.x + cos(itsRod.armAngle + rangle)*d, ap.y + sin(itsRod.armAngle + rangle)*d);
}
PVector getPosition(float len, float angle) {
PVector ap = itsMP.getPosition();
float d = notchToDist(len);
float rangle = radians(angle);
return new PVector(ap.x + cos(itsRod.armAngle + rangle)*d, ap.y + sin(itsRod.armAngle + rangle)*d);
}
boolean isClicked(int mx, int my)
{
PVector ap = itsMP.getPosition();
PVector ep = this.getPosition();
float a = atan2(ap.y-ep.y,ap.x-ep.x);
float d = 6*inchesToPoints;
ap.x = ep.x + cos(a)*d;
ap.y = ep.y + sin(a)*d;
int gr = 5;
return (mx > min(ap.x-gr,ep.x-gr) && mx < max(ap.x+gr,ep.x+gr) &&
my > min(ap.y-gr,ep.y-gr) && my < max(ap.y+gr,ep.y+gr) &&
abs(atan2(my-ep.y,mx-ep.x) - atan2(ap.y-ep.y,ap.x-ep.x)) < radians(10));
}
void unselect() {
selected = false;
}
void select() {
selected = true;
}
int chooseBestDirection(int direction, int keycode, float lenIncr, float angIncr)
{
if (abs(angIncr) > abs(lenIncr) && lastRotation != -1 && (millis()-lastRotation) < 10000) {
return lastDirection * (lastKey == keycode? 1 : -1);
}
PVector pNeg = getPosition(len -lenIncr, angle-angIncr);
PVector pPos = getPosition(len +lenIncr, angle+angIncr);
switch (keycode) {
case RIGHT:
return (pPos.x >= pNeg.x)? 1 : -1;
case LEFT:
return (pPos.x <= pNeg.x)? 1 : -1;
case UP:
return (pPos.y <= pNeg.y)? 1 : -1;
case DOWN:
return (pPos.y >= pNeg.y)? 1 : -1;
default:
return direction;
}
}
void nudge(int direction, int kc) {
float angIncr = 0, lenIncr = 0;
if (kc == RIGHT || kc == LEFT) {
angIncr = 5;
} else {
lenIncr = 0.125;
}
direction = chooseBestDirection(direction, kc, lenIncr, angIncr);
if (abs(angIncr) > abs(lenIncr)) {
lastRotation = millis();
}
lastDirection = direction;
lastKey = kc;
this.angle += angIncr*direction;
if (this.angle > 180) {
this.angle -= 360;
} else if (this.angle <= -180) {
this.angle += 360;
}
setupPens[setupMode][1] = this.angle;
this.len += lenIncr*direction;
this.len = constrain(this.len, 1, 8);
setupPens[setupMode][0] = this.len;
println("Pen: " + this.len + " " + this.angle + "°");
}
void draw() {
itsMP.draw();
PVector ap = itsMP.getPosition();
PVector ep = this.getPosition();
float a = atan2(ap.y-ep.y,ap.x-ep.x);
float d = 6*inchesToPoints;
ap.x = ep.x + cos(a)*d;
ap.y = ep.y + sin(a)*d;
noFill();
if (selected)
stroke(penColor,128);
else
stroke(penColor,64);
strokeWeight(.33*inchesToPoints);
line(ap.x, ap.y, ep.x, ep.y);
float nibRad = inchesToPoints * 0.111;
strokeWeight(0.5);
pushMatrix();
translate(ep.x,ep.y);
rotate(atan2(ap.y-ep.y,ap.x-ep.x));
fill(255);
ellipse(0,0,nibRad,nibRad);
noFill();
stroke(192);
line(-nibRad,0,nibRad,0);
line(0,nibRad,0,-nibRad);
textFont(nFont);
textAlign(CENTER);
fill(penColor);
noStroke();
ellipse(0,0,penWidth/2, penWidth/2);
stroke(255);
fill(255);
for (int i = 0; i < 15; ++i) {
float x = notchToDist(1+i/2.0);
line(x, 6, x, -(6+(i % 2 == 0? 2 : 0)));
if (i % 2 == 0) {
text(""+(1+i/2),x,8);
}
}
popMatrix();
}
}
class LineRail implements Channel {
float x1,y1, x2,y2;
LineRail(float x1, float y1, float x2, float y2) {
this.x1 = x1*inchesToPoints;
this.y1 = y1*inchesToPoints;
this.x2 = x2*inchesToPoints;
this.y2 = y2*inchesToPoints;
}
PVector getPosition(float r) {
return new PVector(x1+(x2-x1)*r, y1+(y2-y1)*r);
}
void draw() {
noFill();
stroke(110);
strokeWeight(.23*inchesToPoints);
line(x1,y1, x2,y2);
}
void snugTo(Gear moveable, Gear fixed) {
float dx1 = x1-fixed.x;
float dy1 = y1-fixed.y;
float dx2 = x2-fixed.x;
float dy2 = y2-fixed.y;
float a1 = atan2(dy1,dx1);
float a2 = atan2(dy2,dx2);
float d1 = dist(x1,y1,fixed.x,fixed.y);
float d2 = dist(x2,y2,fixed.x,fixed.y);
float adiff = abs(a1-a2);
float r = moveable.radius+fixed.radius+meshGap;
float mountRatio;
if (adiff > TWO_PI)
adiff -= TWO_PI;
if (adiff < .01) { // if rail is perpendicular to fixed circle
mountRatio = (r-d1)/(d2-d1);
// find position on line (if any) which corresponds to two radii
} else if ( abs(x2-x1) < .01 ) {
float m = 0;
float c = (-m * y1 + x1);
float aprim = (1 + m*m);
float bprim = 2 * m * (c - fixed.x) - 2 * fixed.y;
float cprim = fixed.y * fixed.y + (c - fixed.x) * (c - fixed.x) - r * r;
float delta = bprim * bprim - 4*aprim*cprim;
if (delta == 0) {
println("zero delta");
}
float my1 = (-bprim + sqrt(delta)) / (2 * aprim);
float mx1 = m * my1 + c;
float my2 = (-bprim - sqrt(delta)) / (2 * aprim); // use this if it's better
float mx2 = m * my2 + c;
// println("V x1,y1 " + x1/inchesToPoints + " " + y1/inchesToPoints + " x2,y2 " + x2/inchesToPoints + " " + y2/inchesToPoints + " fixed " + fixed.x/inchesToPoints + " " + fixed.y/inchesToPoints);
// println(" aprim,bprim,cprim = " + aprim + " " + bprim + " " + cprim);
// of the two spots which are best, and pick the one that is A) On the line and B) closest to the moveable gear's current position
// println(" a m=" + m + " c=" + c + " aprim=" + aprim + " bprim=" + bprim + " cprim=" + cprim + " delta=" + delta);
// println(" a mx1,mx2 " + mx1/inchesToPoints + " " + my1/inchesToPoints + " mx2,my2 " + mx2/inchesToPoints + " " + my2/inchesToPoints);
if (my1 < min(y1,y2) || my1 > max(y1,y2) ||
dist(moveable.x,moveable.y,mx2,my2) < dist(moveable.x,moveable.y,mx1,mx2)) {
// println(" swap");
mx1 = mx2;
my1 = my2;
}
if (delta < 0) {
mountRatio = -1;
} else {
mountRatio = dist(x1,y1,mx1,my1)/dist(x1,y1,x2,y2);
}
} else { // we likely have a gear on one of the lines on the left
// given the line formed by x1,y1 x2,y2, find the two spots which are desiredRadius from fixed center.
float m = (y2-y1)/(x2-x1);
float c = (-m * x1 + y1);
float aprim = (1 + m*m);
float bprim = 2 * m * (c - fixed.y) - 2 * fixed.x;
float cprim = fixed.x * fixed.x + (c - fixed.y) * (c - fixed.y) - r * r;
float delta = bprim * bprim - 4*aprim*cprim;
if (delta == 0) {
println("zero delta");
}
float mx1 = (-bprim + sqrt(delta)) / (2 * aprim);
float my1 = m * mx1 + c;
float mx2 = (-bprim - sqrt(delta)) / (2 * aprim); // use this if it's better
float my2 = m * mx2 + c;
// println("x1,y1 " + x1/inchesToPoints + " " + y1/inchesToPoints + " x2,y2 " + x2/inchesToPoints + " " + y2/inchesToPoints);
// println(" aprim,bprim,cprim = " + aprim + " " + bprim + " " + cprim);
// of the two spots which are best, and pick the one that is A) On the line and B) closest to the moveable gear's current position
// println(" b m=" + m + " c=" + c + " aprim=" + aprim + " bprim=" + bprim + " cprim=" + cprim + " delta=" + delta);
// println(" b mx1,mx2 " + mx1/inchesToPoints + " " + my1/inchesToPoints + " mx2,my2 " + mx2/inchesToPoints + " " + my2/inchesToPoints);
if (mx1 < min(x1,x2) || mx1 > max(x1,x2) || my1 < min(y1,y2) || my1 > max(y1,y2) ||
dist(moveable.x,moveable.y,mx2,my2) < dist(moveable.x,moveable.y,mx1,mx2)) {
// println(" swap");
mx1 = mx2;
my1 = my2;
}
if (delta < 0) {
mountRatio = -1;
} else {
mountRatio = dist(x1,y1,mx1,my1)/dist(x1,y1,x2,y2);
}
}
if (mountRatio < 0 || mountRatio > 1 || Float.isNaN(mountRatio)) {
loadError = 1;
mountRatio = 0;
// println("LOAD ERR");
}
mountRatio = constrain(mountRatio,0,1);
moveable.mount(this,mountRatio);
}
}
class ArcRail implements Channel {
float cx,cy, rad, begAngle, endAngle;
ArcRail(float cx, float cy, float rad, float begAngle, float endAngle) {
this.cx = cx*inchesToPoints;
this.cy = cy*inchesToPoints;
this.rad = rad*inchesToPoints;
this.begAngle = begAngle;
this.endAngle = endAngle;
}
PVector getPosition(float r) {
float a = begAngle + (endAngle - begAngle)*r;
return new PVector(cx+cos(a)*rad, cy+sin(a)*rad);
}
// fixed = 15.017775 6.61 1.5221801
// moveable = 16.518276 2.237212 3.0443602
// rail = 8.91 3.9100003 7.79
// angles = -24.999998 15.0
// desired r = 4.625595
// d = 6.6779423
// i1 12.791063 2.2343254 ang -23.352594
// i2 16.517643 2.2343254 ang -12.421739
void snugTo(Gear moveable, Gear fixed) { // get the movable gear mounted on this rail snug to the fixed gear
// The fixed gear is surrounded by an imaginary circle which is the correct distance (r) away.
// We need to intersect this with our arcRail circle, and find the intersection point which lies on the arcrail.
// Mesh point
// https://gsamaras.wordpress.com/code/determine-where-two-circles-intersect-c/
// println("Snug arcrail");
// println(" fixed = " + fixed.x/72 + " " + fixed.y/72 + " " + fixed.radius/72);
// println(" moveable = " + moveable.x/72 + " " + moveable.y/72 + " " + moveable.radius/72);
// println(" rail = " + cx/72 + " " + cy/72 + " " + rad/72);
// println(" angles = " + degrees(begAngle) + " " + degrees(endAngle));
float x1 = fixed.x;
float y1 = fixed.y;
float r1 = moveable.radius+fixed.radius+meshGap;
float x2 = this.cx;
float y2 = this.cy;
float r2 = this.rad;
float d = dist(x1,y1,x2,y2);
// println(" desired r = " + r1/72);
// println(" d = " + d/72);
if (d > r1+r2) {
println(" circles are too far apart");
loadError = 1;
return;
} else if (abs(d) < .01 && abs(r1-r2) < .01) {
println(" circles coincide");
loadError = 1;
return;
} else if (d + min(r1,r2) < max(r1,r2)) {
println(" one circle contains the other");
loadError = 1;
return;
}
float a = (r1*r1 - r2*r2 + d*d) / (2*d);
float h = sqrt(r1*r1 - a*a);
PVector p2 = new PVector( x1 + (a * (x2 - x1)) / d,
y1 + (a * (y2 - y1)) / d);
// these are our two intersection points (which may or may not fall on the arc)
PVector i1 = new PVector( p2.x + (h * (y2 - y1))/ d,
p2.y + (h * (x2 - x1))/ d);
PVector i2 = new PVector( p2.x - (h * (y2 - y1))/ d,
p2.y + (h * (x2 - x1))/ d);
// println(" i1 " + i1.x/72 + " " + i1.y/72 + " ang " + degrees(atan2(i1.y-cy,i1.x-cx)));
// println(" i2 " + i2.x/72 + " " + i2.y/72 + " ang " + degrees(atan2(i2.y-cy,i2.x-cx)));
PVector best = i2;
float ma = atan2(best.y-cy,best.x-cx);
if (ma < begAngle || ma > endAngle) {
best = i1;
ma = atan2(best.y-cy,best.x-cx);
if (ma < begAngle || ma > endAngle) {
println("Intersection points don't fall on arc rail");
loadError = 1;
return;
}
}
float mountRatio = (ma-begAngle)/(endAngle-begAngle);
if (mountRatio < 0 || mountRatio > 1)
loadError = 1;
moveable.mount(this, mountRatio);
}
void draw() {
noFill();
stroke(110);
strokeWeight(.23*inchesToPoints);
arc(cx, cy, rad, rad, begAngle, endAngle);
}
}
int[] rgTeeth = { // regular gears
30, 32, 34, 36, 40, 48, 50, 58, 60, 66, 72, 74, 80, 90, 94, 98, 100, 108,
};
int [] ttTeeth = { // turntable gears
120, 144, 150
};
class GearSetup {
float notchStart;
float notchEnd;
int nbrLabels;
int teeth;
GearSetup(int teeth, float notchStart, float notchEnd, int nbrLabels)
{
this.teeth = teeth;
this.notchStart = notchStart * inchesToPoints;
this.notchEnd = notchEnd * inchesToPoints;
this.nbrLabels = nbrLabels;
}
}
HashMap<Integer, GearSetup> gearSetups;
void gearInit()
{
gearSetups = new HashMap<Integer, GearSetup>();
gearSetups.put(108, new GearSetup(108, 0.4375, 3.0, 6));
gearSetups.put(100, new GearSetup(100, 0.40625, 2.8125, 5));
gearSetups.put( 98, new GearSetup( 98, 0.4375, 2.8125, 5));
gearSetups.put( 94, new GearSetup( 94, 0.375, 2.625, 5));
gearSetups.put( 90, new GearSetup( 90, 0.40625, 2.5, 5));
gearSetups.put( 80, new GearSetup( 80, 0.40625, 2.25, 4));
gearSetups.put( 74, new GearSetup( 74, 0.40625, 2.031, 4));
gearSetups.put( 72, new GearSetup( 72, 0.375, 2.0, 4));
gearSetups.put( 66, new GearSetup( 66, 0.375, 1.6875, 3));
gearSetups.put( 60, new GearSetup( 60, 0.3125, 1.625, 3));
gearSetups.put( 58, new GearSetup( 58, 0.3125, 1.5625, 3));
gearSetups.put( 50, new GearSetup( 50, 0.25, 1.3125, 2)); // notch joins axel
gearSetups.put( 48, new GearSetup( 48, 0.375, 1.25, 2));
gearSetups.put( 40, new GearSetup( 40, 0.25, 1.0, 2)); // notch joins axel
gearSetups.put( 36, new GearSetup( 36, 0.3125, 0.968, 1));
gearSetups.put( 34, new GearSetup( 34, 0.3125, 0.84375, 1));
gearSetups.put( 32, new GearSetup( 32, 0.3125, 0.8125, 1));
gearSetups.put( 30, new GearSetup( 30, 0.3125, 0.75, 1));
}
class Gear implements Channel, Selectable {
int teeth;
int setupIdx;
float radius;
float rotation;
float phase = 0;
float x,y;
float mountRatio = 0;
boolean doFill = true;
boolean showMount = true;
boolean isMoving = false; // gear's position is moving
boolean isFixed = false; // gear does not rotate or move
boolean selected = false;
boolean contributesToCycle = true;
ArrayList<Gear> meshGears;
ArrayList<Gear> stackGears;
Channel itsChannel;
String nom;
GearSetup itsSetup;
Gear(int teeth, int setupIdx, String nom) {
this.itsSetup = gearSetups.get(teeth);
this.teeth = teeth;
this.nom = nom;
this.setupIdx = setupIdx;
this.radius = (this.teeth*toothRadius/PI);
this.x = 0;
this.y = 0;
this.phase = 0;
meshGears = new ArrayList<Gear>();
stackGears = new ArrayList<Gear>();
}
boolean isClicked(int mx, int my) {
return dist(mx, my, this.x, this.y) <= this.radius;
}
void unselect() {
selected = false;
}
void select() {
selected = true;
}
void nudge(int direction, int keycode) {
int gearIdx = this.setupIdx;
int teeth, oldTeeth;
oldTeeth = this.teeth;
if (isShifting) {
teeth = setupTeeth[setupMode][gearIdx] + direction;
} else {
teeth = findNextTeeth(setupTeeth[setupMode][gearIdx], direction);
}
if (teeth < 24) {
teeth = 151;
} else if (teeth > 151) {
teeth = 30;
}
setupTeeth[setupMode][gearIdx] = teeth;
drawingSetup(setupMode, false);
if (loadError != 0) { // disallow invalid meshes
java.awt.Toolkit.getDefaultToolkit().beep();
setupTeeth[setupMode][gearIdx] = oldTeeth;
drawingSetup(setupMode, false);
}
selectedObject = activeGears.get(gearIdx);
selectedObject.select();
}
int findNextTeeth(int teeth, int direction) {
// println("Finding next tooth: " + teeth + " dir " + direction);
int[] gTeeth = (this == turnTable? ttTeeth : rgTeeth);
if (direction == 1) {
for (int i = 0; i < gTeeth.length; ++i) {
if (gTeeth[i] > teeth)
return gTeeth[i];
}
return gTeeth[0];
} else {
for (int i = gTeeth.length-1; i >= 0; --i) {
if (gTeeth[i] < teeth)
return gTeeth[i];
}
return gTeeth[gTeeth.length-1];
}
}
PVector getPosition(float r) {
float d = notchToDist(r); // kGearLabelStart+(r-1)*kGearLabelIncr;
return new PVector(x+cos(this.rotation+this.phase)*d, y+sin(this.rotation+this.phase)*d);
}
void meshTo(Gear parent) {
parent.meshGears.add(this);
// work out phase for gear meshing so teeth render interlaced
float meshAngle = atan2(y-parent.y, x-parent.x); // angle where gears are going to touch (on parent gear)
if (meshAngle < 0)
meshAngle += TWO_PI;
float iMeshAngle = meshAngle + PI;
if (iMeshAngle >= TWO_PI)
iMeshAngle -= TWO_PI;
float parentMeshTooth = (meshAngle - parent.phase)*parent.teeth/TWO_PI; // tooth on parent, taking parent's phase into account
// We want to insure that difference mod 1 is exactly .5 to insure a good mesh
parentMeshTooth -= floor(parentMeshTooth);
phase = (meshAngle+PI)+(parentMeshTooth+.5)*TWO_PI/teeth;
}
// Find position in our current channel which is snug to the fixed gear
void snugTo(Gear anchor) {
itsChannel.snugTo(this, anchor);
}
float notchToDist(float n) {
return kGearLabelStart+(n-1)*kGearLabelIncr;
}
float distToNotch(float d) {
return 1 + (d - kGearLabelStart)/kGearLabelIncr;
}
// Using this gear as the channel, find position for moveable gear which is snug to the fixed gear (assuming fixed gear is centered)
void snugTo(Gear moveable, Gear fixed) {
float d1 = 0;
float d2 = radius;
float d = moveable.radius+fixed.radius+meshGap;
float mountRadDist = this.radius*d/d2;
if (mountRadDist < 0 || mountRadDist > this.radius)
loadError = 1;
float mountNotch = distToNotch(mountRadDist);
moveable.mount(this, mountNotch);
// find position on line (if any) which corresponds to two radii
}
void stackTo(Gear parent) {
parent.stackGears.add(this);
this.x = parent.x;
this.y = parent.y;
this.phase = parent.phase;
}
void mount(Channel ch) {
mount(ch, 0.0);
}
void recalcPosition() { // used for orbiting gears
PVector pt = this.itsChannel.getPosition(this.mountRatio);
this.x = pt.x;
this.y = pt.y;
}
void mount(Channel ch, float r) {
this.itsChannel = ch;
this.mountRatio = r;
PVector pt = ch.getPosition(r);
this.x = pt.x;
this.y = pt.y;
// println("Gear " + teeth + " is at " + this.x/inchesToPoints + "," + this.y/inchesToPoints);
}
void crank(float pos) {
if (!this.isFixed) {
this.rotation = pos;
float rTeeth = this.rotation*this.teeth;
for (Gear mGear : meshGears) {
mGear.crank(-(rTeeth)/mGear.teeth);
}
for (Gear sGear : stackGears) {
sGear.crank(this.rotation);
}
if (isMoving)
recalcPosition(); // technically only needed for orbiting gears
}
else {
// this gear is fixed, but meshgears will rotate to the passed in pos
for (Gear mGear : meshGears) {
mGear.crank(pos + ( pos*this.teeth )/mGear.teeth);
}
}
}
void draw() {
strokeWeight(1);
strokeCap(ROUND);
strokeJoin(ROUND);
noFill();
stroke(0);
pushMatrix();
translate(this.x, this.y);
rotate(this.rotation+this.phase);
float r1 = radius-.07*inchesToPoints;
float r2 = radius+.07*inchesToPoints;
float tAngle = TWO_PI/teeth;
float tipAngle = tAngle*.1;
if (doFill) {
fill(220);
} else {
noFill();
}
if (selected) {
strokeWeight(4);
stroke(64);
} else {
strokeWeight(0.5);
stroke(128);
}
beginShape();
for (int i = 0; i < teeth; ++i) {
float a1 = i*tAngle;
float a2 = (i+.5)*tAngle;
vertex(r2*cos(a1), r2*sin(a1));
vertex(r2*cos(a1+tipAngle), r2*sin(a1+tipAngle));
vertex(r1*cos(a2-tipAngle), r1*sin(a2-tipAngle));
vertex(r1*cos(a2+tipAngle), r1*sin(a2+tipAngle));
vertex(r2*cos(a1+tAngle-tipAngle), r2*sin(a1+tAngle-tipAngle));
vertex(r2*cos(a1+tAngle), r2*sin(a1+tAngle));
}
endShape();
if (this == turnTable) {
noStroke();
fill(255,192);
beginShape();
for (int i = 0; i < 8; ++i) {
vertex(kPaperRad*cos(i*TWO_PI/8), kPaperRad*sin(i*TWO_PI/8));
}
endShape();
}
strokeWeight(1);
pushMatrix();
translate(0, radius-20);
fill(127);
textFont(gFont);
textAlign(CENTER);
text(""+teeth, 0, 0);
noFill();
popMatrix();
if (showMount) {
noStroke();
fill(192,128);
ellipse(0, 0, kGearMountRadius, kGearMountRadius);
pushMatrix();
float notchStart, notchEnd;
int nbrLabels;
if (itsSetup != null) {
notchStart = itsSetup.notchStart;
notchEnd = itsSetup.notchEnd;
nbrLabels = itsSetup.nbrLabels;
} else {
// Make a guesstimate
notchStart = max(radius*.1,16*seventyTwoScale);
notchEnd = radius-max(radius*.1,8*seventyTwoScale);
nbrLabels = 1 + int((notchEnd-notchStart-0.2*inchesToPoints)/(0.5*inchesToPoints));
}
textFont(nFont);
textAlign(CENTER);
stroke(128);
fill(128);
int nbrNotches = (nbrLabels)*2-1;
for (int i = 0; i < nbrNotches; ++i) {
float x = kGearLabelStart + i * 0.25 * inchesToPoints;
line(x,-(i % 2 == 0? kGearNotchHeightMaj : kGearNotchHeightMin), x, (i % 2 == 0? kGearNotchHeightMaj : kGearNotchHeightMin));
if (i % 2 == 0) {
text((i/2)+1,x,kGearNotchHeightMaj+0.2*inchesToPoints);
}
}
fill(192);
noStroke();
rect(notchStart, -kGearNotchWidth/2, notchEnd-notchStart, kGearNotchWidth);
popMatrix();