forked from Khan/khan-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizing-derivatives.js
More file actions
1147 lines (947 loc) · 38.3 KB
/
visualizing-derivatives.js
File metadata and controls
1147 lines (947 loc) · 38.3 KB
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
$.extend(KhanUtil, {
PiecewiseFunction: function(options) {
options = $.extend(true, {
INTERVAL_WIDTH: 2,
fnArray: [],
rangeArray: [],
_paths: {}
}, options);
$.extend(this, options);
_.bindAll(this);
this.length = function() {
return this.fnArray.length;
};
this.width = function() {
var n = this.length();
return this.rangeArray[n - 1][1] - this.rangeArray[0][0];
};
this.toTextArray = function(fnArray) {
if (!fnArray) {
fnArray = this.fnArray;
}
return _.map(fnArray, function(fn) {
return fn.text();
});
};
this._shiftArray = function() {
var w = this.INTERVAL_WIDTH;
return _.map(this.rangeArray, function(range, i) {
// shift by the original interval start points
// (before trimming if the problem is offset)
return Math.floor(range[0] / w) * w;
});
};
this._shiftFnArray = function() {
var shiftArray = this._shiftArray();
return _.map(this.fnArray, function(fn, i) {
return fn.scale(1, -shiftArray[i]);
});
};
this.derivative = function(fnArray) {
if (!fnArray) {
fnArray = this.fnArray;
}
return _.map(fnArray, function(fn, i) {
return fn.derivative();
});
};
this.translate = function(dx, dy) {
// dx changes rangeArray
if (dx) {
this.rangeArray = _.map(this.rangeArray, function(range) {
return _.map(range, function(x) {
return x + dx;
});
});
}
// dy changes functions
if (dy) {
this.fnArray = _.map(this.fnArray, function(fn) {
var result = fn.add(dy);
return result;
});
}
};
// shift rangeArray so that it starts at x = 0
// shift fnArray so that smallest value is at y = 0
this.calibrate = function() {
var dx = this.rangeArray[0][0];
var w = this.INTERVAL_WIDTH;
var dy = this.fnArray[0].evalOf(0);
_.each(this.fnArray, function(fn) {
// NOTE: only checks start and endpoints at the moment
var start = fn.evalOf(0);
var end = fn.evalOf(w);
dy = Math.min(dy, start, end);
});
this.translate(-dx + 2, -dy);
};
// like w/ JS arrays, startIndex is inclusive, endIndex is exclusive
this.slice = function(startIndex, endIndex) {
if (startIndex >= endIndex) {
return null;
}
var crop = function(array) {
return array.slice(startIndex, endIndex);
};
return new KhanUtil.PiecewiseFunction({
fnArray: crop(this.fnArray),
rangeArray: crop(this.rangeArray)
});
};
// returns a new piecewiseFunction
this.concat = function(piecewiseFn) {
var fnArray = this.fnArray.concat(piecewiseFn.fnArray);
// shift the rangeArray of the second function
var offset = piecewiseFn.rangeArray[0][0];
var start = this.rangeArray[this.rangeArray.length - 1][1];
var shiftedRangeArray = _.map(piecewiseFn.rangeArray, function(range) {
return _.map(range, function(x) {
return x - offset + start;
});
});
var rangeArray = this.rangeArray.concat(shiftedRangeArray);
return new KhanUtil.PiecewiseFunction({
fnArray: fnArray,
rangeArray: rangeArray
});
};
// find matches w/ piecewiseFunction in this object
// only compares fnArray, not rangeArray
this.matches = function(piecewiseFunction, useDerivative) {
var original = this.fnArray;
var other = piecewiseFunction.fnArray;
if (useDerivative) {
original = this.derivative();
other = piecewiseFunction.derivative();
}
original = this.toTextArray(original);
other = this.toTextArray(other);
// no matches if the parameter object has more segments than this
// object
var n = other.length;
var diff = original.length - n;
if (diff < 0) {
return null;
}
var matches = [];
_.times(diff + 1, function(i) {
if (_.isEqual(original.slice(i, i + n), other)) {
matches.push(i);
}
});
return matches;
};
// used in generating the problem graphs
// ex: [[0, 1], [1, 2]] with offset == 0.25
// ==> [[0.25, 1], [1, 1.75]]
this.trimRangeArrayEnds = function(offset) {
// for some reason, an offset of 0 causes this to crash
// without this line. ?_?
if (offset === 0) {
return;
}
var n = this.length();
var offsetAmt = this.INTERVAL_WIDTH * offset;
this.rangeArray[0][0] += offsetAmt;
this.rangeArray[n - 1][1] += (offsetAmt - this.INTERVAL_WIDTH);
};
this._makePlotArray = function(fnArray) {
if (!fnArray) {
fnArray = this.fnArray;
}
return _.map(fnArray, function(fn, i) {
return function(x) {
return fn.evalOf(x);
};
});
};
this._plotEndpoints = function(color, plotDerivative, omitEnds) {
var shiftedFnArray = this._shiftFnArray();
var antiderivFnArray;
if (plotDerivative) {
antiderivFnArray = shiftedFnArray;
shiftedFnArray = this.derivative(shiftedFnArray);
}
// compute endpoints
var emptyEndpoints = [];
var filledEndpoints = [];
var derivEndpoints = [];
var prevX = null;
var prevY = null;
var prevOrigY = null;
var n = this.rangeArray.length;
_.each(this.rangeArray, function(range, i) {
var fn = shiftedFnArray[i];
if (plotDerivative) {
var origFn = antiderivFnArray[i];
}
_.each(range, function(x, j) {
var y = fn.evalOf(x);
var origY = plotDerivative ? origFn.evalOf(x) : null;
if (i > 0 && j === 0) {
// if the graph is not connected, add endpoints
if (y !== prevY || x !== prevX) {
// arbitrarily always set "earlier" endpoint as
// filled
filledEndpoints.push([prevX, prevY]);
emptyEndpoints.push([x, y]);
} else {
// don't add endpoints to derivative if both the
// graph and derivative are continuous at (x, y)
if (origY !== prevOrigY || x !== prevX) {
derivEndpoints.push([x, y]);
}
}
}
// start off with an empty first endpoint
// omit first and last endpoints in a cropped window
if (i === 0 && j === 0 && !omitEnds) {
emptyEndpoints.push([x, y]);
}
// end with a filled endpoint
if (i === n - 1 && j === 1 && !omitEnds) {
filledEndpoints.push([x, y]);
}
prevX = x;
prevY = y;
prevOrigY = origY;
});
});
// all endpoints are undefined (empty) in a derivative graph
if (plotDerivative) {
emptyEndpoints = derivEndpoints.concat(emptyEndpoints)
.concat(filledEndpoints);
filledEndpoints = [];
}
// plot endpoints
var self = this;
this._paths["emptyEndpoints"] = this.graphie.style({
stroke: color,
strokeWidth: 3,
fill: "#FFFFFF"
}, function() {
return self.graphie.plotEndpointCircles(emptyEndpoints);
});
this._paths["filledEndpoints"] = this.graphie.style({
stroke: color,
strokeWidth: 3,
fill: color
}, function() {
return self.graphie.plotEndpointCircles(filledEndpoints);
});
};
this._plotSegments = function(color, plotDerivative) {
var shiftedFnArray = this._shiftFnArray();
if (plotDerivative) {
shiftedFnArray = this.derivative(shiftedFnArray);
}
var plotArray = this._makePlotArray(shiftedFnArray);
var self = this;
this._paths["segments"] = this.graphie.style({
stroke: color,
strokeWidth: 3
}, function() {
return self.graphie.plotPiecewise(plotArray, self.rangeArray);
});
};
this.plot = function(options) {
options = $.extend(true, {
color: KhanUtil.GREEN,
plotDerivative: false,
omitEnds: false
}, options);
this.graphie = options.graphie || KhanUtil.currentGraph;
// plot segments
this._plotSegments(options.color, options.plotDerivative);
// plot endpoints
this._plotEndpoints(options.color, options.plotDerivative, options.omitEnds);
};
this.translatePlot = function(dx, dy) {
var scaled = this.graphie.scaleVector([dx, dy]);
_.each(this._paths, function(pathSet) {
pathSet.translate(scaled[0], scaled[1]);
});
};
// simply changes the opacity of the segment and endpoint paths so
// they aren't visible
this.hide = function(speed) {
if (this.hidden) {
return;
}
speed = speed || 100;
_.each(this._paths, function(pathSet) {
if (pathSet.length === 0) {
return;
}
pathSet.animate({
"fill-opacity": 0,
"opacity": 0
}, speed);
});
this.hidden = true;
};
// make paths visible again
this.show = function(speed) {
if (!this.hidden) {
return;
}
speed = speed || 100;
_.each(this._paths, function(pathSet) {
if (pathSet.length === 0) {
return;
}
pathSet.animate({
"fill-opacity": 1,
"opacity": 1
}, speed);
});
this.hidden = false;
};
this.toFront = function() {
_.each(this._paths, function(pathSet) {
pathSet.toFront();
});
};
this.cleanup = function() {
_.each(this._paths, function(pathSet) {
pathSet.remove();
});
this._paths = {};
};
},
PiecewiseFunctionGenerator: {
_init: (function() {
_.bindAll(KhanUtil.PiecewiseFunctionGenerator);
})(),
// UTILITY FUNCTIONS
_isInRange: function(ylims, val) {
return !(val <= ylims[0] || val >= ylims[1]);
},
// create an n-length array
_scramble: function(n) {
var newIndices = [];
_.times(n, function(i) {
var newIndex = KhanUtil.randRangeExclude(0, n - 1, newIndices);
newIndices.push(newIndex);
});
return newIndices;
},
// add new piecewise functions here
curveTypes: {
"line": {
// slopes
params: {
m: [-1, -0.5, 0, 0.5, 1]
},
// convert from f(x) = m(x - a) + b
// to f(x) = Ax + B
// return [B, A]
generate: function(m, b) {
var coefs = [b, m];
return new KhanUtil.Polynomial(0, 1, coefs);
}
},
"curve": {
params: {
isLeftCurve: [true, false],
m: [-1, 1]
},
// convert from f(x) = m(x - a)^2 + b
// to f(x) = Ax^2 + Bx + C
// return [C, B, A]
generate: function(isLeftCurve, m, b, intervalWidth) {
b = isLeftCurve ? b - m * intervalWidth : b;
var a = isLeftCurve ? intervalWidth : 0;
m = m / intervalWidth;
var coefs = [Math.pow(a, 2) * m + b, -2 * a * m, m];
return new KhanUtil.Polynomial(0, 2, coefs);
}
}
},
// converts curveTypes into an array of options used in the
// recursive generator function
makeCombinations: function() {
// [[1,2,3], ["one", "two", "three"], ["a", "b", "c"]];
var makeParamSets = function(curveTypes) {
var paramSets = [];
_.each(curveTypes, function(curveType, key) {
// since key is a string, it needs to be wrapped in
// an array
var paramSet = [[key]];
_.each(curveType.params, function(val) {
paramSet.push(val);
});
paramSets.push(paramSet);
});
return paramSets;
};
var addParam = function(currList, paramSet, paramIndex) {
if (paramIndex === paramSet.length) {
combinations.push(currList);
return;
}
var params = paramSet[paramIndex];
for (var i = 0; i < params.length; i++) {
currList.push(params[i]);
addParam(currList.slice(), paramSet, paramIndex + 1);
currList.pop();
}
};
var makeCombinations = function() {
var paramSets = makeParamSets(self.curveTypes);
_.each(paramSets, function(paramSet) {
addParam([], paramSet, 0);
});
};
// POSSIBLE TODO(stephanie): make combinations not a global variable
var self = this;
var combinations = [];
makeCombinations();
return combinations;
},
_randomY: function(ylims, excludes) {
return KhanUtil.randRangeExclude(ylims[0], ylims[1], ylims.concat(excludes));
},
generate: function(options) {
options = $.extend(true, {
INTERVAL_WIDTH: 2,
YLIMS: [],
numSegments: 1,
startVal: null, // overrides prevSegment start val
prevSegment: null,
breakIndex: null
}, options);
if (!this.combinations) {
this.combinations = this.makeCombinations();
}
var self = this;
var YLIMS = options.YLIMS;
var INTERVAL_WIDTH = options.INTERVAL_WIDTH;
var endVal = function(segment) {
return segment.evalOf(options.INTERVAL_WIDTH);
};
// start the segment at startVal
var createSegment = function(startVal, prevSegment) {
// POSSIBLE TODO(stephanie): add weights to the combinations
if (startVal == null) {
if (prevSegment == null) {
startVal = self._randomY(YLIMS);
} else {
startVal = endVal(prevSegment);
}
}
// iterate through combinations in a random order each time
var indices = self._scramble(self.combinations.length);
for (var j = 0; j < indices.length; j++) {
var params = self.combinations[indices[j]].slice();
var curveType = self.curveTypes[params[0]];
// add the value of "b" to the params array
params.push(startVal);
params.push(INTERVAL_WIDTH);
var segment = curveType["generate"].apply(null, params.slice(1));
var y = endVal(segment);
// segment is not qualified if it causes the graph to go
// outside the y limits
if (!self._isInRange(YLIMS, y)) {
continue;
}
// segment is not allowed to have the same params as
// the previous one
if (prevSegment) {
var y0 = prevSegment.evalOf(0);
var s0 = prevSegment.subtract(y0).text();
var s1 = segment.subtract(startVal).text();
if (s0 === s1) {
continue;
}
}
// POSSIBLE TODO(stephanie): check for other constraints here
return segment;
}
return null;
};
var makePiecewiseGraph = function() {
var segments = [];
var start = options.startVal;
var prev = options.prevSegment;
while (segments.length < options.numSegments) {
var len = segments.length;
if (options.breakIndex && len === options.breakIndex) {
start = self._randomY(YLIMS, [start]);
}
var segment = createSegment(start, prev);
// no possible problem with these starting segments,
// so start over completely
if (!segment) {
segments = [];
prev = options.prevSegment;
start = options.startVal;
}
segments.push(segment);
prev = segment;
start = null;
}
return segments;
};
var fnArray = makePiecewiseGraph();
var rangeArray = [];
_.each(fnArray, function(fn, i) {
var x = i * options.INTERVAL_WIDTH;
rangeArray.push([x, x + options.INTERVAL_WIDTH]);
});
return new KhanUtil.PiecewiseFunction({
fnArray: fnArray,
rangeArray: rangeArray
});
}
},
VisualizingDerivativesProblem: function(options) {
options = $.extend(true, {
XLIMS: [0, 14],
YLIMS: [-2, 4],
GRAPH_LIMS: [[], []],
INTERVAL_WIDTH: 2,
problem: null,
graph: null,
nIntervals: 7,
nProblemIntervals: 1,
offset: 0.5,
breakIndex: 3,
noSolution: false,
moveDerivative: true,
fnColor: KhanUtil.BLUE,
derivColor: KhanUtil.RED
}, options);
$.extend(this, options);
this._setAxisLims = function() {
var width = this.INTERVAL_WIDTH;
var n = this.nIntervals;
this.XLIMS = [0, width * n];
this.YLIMS = [-width, 2 * width];
var pad = function(lims, padAmt) {
return [lims[0] - padAmt, lims[1] + padAmt];
};
this.GRAPH_LIMS = [pad(this.XLIMS, width), pad(this.YLIMS, 1)];
};
this._invalidParams = function(piecewiseFn, startIndex, nIntervals) {
var outOfBounds = startIndex + nIntervals > piecewiseFn.length();
if (!piecewiseFn || startIndex === null ||
nIntervals === 0 || outOfBounds) {
return true;
}
return false;
};
this.chooseProblemStart = function(totalIntervals, nIntervals, offset) {
var upperBound = totalIntervals - (nIntervals + Math.ceil(offset));
return KhanUtil.randRange(0, upperBound);
};
this.matchesToProblemRanges = function(matches, nIntervals, offset) {
var w = this.INTERVAL_WIDTH;
return _.map(matches, function(matchIndex) {
var startIndex = matchIndex + offset;
var endIndex = startIndex + nIntervals;
return _.map([startIndex, endIndex], function(x) {
return w * x;
});
});
};
this.generateProblem = function(piecewiseFn, startIndex, nIntervals, offset) {
// we need one more interval if there's an offset
nIntervals += (offset === 0) ? 0 : 1;
if (this._invalidParams(piecewiseFn, startIndex, nIntervals)) {
return null;
}
var endIndex = startIndex + nIntervals;
var problem = piecewiseFn.slice(startIndex, endIndex);
// we need to trim the rangeArray if there's an offset
problem.trimRangeArrayEnds(offset);
return problem;
};
this.generateBogusProblem = function(piecewiseFn, startIndex, nIntervals, offset) {
// we need one more interval if there's an offset
nIntervals += (offset === 0) ? 0 : 1;
if (this._invalidParams(piecewiseFn, startIndex, nIntervals)) {
return null;
}
var generator = KhanUtil.PiecewiseFunctionGenerator;
var bogusProblem;
var loopCount = 0;
var validBogus = false;
while (!validBogus) {
// only one interval, so it must be bogus/invalid
if (nIntervals === 1) {
bogusProblem = generator.generate({
nIntervals: 1,
INTERVAL_WIDTH: this.INTERVAL_WIDTH,
YLIMS: this.YLIMS
});
} else {
// as few as 1 and as many as all but one interval can be
// valid
var nValidIntervals = KhanUtil.randRange(1, nIntervals - 1);
// the rest of the intervals are bogus
var nBogusIntervals = nIntervals - nValidIntervals;
var endIndex = startIndex + nValidIntervals;
var prefixFn = piecewiseFn.slice(startIndex, endIndex);
var n = prefixFn.length();
var prevSegment = prefixFn.fnArray[n - 1];
bogusProblem = generator.generate({
numSegments: nBogusIntervals,
prevSegment: prevSegment,
INTERVAL_WIDTH: this.INTERVAL_WIDTH,
YLIMS: this.YLIMS
});
bogusProblem = prefixFn.concat(bogusProblem);
}
var matches = piecewiseFn.matches(bogusProblem, true);
if (!matches.length) {
validBogus = true;
}
// if we can't find a valid bogus in a reasonable # of tries,
// quit and generate a new problem. 50 is an arbitrary
// threshold.
if (loopCount > 50) {
return null;
}
loopCount++;
}
// we need to trim the rangeArray if there's an offset
bogusProblem.trimRangeArrayEnds(offset);
return bogusProblem;
};
this.initSlidingWindow = function(options) {
KhanUtil.addMouseLayer();
var problem = options.problem;
// compute lims
var xlims = this.GRAPH_LIMS[0];
var ylims = this.GRAPH_LIMS[1];
// window cannot move past (the edges of the graph area - 1)
var xmin = xlims[0] + 1;
var xmax = xlims[1] - 1;
// window extends to the top and bottom edges of the graph area
var ymin = ylims[0];
var ymax = ylims[1];
var height = ymax - ymin;
var slidingWindow = KhanUtil.addRectGraph({
x: xmin,
y: ymin,
width: problem.width(),
height: height,
normalStyle: {
area: {
"fill-opacity": 0.08,
fill: options.color
},
edges: {
"stroke-width": 0
},
points: {
opacity: 0
}
},
hoverStyle: {
area: {
"fill-opacity": 0.14,
fill: options.color
},
points: {
opacity: 0
}
},
fixed: {
edges: [true, true, true, true],
points: [true, true, true, true]
},
constraints: {
constrainX: false,
constrainY: true,
xmin: xmin,
xmax: xmax
},
onMove: function(dx, dy) {
problem.translatePlot(dx, dy);
problem.toFront();
}
});
// number of milliseconds it takes the slidingWindow to fade in/out
var speed = 20;
slidingWindow.doHide = function() {
slidingWindow.hide(speed);
problem.hide(speed);
options.onHide();
};
slidingWindow.doShow = function() {
slidingWindow.show(speed);
problem.show(speed);
options.onShow();
};
// problem graph should be in front of sliding window
slidingWindow.toFront();
problem.toFront();
var xOffset = xmin + -problem.rangeArray[0][0];
problem.translatePlot(xOffset, 0);
slidingWindow.startRange = [xmin, xmin + problem.width()];
// attach sliding window to this object
this.slidingWindow = slidingWindow;
};
this.init = function() {
var generator = KhanUtil.PiecewiseFunctionGenerator;
// SET AXIS LIMS
this._setAxisLims();
// MAKE BASE GRAPH
this.graph = generator.generate({
numSegments: this.nIntervals,
breakIndex: this.breakIndex,
INTERVAL_WIDTH: this.INTERVAL_WIDTH,
YLIMS: this.YLIMS
});
// MAKE PROBLEM
var n = this.nProblemIntervals;
var offset = this.offset;
var start = this.chooseProblemStart(this.graph.length(), n, offset);
if (this.noSolution) {
this.problem = this.generateBogusProblem(this.graph, start, n, offset);
// if a valid bogus problem could not be generated for this
// initial graph, just start all over again
if (this.problem === null) {
this.init();
return;
}
} else {
this.problem = this.generateProblem(this.graph, start, n, offset);
}
// FIND MATCHES
var matches = this.graph.matches(this.problem, true);
this.problemRanges = this.matchesToProblemRanges(matches, n, offset);
};
// TODO(stephanie) LATER: refactor this and initAutoscaledGraph in
// derivative intuition
var initRectAutoscaledGraph = function(range, options) {
var graph = KhanUtil.currentGraph;
var xlims = range[0];
var ylims = range[1];
var xrange = xlims[1] - xlims[0];
var yrange = ylims[1] - ylims[0];
var xpixels = 480;
options = $.extend({
tickOpacity: 0.6,
labelOpacity: 0.6,
xpixels: xpixels,
ypixels: xpixels * yrange / xrange,
xdivisions: xrange,
ydivisions: yrange,
labels: true,
unityLabels: true,
range: (typeof range === "undefined" ? [[-10, 10], [-10, 10]] : range)
}, options);
options.scale = [options.xpixels / xrange,
options.ypixels / yrange];
options.gridStep = [xrange / options.xdivisions,
yrange / options.ydivisions];
// Attach the resulting metrics to the graph for later reference
graph.xpixels = options.xpixels;
graph.ypixels = options.ypixels;
graph.range = options.range;
graph.scale = options.scale;
graph.graphInit(options);
};
this.render = function(options) {
// PLOT BASE GRAPH AND PROBLEM GRAPH
initRectAutoscaledGraph(this.GRAPH_LIMS, {});
// store current graph for use by resetCurrentGraph()
this.graphie = KhanUtil.currentGraph;
var windowColor = this.derivColor;
if (this.moveDerivative) {
this.graph.plot({
color: this.fnColor,
graphie: this.graphie
});
this.problem.plot({
color: this.derivColor,
plotDerivative: true,
omitEnds: false,
graphie: this.graphie
});
} else {
this.graph.plot({
color: this.derivColor,
plotDerivative: true,
graphie: this.graphie
});
this.problem.plot({
color: this.fnColor,
omitEnds: true,
graphie: this.graphie
});
windowColor = this.fnColor;
}
// CREATE SLIDING WINDOW
var checkboxIdentifier = ".sol.no-solution :checkbox";
this.initSlidingWindow({
problem: this.problem,
color: windowColor,
onHide: function() {
$(checkboxIdentifier).attr("checked", true);
},
onShow: function() {
$(checkboxIdentifier).attr("checked", false);
}
});
// when user clicks "no solution", hide the sliding window
this.bindNoSolutionHide(checkboxIdentifier);
return this.slidingWindow;
};
// after displaying hint, reset KhanUtil.currentGraph to graphie
// element containing the problem graph to solve bug whereby the
// movable window is less responsive after hint graphs are added to
// the page
this.resetCurrentGraph = function() {
KhanUtil.currentGraph = this.graphie;
};
this.hints = function() {
var hints = [];
this.hintproblems = [];
var moveDeriv = this.moveDerivative;
var self = this;
_.each(this.problem.fnArray, function(fn, i) {
var nth = i > 0 ? "next" : "first";
fn = fn.derivative();
var nCoefs = fn.coefs.length;
var inc;
if (nCoefs === 1) {
if (fn.coefs[0] === 0) {
inc = "zero";
} else if (fn.coefs[0] > 0) {
inc = "constant and positive";
} else {
inc = "constant and negative";
}
} else if (nCoefs === 2) {
if (fn.coefs[1] > 0) {
inc = "increasing";