-
Notifications
You must be signed in to change notification settings - Fork 1
/
ed2.html
2563 lines (2240 loc) · 69.1 KB
/
ed2.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Here goes the title</title>
<!-- #BeginEditable "Script" -->
<style type="text/css">
#gxInputOutput { width:500px; }
#gxInputOutput table{ width: 100%; table-layout: fixed;}
.gxCredit { font-size: x-small; padding-top: 10px; }
.gxInputLowerLimit {width: 33%; text-align: left; font-size: small; }
.gxInputCurrentValue {width: 34%; text-align: center; font-size: small; }
.gxInputUpperLimit {width: 33%; text-align: right; font-size: small; }
.gxInputButton { text-align: center; width: 4em; }
ccc {color:red}
bbb {color:blue}
</style>
<!--link href="your_style_sheet.css" rel="stylesheet" type="text/css"-->
<!--[if lt IE 9]><script type="text/javascript" src="http://www.geometryexpressions.com/appgen/excanvas.js"></script><![endif]-->
<script type="text/javascript">
var FIXED_ASPECT_RATIO = 1;
var FIXED_BOUNDS = 0.0;
var BACKGROUND_COLOR = "#ffffff";
// Internal inputs
var X = 1;
var x = -4.6096181312;
var delta = 0.5801553486;
var theta_0 = 1.57154358886444;
// Internal outputs
var JGOB2X = 1;
var JGOB2Y = 5.46773513282306;
var JGOB4X = -4.6096181312;
var JGOB4Y = 1.23643834857846;
var JGOB5X = -4.0294627826;
var JGOB5Y = 1.91540237854513;
var JGOB6X = -5.1897734798;
var JGOB6Y = 0.571849046493562;
var JGOB7A = 0.999999720799713;
var JGOB7B = 0.000747261999999744;
var JGOB7C = 4.02803035016064;
var JGOB8A = 0.999999720799713;
var JGOB8B = 0.000747261999999744;
var JGOB8C = 5.18934470975157;
var JGOB9A = -0.000747261999999683;
var JGOB9B = 0.999999720799713;
var JGOB9C = -1.91841290818209;
var JGOB10A = -0.000747261999999683;
var JGOB10B = 0.999999720799713;
var JGOB10C = -0.575727007343205;
var z_3_OUT3 = 0.678964029966673;
var z_2_Y_OUT2 = 2.16872005846383;
var z_2_X_OUT2 = -3.80849191345174;
var z_1_Y_OUT1 = -0.0243398080425439;
var z_1_X_OUT1 = -5.74956634830696;
var z_0_Y_OUT0 = 1.03911920367147;
var z_0_X_OUT0 = -4.77902913087935;
// Declaration code
// Declare JGOB0
// Declare JGOB1
// Declare JGOB2
// Declare JGOB3
function JGOB3Y(
X )
{
return F(X);
}
// Declare JGOB4
// Declare JGOB5
// Declare JGOB6
// Declare JGOB7
// Declare JGOB8
// Declare JGOB9
// Declare JGOB10
var dragHandlesList = new Array(
new Circle(JGOB4X, JGOB4Y, 6.000000)
);
var textUIdelta;
var textUIFF;
var F=Math.sin;
var textUIvecz_0_X_OUT0_z_0_Y_OUT0;
var textUIvecz_1_X_OUT1_z_1_Y_OUT1;
var textUIvecz_2_X_OUT2_z_2_Y_OUT2;
var textUIOUTz_3_OUT3;
function updateInputs()
{
if (commonGlobal.currentDraggable == 0) {
x = parameterOnFunction(dragHandlesList[0].x , dragHandlesList[0].y );
}
delta = eval(textUIdelta.value);
F = new Function("x","with(Math){ return "+textUIFF.value+";}");
}
function updateOutputs()
{
dragHandlesList[0].x = JGOB4X; dragHandlesList[0].y = JGOB4Y;
textUIvecz_0_X_OUT0_z_0_Y_OUT0.innerHTML = "(" + z_0_X_OUT0.toPrecision(8) + ", " + z_0_Y_OUT0.toPrecision(8) + ")";
textUIvecz_1_X_OUT1_z_1_Y_OUT1.innerHTML = "(" + z_1_X_OUT1.toPrecision(8) + ", " + z_1_Y_OUT1.toPrecision(8) + ")";
textUIvecz_2_X_OUT2_z_2_Y_OUT2.innerHTML = "(" + z_2_X_OUT2.toPrecision(8) + ", " + z_2_Y_OUT2.toPrecision(8) + ")";
textwxmin.innerHTML = commonGlobal.world.xmin
textwxmax.innerHTML = commonGlobal.world.xmax
textwymin.innerHTML = commonGlobal.world.ymin
textwymax.innerHTML = commonGlobal.world.ymax
textUIOUTz_3_OUT3.innerHTML = z_3_OUT3.toPrecision(8);
}
function recalcGeometry(
){
JGOB2X=X;
JGOB2Y=F(X);
JGOB4X=x;
JGOB4Y=F(x);
JGOB5X=(x+delta);
JGOB5Y=F((x+delta));
JGOB6X=(x+(delta*(-1.0)));
JGOB6Y=F((x+(delta*(-1.0))));
JGOB7A=Math.cos((theta_0+((-1.0)*(Math.PI*safePow(2.0,(-1.0))))));
JGOB7B=Math.sin((theta_0+((-1.0)*(Math.PI*safePow(2.0,(-1.0))))));
JGOB7C=((Math.cos((Math.PI+theta_0+((-1.0)*(Math.PI*safePow(2.0,(-1.0))))))*(x+delta))+(Math.sin((Math.PI+theta_0+((-1.0)*(Math.PI*safePow(2.0,(-1.0))))))*F((x+delta))));
JGOB8A=Math.cos((theta_0+((-1.0)*(Math.PI*safePow(2.0,(-1.0))))));
JGOB8B=Math.sin((theta_0+((-1.0)*(Math.PI*safePow(2.0,(-1.0))))));
JGOB8C=((Math.cos((Math.PI+theta_0+((-1.0)*(Math.PI*safePow(2.0,(-1.0))))))*(x+(delta*(-1.0))))+(Math.sin((Math.PI+theta_0+((-1.0)*(Math.PI*safePow(2.0,(-1.0))))))*F((x+(delta*(-1.0))))));
JGOB9A=Math.cos(theta_0);
JGOB9B=Math.sin(theta_0);
JGOB9C=((Math.cos((theta_0+Math.PI))*(x+delta))+(Math.sin((theta_0+Math.PI))*F((x+delta))));
JGOB10A=Math.cos(theta_0);
JGOB10B=Math.sin(theta_0);
JGOB10C=((Math.cos((theta_0+Math.PI))*(x+(delta*(-1.0))))+(Math.sin((theta_0+Math.PI))*F((x+(delta*(-1.0))))));
z_3_OUT3=Math.abs((F(x)+(F((x+delta))*(-1.0))));
z_2_Y_OUT2=F((x+delta));
z_2_X_OUT2=(x+delta);
z_1_Y_OUT1=F((x+(delta*(-1.0))));
z_1_X_OUT1=(x+(delta*(-1.0)));
z_0_Y_OUT0=F(x);
z_0_X_OUT0=x;
}
function computeGeometry( )
{
recalcGeometry();
// Compute JGOB0
// Compute JGOB1
// Compute JGOB3
// Compute JGOB4
// Compute JGOB5
// Compute JGOB6
// Compute JGOB7
// Compute JGOB8
// Compute JGOB9
// Compute JGOB10
}
function setMinBounds(wrld)
{
// Bounds JGOB0
// Bounds JGOB1
// Bounds JGOB3
addFunctionBounds(wrld, JGOB3Y, -6.55674506452178, 2.4135707312921);
// Bounds JGOB4
wrld.extend(JGOB4X, JGOB4Y);
// Bounds JGOB5
wrld.extend(JGOB5X, JGOB5Y);
// Bounds JGOB6
wrld.extend(JGOB6X, JGOB6Y);
// Bounds JGOB7
// Bounds JGOB8
// Bounds JGOB9
// Bounds JGOB10
// ILine Bounds JGOB0
// ILine Bounds JGOB1
// ILine Bounds JGOB3
// ILine Bounds JGOB4
// ILine Bounds JGOB5
// ILine Bounds JGOB6
// ILine Bounds JGOB7
iLineExtend(wrld, JGOB7A, JGOB7B, JGOB7C);
// ILine Bounds JGOB8
iLineExtend(wrld, JGOB8A, JGOB8B, JGOB8C);
// ILine Bounds JGOB9
iLineExtend(wrld, JGOB9A, JGOB9B, JGOB9C);
// ILine Bounds JGOB10
iLineExtend(wrld, JGOB10A, JGOB10B, JGOB10C);
}
function drawShapes(TranMat, InvTranMat, wrld, ctx)
{
// Draw JGOB0
drawAxis(wrld, TranMat, InvTranMat, ctx, 0, 1, 0, "#000000", 1, 0, 0, 1, "#000000");
// Draw JGOB1
drawAxis(wrld, TranMat, InvTranMat, ctx, 1, 0, 0, "#000000", 1, 1, 0, 1, "#000000");
// Draw JGOB3
drawFunction(wrld, TranMat, ctx, JGOB3Y, 0, 0, "#000000", 1)
// Draw JGOB4
drawPoint(TranMat, ctx, JGOB4X, JGOB4Y, 3.300000, "#0000ff", true);
drawString(wrld, TranMat, ctx, JGOB4X , JGOB4Y , "A", 3.300000, "#000000");
// Draw JGOB5
drawPoint(TranMat, ctx, JGOB5X, JGOB5Y, 3.300000, "#0000ff", false);
drawString(wrld, TranMat, ctx, JGOB5X , JGOB5Y , "B", 3.300000, "#000000");
// Draw JGOB6
drawPoint(TranMat, ctx, JGOB6X, JGOB6Y, 3.300000, "#0000ff", false);
drawString(wrld, TranMat, ctx, JGOB6X , JGOB6Y , "C", 3.300000, "#000000");
// Draw JGOB7
drawInfiniteLine(wrld, TranMat, ctx, JGOB7A, JGOB7B, JGOB7C, "#0000ff", 2);
// Draw JGOB8
drawInfiniteLine(wrld, TranMat, ctx, JGOB8A, JGOB8B, JGOB8C, "#ff0000", 2);
// Draw JGOB9
drawInfiniteLine(wrld, TranMat, ctx, JGOB9A, JGOB9B, JGOB9C, "#0000ff", 2);
// Draw JGOB10
drawInfiniteLine(wrld, TranMat, ctx, JGOB10A, JGOB10B, JGOB10C, "#ff0000", 2);
}
function init()
{
textUIdelta = document.getElementById("UIdelta");
textUIFF = document.getElementById("UIFF");
textUIvecz_0_X_OUT0_z_0_Y_OUT0 = document.getElementById("UIvecz_0_X_OUT0_z_0_Y_OUT0");
textUIvecz_1_X_OUT1_z_1_Y_OUT1 = document.getElementById("UIvecz_1_X_OUT1_z_1_Y_OUT1");
textUIvecz_2_X_OUT2_z_2_Y_OUT2 = document.getElementById("UIvecz_2_X_OUT2_z_2_Y_OUT2");
textwxmax = document.getElementById("wxmax");
textwxmin = document.getElementById("wxmin");
textwymax = document.getElementById("wymax");
textwymin = document.getElementById("wymin");
textUIOUTz_3_OUT3 = document.getElementById("UIOUTz_3_OUT3");
doFullUpdate(commonGlobal.world, commonGlobal.screenBox, commonGlobal.canvas, commonGlobal.context, commonGlobal.world2Screen, commonGlobal.screen2World);
textUIdelta.onchange = function() {
doFullUpdate(commonGlobal.world, commonGlobal.screenBox, commonGlobal.canvas, commonGlobal.context, commonGlobal.world2Screen, commonGlobal.screen2World);
};
textUIdelta.onchange();
textUIFF.onchange = function() {
doFullUpdate(commonGlobal.world, commonGlobal.screenBox, commonGlobal.canvas, commonGlobal.context, commonGlobal.world2Screen, commonGlobal.screen2World);
};
textUIFF.onchange();
}
/*
* Common Section
* - Code common to all generated javascript
*/
/************Math functions***************/
function arccos(x)
{
return Math.acos(x);
}
function arcsin(x)
{
return Math.asin(x);
}
function arctan(x)
{
return Math.atan(x);
}
function arcsec(x)
{
var u = Math.asin(Math.sqrt(x*x-1)/x);
if (x<-1)
{
return Math.PI + u;
}
else if (x>1)
{
return u;
}
return Number.NaN
}
function arccsc(x)
{
var u = Math.acos(Math.sqrt(x*x-1)/x);
if (x<-1)
{
return -u;
}
else if (x>1)
{
return u;
}
return Number.NaN
}
function sinh(x)
{
return (((Math.exp(x)+(Math.exp((x*(-1.0)))*(-1.0)))*0.5));
}
function cosh(x)
{
return (((Math.exp(x)+Math.exp((x*(-1.0))))*0.5));
}
function tanh(x)
{
return (((-1.0)+(Math.pow((1.0+Math.exp((x*(-2.0)))),(-1.0))*2.0)));
}
function arcsinh(x)
{
return (Math.log((x+Math.pow((1.0+Math.pow(x,2.0)),0.5))));
}
function arccosh(x)
{
return (Math.log((x+Math.pow(((-1.0)+Math.pow(x,2.0)),0.5))));
}
function arctanh(x)
{
return (Math.log(Math.pow((Math.pow((1.0+(x*(-1.0))),(-1.0))*(1.0+x)),0.5)));
}
function log()
{
if (arguments.length > 1)
{
return Math.log(arguments[0]) / Math.log(arguments[1]);
}
else if (arguments.length ==1)
{
return Math.log(arguments[0]);
}
return Number.NaN;
}
function log10(x)
{
return Math.log(x) / Math.LN10;
}
/***************Types*****************/
function Point(x, y)
{
this.x = x;
this.y = y;
}
function Circle(x, y, r)
{
this.x = x;
this.y = y;
this.radius = r;
}
function Parabola(vx,vy,fx,fy)
{
this.type = "parabola";
this.vx = vx;
this.vy = vy;
this.fx = fx;
this.fy = fy;
}
function LinePair(A0,B0,C0,A1,B1,C1)
{
this.type = "line pair";
this.A0 = A0;
this.B0 = B0;
this.C0 = C0;
this.A1 = A1;
this.B1 = B1;
this.C1 = C1;
}
function Hyperbola(f0x,f0y,f1x,f1y,p0x,p0y)
{
this.type = "hyperbola";
this.f0x = f0x;
this.f0y = f0y;
this.f1x = f1x;
this.f1y = f1y;
this.p0x = p0x;
this.p0y = p0y;
}
function Ellipse(centerx,centery,p0x,p0y,minor)
{
this.type = "ellipse";
this.centerx = centerx;
this.centery = centery;
this.p0x = p0x;
this.p0y = p0y;
this.minor = minor;
}
function BoundingBox(xmin, ymin, xmax, ymax)
{
this.xmin = xmin;
this.ymin = ymin;
this.xmax = xmax;
this.ymax = ymax;
this.started = false;
this.iLinesStarted = false;
}
BoundingBox.prototype = {
constructor : BoundingBox,
extend : function(x, y)
{
if (isFinite(x) && isFinite(y))
{
if (!this.started)
{
this.xmax = x;
this.xmin = x;
this.ymax = y;
this.ymin = y;
this.started = true;
}
else
{
if (this.xmin > x) this.xmin = x;
if (this.ymin > y) this.ymin = y;
if (this.xmax < x) this.xmax = x;
if (this.ymax < y) this.ymax = y;
}
}
}
}
function Tran2DMatrix(m11, m12, m21, m22, dx, dy)
{
//dest and source must be of type BoundingBox
this.m11 = m11; this.m12 = m12;
this.m21 = m21; this.m22 = m22;
this.dx = dx;
this.dy = dy;
}
Tran2DMatrix.prototype = {
constructor : Tran2DMatrix,
setMatrix : function(source, dest)//Note this sets matrix to invert y direction
{ //when changing World to Screen and vice-versa
this.m11 = (dest.xmax - dest.xmin) / (source.xmax - source.xmin);
this.m12 = 0;
this.m21 = 0;
this.m22 = (dest.ymax - dest.ymin) / (source.ymin - source.ymax);
this.dx = -this.m11 * source.xmin + dest.xmin;
this.dy = -this.m22 * source.ymax + dest.ymin;
}
}
/***************Variables**************/
var commonGlobal = new Object();
var firstTimeIn = 1;
var smallnumber = 1e-6
/***************Functions**************/
function isTiny(x)
{
return Math.abs(x) < smallnumber ? true:false;
}
function safePow(a, b)
{
if(Math.abs(a) < 1E-8)
{
return 0;
}
else
{
return Math.pow(a, b);
}
}
function lerp(x0, x1, y0, y1, x)
{
if(x1 == x)
{
return y1;
}
else
{
return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
}
}
function getArrowDim()
{
var dim = 10;
return new Point(dim*0.8, dim*3.2);
}
function getGCD(val1, val2)
{
var w, x, y;
x = val1;
y = val2;
while (y != 0)
{
w = x % y;
x = y;
y = w;
}
return x;
}
function setCvsOffset()
{
var currentElement = commonGlobal.canvas;
var totalOffsetX = 0;
var totalOffsetY = 0;
do{
totalOffsetX += currentElement.offsetLeft;
totalOffsetY += currentElement.offsetTop;
}
while(currentElement = currentElement.offsetParent)
commonGlobal.cvsMinX = totalOffsetX;
commonGlobal.cvsMinY = totalOffsetY;
}
function getBrowser()
{
//var nAgt = navigator.userAgent;
var browserName = navigator.appName;
//var fullVersion = ''+parseFloat(navigator.appVersion);
//var majorVersion = parseInt(navigator.appVersion,10);
//var verOffset;
//if ((verOffset=nAgt.indexOf("MSIE"))!=-1)
return browserName;
}
function rangeVal(slider, rng)
{
if(commonGlobal.browser != "Opera")
{
return eval(slider.value);
}
else
{
return rng.x + ((eval(slider.value) / (commonGlobal.canvas.width/2)) * (rng.y - rng.x));
}
}
function correctSlider(slider, range)
{
if(commonGlobal.browser == "Opera")
{
slider.value = (slider.value - slider.min)/(slider.max - slider.min) * (commonGlobal.canvas.width / 2);
slider.min = 0;
slider.max = commonGlobal.canvas.width / 2;
slider.step = 1;
}
else
{
slider.step = (slider.max - slider.min) * 2 / commonGlobal.canvas.width;
}
}
function parameterOnCircle(x,y,cx,cy)
{
var dx = x-cx;
var dy = y - cy;
var t = Math.atan2(dy,dx);
return t;
}
function parameterOnFunction(x,y)
{
return x;
}
function parameterOnLine(x,y,A,B,C)
{
return (B*x-A*y)/(A*A+B*B);
}
function parameterOnSegment(x,y,x0,y0,x1,y1)
{
var ux = x1-x0;
var uy = y1 - y0;
var vx = x-x0;
var vy = y-y0;
var len2 = ux*ux+uy*uy;
if (isTiny(len2)) return 0;
return (ux*vx+uy*vy) / len2;
}
function distanceToPointSquared(x,y,x0,y0)
{
var vx = x-x0;
var vy = y-y0;
return vx*vx+vy*vy;
}
function distanceToPoint(x,y,x0,y0)
{
var dd = distanceToPointSquared(x,y,x0,y0);
if (isTiny(dd)) return 0;
return Math.sqrt(dd);
}
function parameterOnCurveRecursive(x,y,f,g, start, end, NUM_SEGS,level )
{
var t = start;
var dt = (end-start)/ (NUM_SEGS);
var i;
var have_solution = false;
var best_t;
var best_distance;
for (i=0;i<NUM_SEGS+1;i++)
{
var xx = f(t);
var yy = g(t);
if (isFinite(xx) && isFinite(yy))
{
var d2 = distanceToPointSquared(x,y,xx,yy);
if ((!have_solution) || (d2 < best_distance))
{
have_solution = true;
best_t = t;
best_distance = d2
}
}
t = t+dt;
}
if (level>0)
{
level--;
var t0 = best_t-dt;
var t1 = best_t+dt;
if (t0<start) t0 = start;
if (t1>end) t1 = end;
best_t = parameterOnCurveRecursive(x,y,f,g, t0,t1, 10,level )
}
return best_t;
}
function parameterOnCurve(x,y,f,g, start, end)
{
return parameterOnCurveRecursive(x,y,f,g, start, end,100,2);
}
function transformCoord(mat, x, y)
{
return new Point(
mat.m11 * x + mat.m12 * y + mat.dx,
mat.m21 * x + mat.m22 * y + mat.dy
);
}
function iLineExtend(wrld, A, B, C)
{
if(wrld.iLinesStarted || (!wrld.started || ((wrld.xmax - wrld.xmin) == 0 || (wrld.ymax - wrld.ymin) == 0)))
{
wrld.iLinesStarted = true;
var x = (-A*C/(A*A + B*B));
var y = (-B*C/(A*A + B*B));
wrld.extend(x, y);
}
}
function setMargin(wrld, margin)
{
var dx = eval(margin*(wrld.xmax - wrld.xmin));
var dy = eval(margin*(wrld.ymax - wrld.ymin));
wrld.xmin -= eval(dx);
wrld.ymin -= eval(dy);
wrld.xmax = eval(wrld.xmax) + eval(dx);
wrld.ymax = eval(wrld.ymax) + eval(dy);
}
// Correct world bounding box if it has no width or height
function correctDegenerate(wrld)
{
if (wrld.xmax <= wrld.xmin)
{
wrld.xmin -= 0.5;
wrld.xmax = eval(wrld.xmax) + eval(0.5);
}
if (wrld.ymax <= wrld.ymin)
{
wrld.ymin -= 0.5;
wrld.ymax = eval(wrld.ymax) + eval(0.5);
}
}
function correctAspectRatio(wrld, scrn)
{
if(((scrn.xmax - scrn.xmin) != 0) && ((scrn.ymax - scrn.ymin) != 0))
{
var dx = wrld.xmax - wrld.xmin;
var dy = wrld.ymax - wrld.ymin;
var xratio = dx / (scrn.xmax - scrn.xmin);
var yratio = dy / (scrn.ymax - scrn.ymin);
if(xratio > yratio)
{
var ymid = (wrld.ymin + wrld.ymax) / 2;
dy = (xratio * (scrn.ymax - scrn.ymin)) / 2;
wrld.ymin = ymid - dy;
wrld.ymax = ymid + dy;
}
else
{
var xmid = (wrld.xmin + wrld.xmax) / 2;
dx = (yratio * (scrn.xmax - scrn.xmin)) / 2;
wrld.xmin = xmid - dx;
wrld.xmax = xmid + dx;
}
}
}
function setMatrices(wrld, scrn, TranMat, InvTranMat)
{
TranMat.setMatrix(wrld, scrn);
InvTranMat.setMatrix(scrn, wrld);
}
function resetBounds(wrld, scrn, TranMat, InvTranMat)
{
wrld.started = false;
if ((!FIXED_BOUNDS) || firstTimeIn)
{
setMinBounds(wrld);
correctDegenerate(wrld);
if (!FIXED_BOUNDS)
{
setMargin(wrld, .075);
}
firstTimeIn = 0;
}
if (FIXED_ASPECT_RATIO)
{
correctAspectRatio(wrld, scrn);
}
setMatrices(wrld, scrn, TranMat, InvTranMat);
}
// dx dy are proportions of world extent
function panWorld(dx,dy)
{
var wrld = commonGlobal.world;
var w = wrld.xmax - wrld.xmin;
var h = wrld.ymax - wrld.ymin;
var u = w*dx;
var v = h*dy;
wrld.xmax += u;
wrld.xmin += u;
wrld.ymax += v;
wrld.ymin += v;
}
function zoomCoordinate(x,x0,k)
{
return x*k+x0*(1-k);
}
// dx dy are proportions of world extent
function zoomWorld(x0,y0,scale)
{
var wrld = commonGlobal.world;
wrld.xmax = zoomCoordinate(wrld.xmax,x0,scale);
wrld.xmin = zoomCoordinate(wrld.xmin,x0,scale);
wrld.ymax = zoomCoordinate(wrld.ymax,y0,scale);
wrld.ymin = zoomCoordinate(wrld.ymin,y0,scale);
}
// Functions for working with auxiliary canvases, used for drawing traces
// Return an auxiliary context to draw on with the same dimensions as the given
// context
function submitCreateAux(ctx, alpha)
{
var auxCanvas = document.createElement("canvas");
if (auxCanvas.getContext)
{
auxCanvas.setAttribute("width", ctx.canvas.width);
auxCanvas.setAttribute("height", ctx.canvas.height);
return auxCanvas.getContext("2d");
}
else
{
// XXX: ExplorerCanvas does not support auxiliary canvases, so just set
// the globalAlpha here and return the same context we were given. The
// results won't look exactly like they should, but it will show
// something.
ctx.globalAlpha = alpha;
return ctx;
}
}
// Draw image data from a previously-created auxiliary canvas
function submitDrawAux(ctx, aux, alpha)
{
if (ctx != aux)
{
// XXX: ExplorerCanvas does not support drawImage from other canvases.
ctx.globalAlpha = alpha;
ctx.drawImage(aux.canvas, 0, 0);
}
ctx.globalAlpha = 1;
}
// Functions for specifying drawing paths
function submitBeginPath(ctx)
{
ctx.beginPath();
}
function submitMoveTo(mat, ctx, point)
{
var tpoint = transformCoord(mat, point.x, point.y);
ctx.moveTo(tpoint.x, tpoint.y);
}
function submitLineTo(mat, ctx, point)
{
var tpoint = transformCoord(mat, point.x, point.y);
ctx.lineTo(tpoint.x, tpoint.y);
}
function submitPolyLine(mat, ctx, points)
{
for (var i = 0; i < points.length; ++i)
{
if (isFinite(points[i].x) && isFinite(points[i].y))
{
var point = transformCoord(mat, points[i].x, points[i].y);
ctx.lineTo(point.x, point.y);
}
}
}
function submitPolyLineReverse(mat, ctx, points)
{
for (var i = 0; i < points.length; ++i)
{
var point = points[points.length-1-i];
if (isFinite(point.x) && isFinite(point.y))
{
var tpoint = transformCoord(mat, point.x, point.y);
ctx.lineTo(tpoint.x, tpoint.y);
}
}
}
// Functions for drawing paths specified using the functions above
// TODO: Allow specifying a fill pattern
function submitFill(ctx, red, green, blue, alpha)
{
ctx.fillStyle = "rgba(" + red + ", " + green + ", " + blue + ", " + alpha + ")";
ctx.fill();
ctx.fillStyle = "#000000";
}
// TODO: Allow specifying a line pattern
function submitStroke(ctx, color, thickness)
{
ctx.strokeStyle = color;
ctx.lineWidth = thickness;
ctx.stroke();
ctx.strokeStyle = "#000000";
ctx.lineWidth = 1;
}
// Functions for drawing normal geometry objects
function drawAxis(wrld, mat, invMat, ctx, A, B, C, clrStr, thickness, type, units, labels, clrTxt)
{
drawInfiniteLine(wrld, mat, ctx, A, B, C, clrStr, thickness);
var label = new Object;
label.symbol = "";
label.divisor = 1;
label.magnitude = 1;
var dist = getGridSpacing(wrld, units, label);
var ticHalf = 5;
if(type == 0) //X-axis
{
if((wrld.ymin < 0) && (wrld.ymax > 0))
{
var posLo = Math.ceil(wrld.xmin / dist);
var posHi = Math.ceil(wrld.xmax / dist);
for (var i = posLo; i < posHi; ++i)
{
var pos = i * dist;
drawAxisTic(mat, ctx, type, pos, clrStr, thickness, ticHalf);
if(labels)
{
drawTicLabel(mat, ctx, type, pos, dist, units, label, clrTxt, ticHalf);
}
}
}
}
else
{
if((wrld.xmin < 0) && (wrld.xmax > 0))
{
var posLo = Math.ceil(wrld.ymin / dist);
var posHi = Math.ceil(wrld.ymax / dist);
for (var i = posLo; i < posHi; ++i)
{
var pos = i * dist;
drawAxisTic(mat, ctx, type, pos, clrStr, thickness, ticHalf);
if(labels)
{
drawTicLabel(mat, ctx, type, pos, dist, units, label, clrTxt, ticHalf);
}
}
}
}
}
function getGridSpacing(wrld, units, label)
{
var gspacing = 0;
var width = wrld.xmax - wrld.xmin;
var height = wrld.ymax - wrld.ymin;
var size = (width > height) ? width : height;
var degToRad = (2.0*Math.PI)/360.0;
// see what the ideal spacing would be if we wanted AXIS_IDEAL_LABEL_COUNT on the screen
var idealspacing = (size / commonGlobal.axisIdealLabelCount);
// idealspacing needs to be in the range of 1 to 10 in order for spacing to work
// save the magnitude and apply it to gspacing at the end
var magnitude = 1.0;
if(units == 0) // decimal spacing
{
var power = Math.floor(Math.log(Math.abs(idealspacing)) / Math.log(10));
magnitude = Math.pow(10.0, power);
label.magnitude = magnitude;
idealspacing /= magnitude;
// tics will be some magnitude of 10, 5, 2 or 1
// see which division is closest to the ideal spacing
var tics10 = Math.abs(idealspacing - (10.0));
var tics5 = Math.abs(idealspacing - (5.0));
var tics2 = Math.abs(idealspacing - (2.0));
var tics1 = Math.abs(idealspacing - (1.0));
if (tics10 < tics5 && tics10 < tics2 && tics10 < tics1)
{
gspacing = 10.0;
}
else if (tics5 < tics2 && tics5 < tics1)
{
gspacing = 5.0;
}
else if (tics2 < tics1)
{
gspacing = 2.0;
}
else
{
gspacing = 1.0;
}
}
else if(units == 1) // pi and degree spacing for degrees
{
label.symbol = "\u00B0";
if(idealspacing<=(5*degToRad))
{
gspacing=5*degToRad;
}
else if(idealspacing<=(15*degToRad))
{
gspacing=15*degToRad;
}
else if(idealspacing<=(45*degToRad))
{
gspacing=45*degToRad;