-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathcubictool.js
More file actions
380 lines (327 loc) · 12.7 KB
/
cubictool.js
File metadata and controls
380 lines (327 loc) · 12.7 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
/* global graphTool, JXG */
'use strict';
(() => {
if (graphTool && graphTool.cubicTool) return;
graphTool.cubicTool = {
Cubic(gt) {
return class extends gt.GraphObject {
static strId = 'cubic';
constructor(point1, point2, point3, point4, solid) {
for (const point of [point1, point2, point3, point4]) {
point.setAttribute(gt.definingPointAttributes());
if (!gt.isStatic) {
point.on('down', () => gt.onPointDown(point));
point.on('up', () => gt.onPointUp(point));
}
}
super(gt.graphObjectTypes.cubic.createCubic(point1, point2, point3, point4, solid, gt.color.curve));
this.definingPts.push(point1, point2, point3, point4);
this.focusPoint = point1;
}
stringify() {
return [
this.constructor.strId,
this.baseObj.getAttribute('dash') == 0 ? 'solid' : 'dashed',
...this.definingPts.map(
(point) =>
`(${gt.snapRound(point.X(), gt.snapSizeX)},${gt.snapRound(point.Y(), gt.snapSizeY)})`
)
].join(',');
}
fillCmp(point) {
return gt.sign(point[2] - this.baseObj.Y(point[1]));
}
static restore(string) {
let pointData = gt.pointRegexp.exec(string);
const points = [];
while (pointData) {
points.push(pointData.slice(1, 3));
pointData = gt.pointRegexp.exec(string);
}
if (points.length < 4) return false;
const point1 = gt.graphObjectTypes.quadratic.createPoint(
parseFloat(points[0][0]),
parseFloat(points[0][1])
);
const point2 = gt.graphObjectTypes.quadratic.createPoint(
parseFloat(points[1][0]),
parseFloat(points[1][1]),
[point1]
);
const point3 = gt.graphObjectTypes.quadratic.createPoint(
parseFloat(points[2][0]),
parseFloat(points[2][1]),
[point1, point2]
);
const point4 = gt.graphObjectTypes.quadratic.createPoint(
parseFloat(points[3][0]),
parseFloat(points[3][1]),
[point1, point2, point3]
);
return new this(point1, point2, point3, point4, /solid/.test(string));
}
static createCubic(point1, point2, point3, point4, solid, color) {
return gt.board.create(
'curve',
[
// x and y coordinate of point on curve
(x) => x,
(x) => {
const x1 = point1.X(),
x2 = point2.X(),
x3 = point3.X(),
x4 = point4.X(),
y1 = point1.Y(),
y2 = point2.Y(),
y3 = point3.Y(),
y4 = point4.Y();
return (
((x - x2) * (x - x3) * (x - x4) * y1) / ((x1 - x2) * (x1 - x3) * (x1 - x4)) +
((x - x1) * (x - x3) * (x - x4) * y2) / ((x2 - x1) * (x2 - x3) * (x2 - x4)) +
((x - x1) * (x - x2) * (x - x4) * y3) / ((x3 - x1) * (x3 - x2) * (x3 - x4)) +
((x - x1) * (x - x2) * (x - x3) * y4) / ((x4 - x1) * (x4 - x2) * (x4 - x3))
);
},
// domain minimum and maximum
() => gt.board.getBoundingBox()[0],
() => gt.board.getBoundingBox()[2]
],
{
strokeWidth: 2,
highlight: false,
strokeColor: color ? color : gt.color.underConstruction,
dash: solid ? 0 : 2,
tabindex: '',
aria: {
enabled: true,
label: (c) =>
(c.getAttribute('dash') == 0 ? 'solid' : 'dashed') +
` cubic passing through ${point1.X()}, ${point1.Y()}, and ` +
`${point2.X()}, ${point2.Y()}, and ${point3.X()}, ${point3.Y()}, ` +
`and ${point4.X()}, ${point4.Y()}`,
roledescription: 'cubic',
live: 'assertive',
atomic: true
}
}
);
}
};
},
CubicTool(gt) {
return class extends gt.GenericTool {
object = 'cubic';
supportsSolidDash = true;
useStandardActivation = true;
activationHelpText = 'Plot four points on the cubic.';
useStandardDeactivation = true;
constructionObjects = ['point1', 'point2', 'point3'];
constructor(container, iconName, tooltip) {
super(container, iconName ?? 'cubic', tooltip ?? '4-Point Cubic Tool: Graph a cubic function.');
}
phase1(coords) {
// Don't allow the point to be created off the board.
if (!gt.boardHasPoint(coords[1], coords[2])) return;
gt.board.off('up');
this.point1 = gt.graphObjectTypes.quadratic.createPoint(coords[1], coords[2]);
this.point1.setAttribute({ fixed: true, highlight: false });
// Get a new x coordinate that is to the right, unless that is off the board.
// In that case go left instead.
let newX = this.point1.X() + gt.snapSizeX;
if (newX > gt.board.getBoundingBox()[2]) newX = this.point1.X() - gt.snapSizeX;
this.updateHighlights(new JXG.Coords(JXG.COORDS_BY_USER, [newX, this.point1.Y()], gt.board));
this.helpText = 'Plot three more points on the cubic.';
gt.updateHelp();
gt.board.on('up', (e) => this.phase2(gt.getMouseCoords(e).usrCoords));
gt.board.update();
}
phase2(coords) {
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are on the same vertical line as the first point,
// then use the highlight point coordinates instead.
if (Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps)
coords = this.hlObjs.hl_point.coords.usrCoords;
gt.board.off('up');
this.point2 = gt.graphObjectTypes.quadratic.createPoint(coords[1], coords[2], [this.point1]);
this.point2.setAttribute({ fixed: true, highlight: false });
// Get a new x coordinate that is to the right, unless that is off the board.
// In that case go left instead.
let newX = this.point2.X() + gt.snapSizeX;
if (newX === this.point1.X()) newX += gt.snapSizeX;
if (newX > gt.board.getBoundingBox()[2]) newX = this.point2.X() - gt.snapSizeX;
if (newX === this.point1.X()) newX -= gt.snapSizeX;
this.updateHighlights(new JXG.Coords(JXG.COORDS_BY_USER, [newX, this.point2.Y()], gt.board));
this.helpText = 'Plot two more points on the cubic.';
gt.updateHelp();
gt.board.on('up', (e) => this.phase3(gt.getMouseCoords(e).usrCoords));
gt.board.update();
}
phase3(coords) {
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are on the same vertical line as the first point, or on the same
// vertical line as the second point, then use the highlight point coordinates instead.
if (
Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
Math.abs(this.point2.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps
)
coords = this.hlObjs.hl_point.coords.usrCoords;
gt.board.off('up');
this.point3 = gt.graphObjectTypes.quadratic.createPoint(coords[1], coords[2], [
this.point1,
this.point2
]);
this.point3.setAttribute({ fixed: true, highlight: false });
// Get a new x coordinate that is to the right, unless that is off the board.
// In that case go left instead.
let newX = this.point3.X() + gt.snapSizeX;
while ([this.point1, this.point2].some((other) => newX === other.X())) newX += gt.snapSizeX;
// If the computed new x coordinate is off the board, then we need to move the point back instead.
const boundingBox = gt.board.getBoundingBox();
if (newX < boundingBox[0] || newX > boundingBox[2]) {
newX = this.point3.X() - gt.snapSizeX;
while ([this.point1, this.point2].some((other) => newX === other.X())) newX -= gt.snapSizeX;
}
this.updateHighlights(new JXG.Coords(JXG.COORDS_BY_USER, [newX, this.point3.Y()], gt.board));
this.helpText = 'Plot one more point on the cubic.';
gt.updateHelp();
gt.board.on('up', (e) => this.phase4(gt.getMouseCoords(e).usrCoords));
gt.board.update();
}
phase4(coords) {
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are on the same vertical line as the first point, on the same vertical
// line as the second point, or on the same vertical line as the third point, then use the highlight
// point coordinates instead.
if (
Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
Math.abs(this.point2.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
Math.abs(this.point3.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps
)
coords = this.hlObjs.hl_point.coords.usrCoords;
gt.board.off('up');
const point4 = gt.graphObjectTypes.quadratic.createPoint(coords[1], coords[2], [
this.point1,
this.point2,
this.point3
]);
gt.selectedObj = new gt.graphObjectTypes[this.object](
this.point1,
this.point2,
this.point3,
point4,
gt.drawSolid
);
gt.selectedObj.focusPoint = point4;
gt.graphedObjs.push(gt.selectedObj);
delete this.point1;
delete this.point2;
delete this.point3;
this.finish();
}
handleKeyEvent(e) {
if (!this.hlObjs.hl_point || !gt.board.containerObj.contains(document.activeElement)) return;
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
e.stopPropagation();
if (this.point3) this.phase4(this.hlObjs.hl_point.coords.usrCoords);
else if (this.point2) this.phase3(this.hlObjs.hl_point.coords.usrCoords);
else if (this.point1) this.phase2(this.hlObjs.hl_point.coords.usrCoords);
else this.phase1(this.hlObjs.hl_point.coords.usrCoords);
}
}
updateHighlights(e) {
this.hlObjs.hl_line?.setAttribute({ dash: gt.drawSolid ? 0 : 2 });
this.hlObjs.hl_parabola?.setAttribute({ dash: gt.drawSolid ? 0 : 2 });
this.hlObjs.hl_cubic?.setAttribute({ dash: gt.drawSolid ? 0 : 2 });
this.hlObjs.hl_point?.rendNode.focus();
let coords;
if (e instanceof MouseEvent && e.type === 'pointermove') {
coords = gt.getMouseCoords(e);
this.hlObjs.hl_point?.setPosition(JXG.COORDS_BY_USER, [
coords.usrCoords[1],
coords.usrCoords[2]
]);
} else if (e instanceof KeyboardEvent && e.type === 'keydown') {
coords = this.hlObjs.hl_point.coords;
} else if (e instanceof JXG.Coords) {
coords = e;
this.hlObjs.hl_point?.setPosition(JXG.COORDS_BY_USER, [
coords.usrCoords[1],
coords.usrCoords[2]
]);
} else return false;
if (!this.hlObjs.hl_point) {
this.hlObjs.hl_point = gt.board.create('point', [coords.usrCoords[1], coords.usrCoords[2]], {
size: 2,
color: gt.color.underConstruction,
snapToGrid: true,
snapSizeX: gt.snapSizeX,
snapSizeY: gt.snapSizeY,
highlight: false,
withLabel: false,
tabindex: '',
aria: gt.pointAria
});
this.hlObjs.hl_point.rendNode.focus();
}
// Make sure the highlight point is not moved off the board or onto the same
// vertical line as any of the other points that have already been created.
if (e instanceof Event) {
const groupedPoints = [];
if (this.point1) groupedPoints.push(this.point1);
if (this.point2) groupedPoints.push(this.point2);
if (this.point3) groupedPoints.push(this.point3);
gt.graphObjectTypes.quadratic.adjustDragPosition(e, this.hlObjs.hl_point, groupedPoints);
}
if (this.point3 && !this.hlObjs.hl_cubic) {
// Delete the temporary highlight parabola if it exists.
if (this.hlObjs.hl_parabola) {
gt.board.removeObject(this.hlObjs.hl_parabola);
delete this.hlObjs.hl_parabola;
}
this.hlObjs.hl_cubic = gt.graphObjectTypes.cubic.createCubic(
this.point1,
this.point2,
this.point3,
this.hlObjs.hl_point,
gt.drawSolid
);
} else if (this.point2 && !this.point3 && !this.hlObjs.hl_parabola) {
// Delete the temporary highlight line if it exists.
if (this.hlObjs.hl_line) {
gt.board.removeObject(this.hlObjs.hl_line);
delete this.hlObjs.hl_line;
}
this.hlObjs.hl_parabola = gt.graphObjectTypes.quadratic.createQuadratic(
this.point1,
this.point2,
this.hlObjs.hl_point,
gt.drawSolid
);
} else if (this.point1 && !this.point2 && !this.hlObjs.hl_line) {
this.hlObjs.hl_line = gt.board.create('line', [this.point1, this.hlObjs.hl_point], {
fixed: true,
strokeColor: gt.color.underConstruction,
highlight: false,
dash: gt.drawSolid ? 0 : 2,
tabindex: '',
aria: {
enabled: true,
label: (l) =>
(l.getAttribute('dash') == 0 ? 'solid' : 'dashed') +
` line through ${l.point1.X()}, ${l.point1.Y()}, ` +
`and ${l.point2.X()}, ${l.point2.Y()}`,
roledescription: 'line',
live: 'assertive',
atomic: true
}
});
}
gt.setTextCoords(this.hlObjs.hl_point.X(), this.hlObjs.hl_point.Y());
gt.board.update();
return true;
}
};
}
};
})();