-
Notifications
You must be signed in to change notification settings - Fork 1
/
global.js
305 lines (249 loc) · 11.5 KB
/
global.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
(function ($, root, undefined) {
var nulpunkter = [];
var functionName = "f";
var functionVariable = "x";
var endCaps = ["infty", -10, "infty", 10];
$(document).ready(function () {
handleNulpunkter();
$('#btnDraw').click(function () {
updateMonotoniLinje();
return false;
});
$('.interval').click(changeDRType);
$('.interval input').change(updateDR);
$("#functionName").change(updateFunctionName);
$("#functionVariable").change(updateFunctionName);
handleCanvasDragger();
});
function updateFunctionName() {
var fn = $("#functionName");
var fv = $("#functionVariable");
functionName = fn.val();
functionVariable = fv.val();
$("#dRdesc").html("Dm(" + functionName + ") =");
$("#forskriftDesc").html(functionName + "'(" + functionVariable + ") =");
$("#nulpunkterDesc").html(functionVariable + " =");
updateMonotoniLinje();
}
function handleCanvasDragger() {
var svg = $("#canvas");
var drag = $("#canvasDragger");
drag.css("left", svg.attr("width") - 5 + "px");
drag.css("top", svg.attr("height") - 5 + "px");
var dragging = false;
drag.mousedown(function () {
dragging = true;
});
$(document).mousemove(function (e) {
if (dragging) {
svg = $("#canvas");
var parentOffset = $("#canvasContainer").offset();
var mouseX = (e.pageX - parentOffset.left);
var mouseY = (e.pageY - parentOffset.top);
svg.attr("width", mouseX);
svg.attr("height", mouseY);
drag.css("left", mouseX - 5 + "px");
drag.css("top", mouseY - 5 + "px");
updateMonotoniLinje();
}
});
$(document).mouseup(function () {
dragging = false;
});
}
function handleNulpunkter() {
$("#nulpunkter").click(function () {
$("#nulpunkterInput").focus();
});
$("#nulpunkterInput").keydown(function (e) {
if (e.keyCode == 32) {
addNulpunkt();
updateMonotoniLinje();
} else if (e.keyCode == 8) {
if (nulpunkter.length > 0) {
nulpunkter.pop().remove();
}
updateMonotoniLinje();
}
});
}
// Updates the definition range for the function
function updateDR() {
var $dRl = $("#dR .interval.left");
if ($dRl.hasClass("infty")) {
endCaps[0] = "infty";
} else if ($dRl.hasClass("open")) {
endCaps[0] = "open";
} else {
endCaps[0] = "closed";
}
endCaps[1] = Number($dRl.find("input").val());
var $dRr = $("#dR .interval.right");
if ($dRr.hasClass("infty")) {
endCaps[2] = "infty";
} else if ($dRr.hasClass("open")) {
endCaps[2] = "open";
} else {
endCaps[2] = "closed";
}
endCaps[3] = Number($dRr.find("input").val());
updateMonotoniLinje();
}
function changeDRType (e) {
var $e = $(e.target);
if ($e.hasClass("infty")) {
$e.removeClass("infty");
$e.addClass("open");
} else if ($e.hasClass("open")) {
$e.removeClass("open");
} else {
$e.addClass("infty");
}
updateDR();
}
function addNulpunkt() {
var point = parseFloat($("#nulpunkterInput").val());
$("#nulpunkterInput").val("");
if (isNaN(point)) {
return; // Drop the point, it isn't interesting
}
var nulpunkt = $('<span class="label label-default">' + point + '</span>');
nulpunkt.data("value", point);
var inserted = false;
// Insert the zero-point back into the array
for (var i = 0; i < nulpunkter.length; i++) {
var val = nulpunkter[i].html();
if (val == point) {
return; // Don't add the point twice
} else if (val > point) {
nulpunkter[i].before(nulpunkt);
nulpunkter.splice(i, 0, nulpunkt);
inserted = true;
break;
}
}
if (! inserted) {
nulpunkter.push(nulpunkt);
$("#nulpunktsContainer").append(nulpunkt);
}
}
function updateMonotoniLinje () {
var ticks = [];
var x = 0;
var firstZeroPoint = nulpunkter[0].data("value");
// If the first zero point isn't the start of the definition range evaluate before the first zero point
if (endCaps[0] != "closed" || firstZeroPoint != endCaps[1]) {
x = endCaps[0] == "infty" ? firstZeroPoint - 10 : (firstZeroPoint + endCaps[1]) * 0.5;
ticks.push([x, evalExpressionAt(x)]);
}
for (var i = 0; i < nulpunkter.length; i++) {
var zeroPoint = nulpunkter[i].data("value");
ticks.push([zeroPoint, 0]);
// Evaluate between this zero point and the next
if (i < nulpunkter.length - 1) {
x = (zeroPoint + nulpunkter[i + 1].data("value")) * 0.5;
ticks.push([x, evalExpressionAt(x)]);
}
}
// if the last zero point isn't the end of the definition range evaluate after the last zero point
var lastZeroPoint = nulpunkter[nulpunkter.length -1].data("value");
if (endCaps[2] != "closed" || lastZeroPoint != endCaps[3]) {
x = endCaps[2] == "infty" ? lastZeroPoint + 10 : (lastZeroPoint + endCaps[3]) * 0.5;
ticks.push([x, evalExpressionAt(x)]);
}
drawMonotoniLinje(ticks);
}
function drawMonotoniLinje (ticks) {
var svg = $("#canvas");
var svgWidth = svg.attr("width");
svg.empty();
// Draw the line itself
var arrowStart = svgWidth - 14;
svg.append('<line x1="0" y1="50" x2="100%" y2="50" style="stroke:black; stroke-width:2" />');
svg.append('<polygon points="' + arrowStart + ',45 ' + arrowStart + ',55 ' + (svgWidth - 2) + ',50" style="fill:black;" />')
var totalSteps = ticks.length + 2;
if (endCaps[0] != "infty") totalSteps += 1;
if (endCaps[2] != "infty") totalSteps += 1;
var step = svgWidth/totalSteps;
var currentStep = 0;
// Add variable names
svg.append('<text class="italic" text-anchor="end" x="' + (step - 5) + '" y="40" fill="black">' + functionVariable + '</text>');
svg.append('<text class="italic" text-anchor="end" x="' + (step - 5) + '" y="70" fill="black">' + functionName + ' \'(' + functionVariable + ')</text>');
svg.append('<text class="italic" text-anchor="end" x="' + (step - 5) + '" y="100" fill="black">' + functionName + '(' + functionVariable + ')</text>');
if (endCaps[0] != "infty") {
currentStep = 3;
} else {
currentStep = 2;
}
// Add the first point
for (var i = 0; i < ticks.length; i++) {
var x = currentStep * step;
var tick = ticks[i];
if (tick[1] == 0) {
// This point is a zeropoint
// Draw the x value
svg.append('<text text-anchor="middle" x="' + x + '" y="40" fill="black">' + tick[0] + '</text>');
// Draw the actual tick
svg.append('<line x1="' + x + '" y1="45" x2="' + x + '" y2="55" style="stroke:black; stroke-width:2" />');
// Draw the derivative value
svg.append('<text text-anchor="middle" x="' + x + '" y="70" fill="black">0</text>');
// Draw whether it's a local minimum or maximum
var type = i > 0 ? (ticks[i - 1][1] > 0 ? '+' : '-') : "/";
type += i < ticks.length - 1 ? (ticks[i + 1][1] > 0 ? '+' : '-') : "/";
if (type == "/-" || type == "+-" || type == "-/" || type == "-+") {
svg.append('<text text-anchor="middle" x="' + x + '" y="100" fill="black">lok.</text>');
}
if (type == "/-" || type == "+-") {
svg.append('<text text-anchor="middle" x="' + x + '" y="113" fill="black">maks.</text>');
} else if (type == "-/" || type == "-+") {
svg.append('<text text-anchor="middle" x="' + x + '" y="113" fill="black">min.</text>');
}
} else {
// This point is not a zeropoint
svg.append('<text text-anchor="middle" x="' + x + '" y="70" fill="black">' + (tick[1] > 0 ? "+" : "-") + '</text>');
svg.append('<text text-anchor="middle" x="' + x + '" y="100" fill="black">' + (tick[1] > 0 ? "↗" : "↘") + '</text>');
}
// Increase the step, so that the next tick gets moved accordingly
currentStep++;
}
// Draw the definition range
drawDR(svg, step, svgWidth);
svg.DOMRefresh();
}
function drawDR (svg, step, svgWidth) {
var $dRl = $($("#dR .interval")[0]);
if (!$dRl.hasClass("infty")) {
svg.append('<line x1="' + step * 2 + '" y1="45" x2="' + step * 2 + '" y2="55" style="stroke:black; stroke-width:2" />');
svg.append('<text text-anchor="middle" x="' + step * 2 + '" y="40" fill="black">' + $dRl.find("input").val() + '</text>');
svg.append('<rect x="' + step + '" y="50" width="' + step + '" height="100%" fill="url(#diagonalHatch)"/>');
if ($dRl.hasClass("open")) {
svg.append('<line x1="' + step * 2 + '" x2="' + step * 2 + '" y1="50" y2="100%" stroke="black" stroke-width="2" stroke-linecap="round" stroke-dasharray="1, 6"/>');
} else {
svg.append('<line x1="' + step * 2 + '" x2="' + step * 2 + '" y1="50" y2="100%" stroke="black" stroke-width="2"/>');
}
}
var $dRr = $($("#dR .interval")[1]);
if (!$dRr.hasClass("infty")) {
svg.append('<line x1="' + (svgWidth - step) + '" y1="45" x2="' + (svgWidth - step) + '" y2="55" style="stroke:black; stroke-width:2" />');
svg.append('<text text-anchor="middle" x="' + (svgWidth - step) + '" y="40" fill="black">' + $dRr.find("input").val() + '</text>');
svg.append('<rect x="' + (svgWidth - step) + '" y="50" width="' + step + '" height="100%" fill="url(#diagonalHatch)"/>');
if ($dRr.hasClass("open")) {
svg.append('<line x1="' + (svgWidth - step) + '" x2="' + (svgWidth - step) + '" y1="50" y2="100%" stroke="black" stroke-width="2" stroke-linecap="round" stroke-dasharray="1, 6"/>');
} else {
svg.append('<line x1="' + (svgWidth - step) + '" x2="' + (svgWidth - step) + '" y1="50" y2="100%" stroke="black" stroke-width="2"/>');
}
}
svg.append('<pattern id="diagonalHatch" width="10" height="10" patternTransform="rotate(45 0 0)" patternUnits="userSpaceOnUse"><line x1="0" y1="0" x2="0" y2="10" style="stroke:black; stroke-width:1" /></pattern>')
}
function evalExpressionAt(x) {
var variable = {};
variable[functionVariable] = x;
return math.eval($('#forskrift').val(), variable);
}
$.fn.xml = function() {
return (new XMLSerializer()).serializeToString(this[0]);
};
$.fn.DOMRefresh = function() {
return $($(this.xml()).replaceAll(this));
};
})(jQuery, this);