-
Notifications
You must be signed in to change notification settings - Fork 0
/
proportion.js
executable file
·1410 lines (1189 loc) · 36.2 KB
/
proportion.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
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
///////////////////////////////////////
// This is the more-or-less well-commented version of the Proportion Library.
// (c)2013 Neal McDonald, released under MIT License
// Use it however you like; have a nice day.
// There are two classes defined below: base and page.
// prBase objects can be points, lines, or circles, which are used to build figures,
// or segments and arcs, which mark parts of lines or circles for drawing,
// or lofts, which allow you fill areas of the drawing with color.
// The points, lines, and circles can interact. You can use points to define lines and circles,
// and intersect lines and circles to define points.
// prPage provides the interface for the whole system, and contains all the prBase.
// coords: the page takes normalized coords; 0,0 is top-left, and 1,1 is bottom-right.
// canvas aspect ratio might impact construction success; caveat aedicicator.
// Normal usage, if you're animating, is to clear the canvas every time you redraw,
// and rebuild from scratch. Doing otherwise is generally not a time-saver, computationally,
// and if the canvas size changes, ...
////////////////////////////////////
// constant definitions / enum ts
// allowed values for "t"
// allowed values for prBase.t
const PR_N = 0; // null
const PR_P = 1; // point
const PR_L = 2; // line
const PR_C = 3; // circle
const PR_S = 5; // segment -- part of a line
const PR_A = 6; // arc -- part of a circle
const PR_F = 7; // loft
const PR_E = 0.000000025; // the number that is basically zero
const PR_2P = 6.28318530272959; // Math.PI * 2
////////////////////////////////////////////////////////////
// prBase: prPages (below) have an array of points, lines and circles
// I could make one object that represents all three, or have a base parent class...
function prBase() {
this.t = PR_N; // type: null, pt, line, circle, parameter, segment, arc, or loft
this.x = 0.0; // position of points, p1 of lines, centers of circles
this.y = 0.0;
this.x2 = 0.0; // unused for points. lines and circles are set by two points, A and B.
this.y2 = 0.0; // x2,y2 = A-B
this.r = 0.0; // length of segments and lines, radius of arcs and circles
this.g = 0; // render group
this.p0 = 0.0; // arc scan start parameter
this.dp = 0.01; // arc scanning parameter
}
// t, r, and g are always used the same way
// PR_N: nothing else is used
// PR_P: (x,y) are coords
// PR_L: (x,y) is one end, (x+x2,y+y2) is the other. r is distance between them
// PR_C: (x,y) is the center, (x+x2,y+y2) is on the circle
// PR_S: a mark! copies a line, xy, x2y2 give the endpoints, and the parameter is 0-1
// PR_A: a mark! as circles, but "dp" gives the parameter going around the arc
// scan with t going 0-1, points of arc given by x,y+(rcos(p0+t*dp),rsin(p1+t*dp))
// for lofting, vary t by 1/r?
// PR_F: x=one mark's index into a page, y=other mark.
prBase.prototype.cp = function (it) { // "copy" function
this.t = it.t;
this.x = it.x;
this.y = it.y;
this.x2 = it.x2;
this.y2 = it.y2;
this.r = it.r;
this.g = it.g;
this.p0 = it.p0;
this.dp = it.dp;
}
prBase.prototype.cl = function () { // "clear" function
this.t = PR_N;
this.x = 0.0;
this.y = 0.0;
this.x2 = 0.0;
this.y2 = 0.0;
this.r = 0.0;
this.g = 0;
this.p0 = 0.0;
this.dp = 0.01;
}
prBase.prototype.ec = function(label) { // "echo"
var displayStr = label + "t:" + this.t + " x:" + this.x + " y:";
displayStr += this.y + " x2:" + this.x2 + " y2:" + this.y2 + " r:" + this.r;
displayStr += " g:" + this.g + " p0:" + this.p0+ " dp:" + this.dp;
console.log(displayStr);
}
///////////// Mark-rendering helper functions
prBase.prototype.sX = function(u) { // segment X coord for parameter u
return this.x + (this.x2*u);
}
prBase.prototype.sY = function(u) { // segment y coord
return this.y + (this.y2*u);
}
// note that 2pi is scaled in; u is expected 0<u<1
prBase.prototype.aX = function(u) { // arc X coord for parameter u
return this.x + (this.r*Math.cos(PR_2P *(this.p0 + (this.dp * u))));
}
prBase.prototype.aY = function(u) { // arc Y
return this.y + (this.r*Math.sin(PR_2P *(this.p0 + (this.dp * u))));
}
//"trace": draw to screen in default manner
prBase.prototype.tr = function(context) {
var n, p, plim, pct, p1x, p1y;
switch (this.t) {
case PR_P:
context.moveTo(this.x+3.0 , this.y);
context.arc(this.x, this.y, 3.0,0.0, PR_2P);
break;
case PR_L:
context.moveTo(this.x-(200.0*this.x2), this.y-(200.0*this.y2));
context.lineTo(this.x+(200.0*this.x2), this.y+(200.0*this.y2));
break;
case PR_C:
context.moveTo(this.x+this.r , this.y);
context.arc(this.x, this.y, this.r, 0.0, PR_2P);
break;
case PR_S:
context.moveTo(this.x, this.y);
context.lineTo(this.x+this.x2, this.y+this.y2);
break;
case PR_A: // the JS "arc" command does not suit me for lofting
p1x = this.aX(0.0);
p1y = this.aY(0.0);
context.moveTo(p1x, p1y);
if (this.dp>0.0) { plim = 1.0; } // FIXME; no need to swap, do better pct
else { plim = -1.0; }
pct = (plim / 50.0);
p = 0.0;
for (n=0.0; (n*n)<=1.0; n=n+pct) {
p1x = this.aX(n);
p1y = this.aY(n)
context.lineTo(p1x, p1y);
}
break;
// PR_F is not drawn here; it needs the page's list of prBase
}
}
/////////////////// utilities-- private
// returns 1 if "this" is a point
prBase.prototype.isaP = function () {
var res = 0;
if (this.t == PR_P) {
res = 1;
}
return res;
}
// returns 1 if points it and this are very close
// note: doesn't check type.
prBase.prototype.eqP = function (it) {
var dx, dy, len;
var res = 0;
dx = this.x-it.x;
dy = this.y-it.y;
len = dx*dx+dy*dy;
if (len<PR_E) {
res=1;
}
return res;
}
// returns 0 unless p1 and p2 are in the same place
prBase.prototype.eqs = function(x1,y1,x2,y2) {
var res = 0;
if (Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)))<PR_E) { res = 1; }
return res;
}
// return angle in radians that p2-p1 makes with the x-axis
// can't work if points are the same!
// returns 0<=ang<=2pi, or -1 for error
prBase.prototype.ang = function(x1,y1,x2,y2) {
var dx = x2-x1;
var dy = y2-y1;
var len = Math.sqrt(dx*dx+dy*dy);
if (len<PR_E) { return -1.0; } // error: coincident points.
var res = Math.acos(dx/len);
if (dy<0.0) { res = PR_2P - res; }
return res;
}
// take a maximum facet length, maxd.
// return a dt for the parameters that deliver that
prBase.prototype.scanLength = function(maxd) {
var dp;
var res = (maxd / this.r);
if (this.t == PR_A) {
res /= PR_2P;
}
res *= (this.t2 - this.t1);
return res;
}
////////// closest-points!
// this as opposed to calling them "point on"-- numerical
// self is line or circle; returns parameter of point on object closest to
// "closest point on line"
// return the parameter of the point on the object closest to p
prBase.prototype.cpoL = function (pt) {
var dx, dy;
var res = 0.0;
if (this.r>PR_E) {
dx = pt.x - this.x;
dy = pt.y - this.y;
res = ((dx*this.x2) + (dy*this.y2))/(this.r*this.r); // a parameter, but an incorrect one
} // else self is not really a line; problematic! and t should==0
return res;
}
// "closest point on circle"
prBase.prototype.cpoC = function (pt) {
var ang = this.ang(this.x, this.y, pt.x, pt.y);
var res = -1.0;
if (ang>-0.5) {
res = (ang/PR_2P) - this.p0;
if (res<0.0) { res +=1.0; }
}
return res;
}
// "closest parametric point"
prBase.prototype.cPP = function (pt) {
var res = 0.0;
if (this.t!=PR_P) {
switch (this.t) {
// case PR_P: res = 0.0; break;
case PR_L:
res = this.cpoL(pt);
break;
case PR_C:
res = this.cpoC(pt);
break;
}
}
return res;
}
///////////////////////////////////////////////////
// "Set As" functions
// there are many functions that "set (this) as X"
// set as Point
prBase.prototype.saP = function (xin, yin) { // set as point
this.t = PR_P;
this.x = xin;
this.y = yin;
// the other members are set by the constructor
}
// set as point from another point -- maybe never used?
prBase.prototype.saPP = function ( pt, tin) {
this.t = PR_P;
this.x = pt.x;
this.y = pt.y;
}
// given line, line parameters, set this as a point on that line
prBase.prototype.saPL = function ( ln, tin) {
this.t = PR_P;
this.x = ln.sX(tin);
this.y = ln.sY(tin);
}
// set as point on circle
prBase.prototype.saPC = function ( ck, tin) {
this.t = PR_P;
this.x = ck.aX(tin); // implies that both circles and arcs need p0 dp's set
this.y = ck.aY(tin);
}
// set as parametric --
prBase.prototype.saPm = function ( ob, tin) {
switch (ob.t) {
case PR_P:
this.saPP(ob, tin);
break;
case PR_L:
this.saPL(ob, tin);
break;
case PR_C:
this.saPC(ob, tin);
break;
case PR_S:
this.saPL(ob, tin);
break;
case PR_A:
this.saPC(ob, tin);
break;
}
}
// taking a prBase of type PR_T-- never used; PR_T's are gone?
prBase.prototype.saPmO = function(ob, tob) {
this.saPm(ob, tob.t1);
}
// set as line
prBase.prototype.saL = function (xin, yin, x2in, y2in) {
this.t = PR_L;
this.x = xin;
this.y = yin;
this.x2 = x2in-this.x;
this.y2 = y2in-this.y;
this.r = Math.sqrt(this.x2*this.x2+this.y2*this.y2);
if (this.r<PR_E) { // if the two points are the same, it's really a point
this.t = PR_P;
}
}
// set as circle
prBase.prototype.saC = function (xin, yin, x2in, y2in) {
this.t = PR_C;
this.x = xin;
this.y = yin;
this.x2 = x2in-this.x;
this.y2 = y2in-this.y;
this.r = Math.sqrt(this.x2*this.x2+this.y2*this.y2);
if (this.r<PR_E) { // points same? return a point.
this.t = PR_P;
} else {
this.p0 = this.ang(xin, yin, x2in, y2in);
this.p0 /= PR_2P;
this.dp = 1.0;
}
}
// set as parametric segment, define endpoints using parameters
prBase.prototype.sapS = function (lineIn, t1in, t2in) {
var nx1, ny1, nx2, ny2;
if (((t1in-t2in)*(t1in-t2in))>PR_E) {
this.cp(lineIn); // copy the input line
nx1 = this.sX(t1in); // but the parameters give the new endpoints
ny1 = this.sY(t1in);
nx2 = this.sX(t2in);
ny2 = this.sY(t2in);
this.t = PR_S; // then tweak.
this.x = nx1;
this.y = ny1;
this.x2 = nx2-nx1;
this.y2 = ny2-ny1;
} else {
this.t = PR_N; // if the points coincide, no mark.
}
}
// set as segment using points; project the points onto the line,
// then define the segment using them.
prBase.prototype.saS = function (lineIn, p1, p2) {
var t1 = lineIn.cpoL(p1); // t1,t2= parameters of p1,p2
var t2 = lineIn.cpoL(p2);
this.sapS(lineIn, t1, t2);
}
// set as parametric arc, using 3 parametric points on a circle
// t1 and t3 give arc ends, but that gives 2 arcs!
// we want the arc that sweeps through t2/p2
prBase.prototype.sapA = function (circleIn, t1in, t2in, t3in) {
this.t=PR_A;
// normalize t1t2t3 to be 0<t<1
var t1 = t1in - Math.floor(t1in);
if (t1<0.0) { t1 += 1.0; }
var t2 = t2in - Math.floor(t2in);
if (t2<0.0) { t2 += 1.0; }
var t3 = t3in - Math.floor(t3in);
if (t3<0.0) { t3 += 1.0; }
// fail if there are not 2 different points
var s1 = t3-t1;
var s2 = t3-t2;
if ((s1*s1<PR_E)&&(s2*s2<PR_E)) { this.t = PR_N; }
if (this.t!=PR_N) {
this.cp(circleIn); // start out as a a circle
this.t=PR_A;
// preserve radius & set 0-angle to be through t1
this.x2 = this.r * Math.cos(t1 * PR_2P);
this.y2 = this.r * Math.sin(t1 * PR_2P);
if (((t1<t2)&&(t2<t3)) || ((t1>t2)&&(t2>t3))) { // no 0 crossing
this.p0 = t1; this.dp = t3-t1;
} else {
if (t1<t3) { // cross 0 going down
this.p0 = t1; this.dp = (t3-1.0) - t1;
} else { // cross 0 going up
this.p0 = t1-1.0; this.dp = t3 - (t1-1.0);
// could add 1 to dp, but that makes a second test for aX() aY()
}
}
/* recast t2 and t3 st t1==0
t2 = t2 - t1;
if (t2<0.0) { t2 += 1.0; }
t3 = t3 - t1;
if (t3<0.0) { t3 += 1.0; }
if (this.t==PR_A) {
this.p0 = t1;
if (t2<t3) {
this.dp = t3;
} else {
this.dp = t3-1.0;
}
}*/
}
}
// set as arc, using points to define the arc. the norm; how Euclid would do it!
prBase.prototype.saA = function (circleIn, p1,p2,p3) {
var t1 = this.ang(circleIn.x, circleIn.y, p1.x, p1.y);
var t2 = this.ang(circleIn.x, circleIn.y, p2.x, p2.y);
var t3 = this.ang(circleIn.x, circleIn.y, p3.x, p3.y);
// failure cause: any of p1,p2,p3, being coincident with circle center
this.t = PR_A;
if (t1<0.0) { this.t = PR_N; }
if (t2<0.0) { this.t = PR_N; }
if (t3<0.0) { this.t = PR_N; }
if (this.t!=PR_N) {
this.sapA(circleIn, t1/PR_2P, t2/PR_2P, t3/PR_2P); // note 2pi factored out
}
}
// set as loft:
// inputs are the indices into the prPage of the marks that bound
// the loft. So, most of the code for lofting lives in prPage.
// NOTE GIVE INDICES, NOT OBJECTS
prBase.prototype.saF = function (mark1, mark2) {
this.t = PR_F;
this.x = mark1;
this.y = mark2;
}
///////////////////////////////////////////////////
// helpers for intersection
// there are 0, 1, or 2 intersections of points, lines, and circles.
// points don't intersect things; use "closestPointOn" and "pointAlong"
// (I thought about adding paraboloids... many many more cases).
// "closer" takes three points, and picks one, which is the basis for designating
// a point as "first" or "second"
// the intersect routines take two lines or circles and returns the number of
// intersections, returning both points in a prBase.
///// "closer"
// returns 0 if only self is a point, or not even
// returns 1 if only self and 1 are points
// returns 2 if only self and 2 are points
// returns 1 if all are points, and 1 is closer to self than 2
// returns 2 if 2 is closer
// returns 1 is they are equidistant and 1 is on top
// returns 2 is they are equidistant and 2 is on top
// returns 1 is they are equidistant and level and 1 is leftmost
// returns 2 is they are equidistant and level and 2 is leftmost
// otherwise, return 1
prBase.prototype.closer = function ( ob1, ob2, ob3) {
var a, b, c, dx, dy, ac, bc;
var res = 1;
a = ob1.isaP();
b = ob2.isaP();
c = ob3.isaP();
if ((c==0)||((a==0)&&(b==0))) {
res = 0;
}
else {
if ((a==1)&&(b==0)) {
res = 1;
}
else if ((a==0)&&(b==1)) {
res = 2;
}
else {
// a, b, and c are points
dx = ob1.x - ob3.x;
dy = ob1.y - ob3.y;
ac = Math.sqrt(dx*dx+dy*dy);
dx = ob2.x - ob3.x;
dy = ob2.y - ob3.y;
bc = Math.sqrt(dx*dx+dy*dy);
dx = (ac-bc)*(ac-bc);
if (dx>PR_E) { // the distances are different
if (ac<bc) {
res =1;
}
else {
res = 2;
}
}
else { // the distances are the same
dy = ob1.y - ob2.y;
if (dy*dy>PR_E) {
if (ob1.y < ob2.y) {
res = 1;
}
else {
res = 2;
}
}
else { // the heights are the same
dx = ob1.x - ob2.x;
if (dx*dx>PR_E) {
if (ob1.x < ob2.x) {
res = 1;
}
else {
res = 2;
}
}
else {
// ob2=ob3
res = 1;
}
}
}
}
}
return res;
}
/// iLL, iLC, and iCC intersect this and an input prBase with each other.
// they return an integer (the number of intersections), and a special,
// abused prBase that has the first intersection in x,y and the second in x2, y2
// intersect self, as a line, with the input line; set pt if there is one
// the algorithm is from Mathematica notebook, included as linelineA.jpg
prBase.prototype.iLL = function ( line, pt) {
var xa, xb, xc, xd, ya, yb, yc, yd, res;
var det = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
res = 0;
xa = this.x;
xb = this.x + this.x2;
ya = this.y;
yb = this.y + this.y2;
xc = line.x;
xd = line.x + line.x2;
yc = line.y;
yd = line.y + line.y2;
det[0] = xa*yb - xb*ya;
det[1] = xc*yd - xd*yc;
det[2] = (xa-xb);
det[3] = xa - xb;
det[4] = xc - xd;
det[5] = ya-yb;
det[6] = yc-yd;
det[7] = (det[3]*det[6]) - (det[4]*det[5]);
if ((det[7]*det[7])>PR_E) {
det[8] = (det[0]*det[4]) - (det[1]*det[3]);
det[9] = (det[0]*det[6]) - (det[1]*det[5]);
pt.t = PR_P;
pt.x = det[8] / det[7];
pt.y = det[9] / det[7];
res = 1;
}
else { // || or colinear
res = 0;
}
return res;
}
// intersect self, as circle, with line
// from Mathematica Notebook, lineCircle.jpg
prBase.prototype.iLC = function ( line, pt) {
var dx, dy, dr2, r2, lx1, lx2, ly1, ly2, D, disc, sgndy, absdy;
var res = 0;
lx1 = line.x - this.x;
ly1 = line.y - this.y;
lx2 = (line.x+line.x2) - this.x;
ly2 = (line.y+line.y2) - this.y;
res = 1;
dx = lx2-lx1;
dy = ly2-ly1;
dr2 = dx*dx+dy*dy;
r2 = this.r * this.r;
D = (lx1*ly2)-(lx2*ly1);
disc = (r2 * dr2) - (D*D);
sgndy = 1;
if (dy<0) {
sgndy = -1;
}
absdy = dy;
if (absdy<0.0) {
absdy *= -1.0;
}
if (disc<0.0) { // no intersection
res = 0;
}
else {
if (disc*disc<PR_E) { // line is tangent
pt.x = (D * dy) / dr2;
pt.y = (-1.0 * D * dx) / dr2;
res = 1;
}
else { // disc is well-greater than 0; two intersections
pt.x = ((D * dy) + (sgndy * dx * Math.sqrt(disc))) / dr2;
pt.y = ((-1.0 * D * dx) + (absdy * Math.sqrt(disc))) / dr2;
pt.x2 = ((D * dy) - (sgndy * dx * Math.sqrt(disc))) / dr2;
pt.y2 = ((-1.0 * D * dx) - (absdy * Math.sqrt(disc))) / dr2;
pt.x += this.x;
pt.y += this.y;
pt.x2 += this.x;
pt.y2 += this.y;
res = 2;
}
}
return res;
}
// intersect self, as circle, with circle
// circcirc.jpg, Mathematica Notebook
prBase.prototype.iCC = function (cr, pt) {
var dx, dy, rsum, sep, crx, cry, a, b, c;
var res = 0;
rsum = this.r + cr.r;
dx = this.x - cr.x; // dxy= connection c1 to c2
dy = this.y - cr.y;
sep = Math.sqrt(dx*dx+dy*dy);
if (sep>PR_E) { // the centers are not the same
if (rsum<sep) {
// can't touch; return null
res = 0;
}
else {
if ( ((rsum-sep)*(rsum-sep)) < PR_E) {
// touch at one spot, which is self.r away from self.center
// along dxy
pt.x = this.x + (dx*this.r/sep);
pt.y = this.y + (dy*this.r/sep);
res = 1;
}
else {
// there are two points of contact.
res = 2;
// two triangles, r1sq=csq+asq and r2sq=bsq+asq
// also, b+c=seperation,
c = (0.5*sep) - ((this.r*this.r*0.5)/sep) + ((cr.r*cr.r*0.5)/sep);
b = sep - c;
a = Math.sqrt(this.r*this.r - b*b);
dx /= sep;
dy /= sep;
crx = -dy;
cry = dx;
pt.x = this.x - (dx*b) + (crx*a);
pt.y = this.y - (dy*b) + (cry*a);
pt.x2 = this.x - (dx*b) - (crx*a);
pt.y2 = this.y - (dy*b) - (cry*a);
}
}
}
return res;
}
// returns 0 if there is no (new) point, ow= the number of intersection points: 1 or 2.
// calling fns use "closer" to choose which is first and which second.
prBase.prototype.intersect = function (it, pt) {
var res = 0;
var tcases = this.t*10 + it.t;
switch (tcases) {
case PR_L*10 + PR_L:
res = this.iLL(it, pt);
break;
case PR_L*10 + PR_C:
res = it.iLC(this, pt);
break;
case PR_C*10 + PR_L:
res = this.iLC(it, pt);
break;
case PR_C*10 + PR_C:
res = this.iCC(it, pt);
break;
default:
// intersections with points, parameters, marks,
//and lofts return nothing.
break;
}
if (res!=0) {
pt.t = PR_P;
}
return res;
}
// set as first intersection
prBase.prototype.saFI = function (obj1, obj2, target) {
var count, which;
var pt1 = new prBase();
var pt2 = new prBase();
count = obj1.intersect(obj2, pt1);
switch (count) {
case 0:
this.t = PR_N;
break;
case 1:
this.saP(pt1.x, pt1.y);
break;
case 2:
pt2.saP(pt1.x2, pt1.y2);
which = this.closer(pt1, pt2, target);
if (which==2) {
this.saP(pt2.x, pt2.y);
}
else {
this.saP(pt1.x, pt1.y);
}
break;
}
}
// set as second intersection
prBase.prototype.saSI = function (obj1, obj2, target) {
var count, which;
var pt1 = new prBase();
var pt2 = new prBase();
count = obj1.intersect(obj2, pt1);
switch (count) {
case 0:
case 1:
this.t = PR_N;
break;
case 2:
pt2.saP(pt1.x2, pt1.y2);
which = this.closer(pt1, pt2, target);
if (which==2) {
this.saP(pt1.x, pt1.y);
}
else {
this.saP(pt2.x, pt2.y);
}
break;
}
}
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function prPage(context) {
this.cx = context;
this.cxw = context.canvas.width;
this.cxh = context.canvas.height;
this.objs = new Array();
this.objs[0] = new prBase; // default is 0== null point; useful! I think.
this.objCount = 1;
// iteration through grouped objs
this.itC = 0; // index of last returned thing in objs[]
this.itG = 0; // group being iterated over
// all prBase have a group number. the group number should
// for pt, line, circle, segment & arc, it's the line color
this.groupColors = [];
this.groupColors[0] = "#fff"; // 0 is the background group
this.groupColors[1] = "#000";
this.currentGroup = 1; // new prBase are given this group number.
// use -1 as a group if you don't want something drawn.
}
prPage.prototype.setGroupColor = function(group, color) {
this.groupColors[group] = color;
}
prPage.prototype.setPageColor = function(group, color) {
this.groupColors[0] = color;
}
prPage.prototype.setCurrentGroup = function(group) {
this.currentGroup = group;
}
prPage.prototype.setGroup = function(obj, group) {
this.objs[obj].g = group;
}
prPage.prototype.redraw = function() {
var i, j, gc, objCt, ob, m1, m2;
gc = this.groupColors.length; // group count
objCt = this.objCount;
// erase
this.cx.fillStyle = this.groupColors[0];
this.cx.beginPath();
this.cx.rect(0,0, this.cxw, this.cxh);
this.cx.fill();
for (i=1; i<gc; i=i+1) { // for each group; group 0=not drawn
this.cx.strokeStyle = this.groupColors[i];
this.cx.beginPath();
for (j=1; j<objCt; j=j+1) {
ob = this.objs[j];
if ((ob.g===i)&&(ob.t!=PR_N)&&(ob.t!=PR_F)) {
this.objs[j].tr(this.cx);
}
if ((ob.g===i)&&(ob.t==PR_F)) {
m1 = this.objs[j].x;
m2 = this.objs[j].y;
}
}
this.cx.stroke();
}
}
/////////////////////// convenience
prPage.prototype.copy = function(it) {
var i;
this.cx = it.cx;
this.bg = it.bg;
this.cxw = it.cx.canvas.width;
for (i=0; i<it.objCount; ++i) {
this.objs[i] = new prBase();
this.objs[i].cp(it.objs[i]);
}
this.objCount = it.objCount;
this.iteratorCounter = it.iteratorCounter;
this.iteratorTag = it.iteratorTag;
}
prPage.prototype.clear = function() {
this.objCount = 1;
this.iteratorCounter = 0;
}
prPage.prototype.merge = function(it) {
var i, mx;
mx = this.objCount;
for (i=0; i<it.objCount; ++i) {
this.objs[mx+i] = new prBase;
this.objs[mx+i].copy(it.objs[i]);
}
this.objCount += it.objCount;
}
/////////////////////// debugging and tracing
prPage.prototype.echo = function() {
var i=0;
for (i=0; i<this.objCount; ++i) {
// print(i);
this.objs[i].ec(i+":");
}
}
prPage.prototype.echoOne = function(which) {
var i=0;
if ((which>0)&&(which<this.objCount)) {
this.objs[which].ec(which + ":") ;
}
}
///////////////////// adding prBase
/// error checks
prPage.prototype.isAPoint = function(ob) {
var res = 0;
if ((0<ob)&&(ob<this.objCount)) { // 0<ob<objCount
if (this.objs[ob]) { // there is an obj at objs[ob]
if (this.objs[ob].t==PR_P) { // is a point
res = 1;
}
}
}
return res;
}
prPage.prototype.isALine = function(ob) {
var res = 0;
if ((0<ob)&&(ob<this.objCount)) { // 0<ob<objCount
if (this.objs[ob]) { // there is an obj at objs[ob]
if (this.objs[ob].t==PR_L) { // is a line
res = 1;
}
}
}
return res;
}
prPage.prototype.isACircle = function(ob) {
var res = 0;
if ((0<ob)&&(ob<this.objCount)) { // 0<ob<objCount
if (this.objs[ob]) { // there is an obj at objs[ob]
if (this.objs[ob].t==PR_C) { // is a circle
res = 1;
}
}