-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.js
359 lines (312 loc) · 12.2 KB
/
Graph.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
'use strict'
// #1b1d1c B (27, 29, 28)
// #b0d68b g (176, 214, 139)
// #d56061 r (213, 96, 97)
// #ae89d5 p (174, 137, 213)
// #fda657 o (253, 166, 87)
// #63b1f0 b (99, 177 240)
// This class should do drawing in terms of pixels rather
// than real number values, i.e values should all be scaled to
// graph size.
// Maybe draw points and lines? could help abate missing line segments
class Graph {
constructor(points, n, minX, maxX, minY, maxY, maxN) {
// could be good to set constants for reused expressions,
// i.e. yAxis and xAxis and yScale / xScale (this.maxY/this.range)
// would be good to include padding in these constants as well
this.points = [];
this.unscaledPoints = points;
this.n = n;
this.maxN = maxN;
this.minX = minX;
this.maxX = maxX;
this.minY = minY;
this.maxY = maxY;
this.domain = this.maxX - this.minX;
this.range = this.maxY - this.minY;
this.yAxisRatio = this.maxY / this.range;
this.xAxisRatio = this.maxX / this.domain;
this.showGrid = true;
this.padding = 30;
this.buttons = [];
// this block is replicated in resize()
this.graphWidth = width - this.padding * 2;
this.graphHeight = height - this.padding * 2;
this.yScale = (this.graphHeight)/ this.range;
this.controlPanelHeight = height * .03;
this.dx = this.graphWidth / this.n;
this.scalePoints();
this.createButtons();
this.pixelConversion = this.points.length / this.graphWidth;
// if there is a seperate UI class, this.slider should be created there
var sliderLength = this.graphWidth / 5;
this.slider = new Slider(width - this.padding - sliderLength / 2, this.padding / 2, sliderLength);
this.sums = new Sums(this.n, this.unscaledPoints, this.domain);
}
setN(n) {
this.n = n;
this.sums.setN(n);
this.dx = this.graphWidth / this.n;
}
scalePoints() {
this.points = this.unscaledPoints.map(function (pt) {
return this.padding + this.graphHeight * this.yAxisRatio - (pt * this.yScale);
}.bind(this));
}
drawGrid(ticks) {
if (this.showGrid) {
for(var i = 0; i < ticks; i++) {
stroke(127, 127);
strokeWeight(0.5)
line(this.padding + (this.graphWidth / ticks) * i,
this.padding,
this.padding + (this.graphWidth / ticks) * i,
height - this.padding);
line(this.padding,
this.padding + this.graphHeight - i * (this.graphHeight / ticks),
width - this.padding,
this.padding + this.graphHeight - i * (this.graphHeight / ticks));
}
}
}
drawAxes(ticks) {
stroke(255);
noFill();
var xAxisVisible =
this.yAxisRatio >= 1 || this.yAxisRatio <= 0 ? false : true;
var yAxisVisible =
this.xAxisRatio >= 1 || this.xAxisRatio <= 0 ? false : true;
// Graph border
rect(this.padding, this.padding,
this.graphWidth, height - 2 * this.padding);
// X axis
if (xAxisVisible) {
line(this.padding, this.graphHeight * this.yAxisRatio + this.padding,
width - this.padding,
this.graphHeight * this.yAxisRatio + this.padding);
}
// Y axis
if (yAxisVisible) {
line(width - this.padding - (this.graphWidth * (this.maxX / this.domain)),
this.padding,
width - this.padding - (this.graphWidth * (this.maxX / this.domain)),
height - this.padding);
}
for(var i = 0; i < ticks; i++) {
// need option to show incrs of PI (?)
var fontSize = Math.round((this.graphWidth / ticks) / 5);
var maxSize = Math.round((0.015 * this.graphWidth));
fontSize = fontSize > maxSize ? maxSize : fontSize;
textSize(fontSize);
stroke(27, 29, 28);
strokeWeight(2);
fill(255, 200);
// X axis
if (!xAxisVisible) {
text(this.roundTo(this.minX + (i * (this.domain / ticks)), 3),
this.padding + (this.graphWidth / ticks) * i,
this.padding + this.graphHeight + fontSize);
} else {
text(this.roundTo(this.minX + (i * (this.domain / ticks)), 3),
this.padding + (this.graphWidth / ticks) * i,
this.padding + this.graphHeight * this.yAxisRatio + fontSize);
}
// Y axis
if (!yAxisVisible) {
text(this.roundTo(this.minY + (i * (this.range / ticks)), 3),
this.padding,
this.padding + this.graphHeight - i * (this.graphHeight / ticks));
} else {
text(this.roundTo(this.minY + (i * (this.range / ticks)), 3),
width - this.padding - this.graphWidth * (this.maxX / this.domain),
this.padding + this.graphHeight - i * (this.graphHeight / ticks));
}
}
}
drawCurve() {
fill(255);
strokeWeight(0.5);
var index = 0;
for (var i = 0; i < this.graphWidth - 1; i++) {
stroke(255);
index = Math.round(i * this.pixelConversion);
line(i + this.padding,
this.points[index],
i + this.padding + 1,
this.points[index + Math.round(this.pixelConversion)]);
}
}
roundTo(n, dec) {
var factor = pow(10, dec);
return Math.round(n * factor) / factor;
}
drawTrapezoid() {
fill(213, 96, 97, 100);
var scaledHeight = 0;
var index = 0;
var nextIndex = 0;
for (var i = 0; i < this.n; i++) {
index = Math.round(i * this.dx * this.pixelConversion);
nextIndex = Math.round((i + 1) * this.dx * this.pixelConversion) - 1;
quad(this.padding + (i + 1) * this.dx, this.points[nextIndex],
this.padding + i * this.dx, this.points[index],
this.padding + i * this.dx,
this.graphHeight * this.yAxisRatio + this.padding,
this.padding + (i + 1) * this.dx,
this.graphHeight * this.yAxisRatio + this.padding); // :c
}
}
drawLH() {
fill(174, 137, 213, 100);
var scaledHeight = 0;
var index = 0;
for (var i = 0; i < this.n; i++) {
index = Math.round(i * this.dx * this.pixelConversion);
scaledHeight = this.unscaledPoints[index] * this.yScale;
rect(this.padding + i * this.dx, this.points[index],
this.dx, scaledHeight);
}
}
drawMidpoint() {
// need a prettier color here...
fill(253, 166, 87, 100);
var scaledHeight = 0;
var index = 0;
for (var i = 0; i < this.n; i++) {
index = Math.round(i * this.dx * this.pixelConversion);
index += Math.round(this.dx * this.pixelConversion / 2);
scaledHeight = this.unscaledPoints[index] * this.yScale;
rect(this.padding + i * this.dx, this.points[index],
this.dx, scaledHeight);
}
}
drawRH() {
fill(99, 177, 240, 100);
var scaledHeight = 0;
var index = 0;
for (var i = 1; i < this.n + 1; i++) {
index = Math.round(i * this.dx * this.pixelConversion);
scaledHeight = this.unscaledPoints[index - 1] * this.yScale;
rect(this.padding + i * this.dx, this.points[index - 1],
-this.dx, scaledHeight);
}
}
createButtons() {
var spacing = this.graphWidth / 6;
var actual = new Button(this.padding, this.padding/ 1.5,
"actual: ", () => { return this.roundTo(this.sums.actual(), 5)});
var leftHand = new Button(this.padding + 1 * spacing, this.padding/ 1.5,
"LH: ", () => { return this.roundTo(this.sums.leftHand(), 9)}, true);
var rightHand = new Button(this.padding + 2 * spacing, this.padding/ 1.5,
"RH: ", () => { return this.roundTo(this.sums.rightHand(), 9)}, true);
var trapezoid = new Button(this.padding + 3 * spacing, this.padding/ 1.5,
"T: ", () => { return this.roundTo(this.sums.trapezoid(), 9)}, true);
var n = new Button(this.padding + 4 * spacing, this.padding/ 1.5,
"n: ", () => { return this.n});
this.buttons = [actual, leftHand, rightHand, trapezoid, n];
}
drawButtons() {
this.buttons.forEach(function (button) {
button.draw();
});
}
drawActiveSums() {
var self = this;
this.buttons.forEach(function (button) {
if (button.active) {
switch (button.text) {
case "LH: ":
self.drawLH();
break;
case "RH: ":
self.drawRH();
break;
case "T: ":
self.drawTrapezoid();
break;
default:
break;
}
}
});
}
// Accepts a signed array of values,
// -x, +x, -y, +y,
// errors when invalid bounds
setBounds(bounds) {
this.minX = bounds[0];
this.maxX = bounds[1];
this.minY = bounds[2];
this.maxY = bounds[3];
this.domain = this.maxX - this.minX;
this.range = this.maxY - this.minY;
this.yAxisRatio = this.maxY / this.range;
this.xAxisRatio = this.maxX / this.domain;
this.yScale = (this.graphHeight)/ this.range;
// can just set domain instead of creating new sums object (?)
// need new points for sure
this.sums = new Sums(this.n, this.unscaledPoints, this.domain);
this.scalePoints();
}
getButtons() {
return this.buttons;
}
getBounds() {
return this.minX + " " + this.maxX + " " + this.minY + " " + this.maxY;
}
setPoints(newPoints) {
this.unscaledPoints = newPoints;
this.scalePoints();
this.sums = new Sums(this.n, this.unscaledPoints, this.domain);
}
setMaxN(max) {
this.maxN = max;
if (this.maxN < this.n) {
this.setN(this.maxN);
}
this.resize();
}
// shoud be called drawUIElements
drawTopBar() {
// also redraws area above and below graph to cover any sums drawn
// outside of the graph view... hacky....
fill(27, 29, 28);
noStroke();
rect(0, 0, width, this.padding);
rect(0, height - this.padding, width, this.padding);
this.drawButtons();
this.slider.draw();
}
drawActiveFunction(functionString) {
noStroke();
rectMode(CENTER);
textAlign(CENTER);
textSize(this.padding / 2);
fill(27, 29, 28, 200);
rect(width / 2, height - this.padding / 2.1, textWidth("f(x) = " + functionString), this.padding, 5);
fill(213, 96, 97);
text("f(x) = " + functionString, width / 2, height - this.padding / 3);
textAlign(LEFT);
rectMode(CORNER);
}
resize() {
this.controlPanelHeight = height * .03;
this.graphWidth = width - this.padding * 2;
this.graphHeight = height - this.padding * 2;
this.yScale = (this.graphHeight)/ this.range;
this.dx = this.graphWidth / this.n;
this.scalePoints();
this.createButtons();
this.pixelConversion = this.points.length / this.graphWidth;
var sliderLength = this.graphWidth / 5;
var newX = width - this.padding - sliderLength / 2
this.slider.setLength(sliderLength);
this.slider.setX(newX);
this.slider.setY(this.padding / 2);
this.slider.handleX = newX - sliderLength / 2 +
(this.n / this.maxN) * sliderLength;
}
noSubRect(x, y, w, h) {
rect(Math.floor(x), Math.floor(y), Math.ceil(w), Math.ceil(h));
}
}