-
Notifications
You must be signed in to change notification settings - Fork 153
/
functions.ts
339 lines (278 loc) · 10.5 KB
/
functions.ts
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
// =============================================================================
// Polygons and Polyhedra
// (c) Mathigon
// =============================================================================
import {tabulate} from '@mathigon/core';
import {clamp, toWord, Segment, Point, Angle, lerp, Line} from '@mathigon/fermat';
import {Browser, slide} from '@mathigon/boost';
import {GeoElement, Geopad, GeoPoint, Slider, Step} from '../shared/types';
import {Solid} from '../shared/components/solid';
import {Graphics3D} from '../shared/components/webgl';
import {Anibutton} from './components/anibutton';
import {PolyhedronData, PolyhedronDataItem} from './components/polyhedron-data';
import {Tessellation} from './components/tessellation';
import '../shared/components/solid';
import './components/tessellation';
import './components/polyhedron';
import './components/folding';
import './components/anibutton';
export function angles($step: Step) {
const totals = [360, 540];
const $buttons = $step.$$('x-anibutton') as Anibutton[];
let overlap = [false, false];
for (const [i, $b] of $buttons.entries()) {
$step.model.watch(() => $b.setAttr('text', '???'));
$b.on('click', () => {
if (overlap[i]) return $step.addHint('no-overlap', {force: true});
$b.play();
$b.setAttr('text', totals[i] + '°');
$step.score('angle-' + i);
});
}
$step.model.watch(s => {
const a1 = new Angle(s.b, s.a, s.d).deg;
const a2 = new Angle(s.c, s.b, s.a).deg;
const a3 = new Angle(s.d, s.c, s.b).deg;
const a4 = new Angle(s.a, s.d, s.c).deg;
overlap[0] = a1 + a2 + a3 + a4 > 361;
const b1 = new Angle(s.f, s.e, s.i).deg;
const b2 = new Angle(s.g, s.f, s.e).deg;
const b3 = new Angle(s.h, s.g, s.f).deg;
const b4 = new Angle(s.i, s.h, s.g).deg;
const b5 = new Angle(s.e, s.i, s.h).deg;
overlap[1] = b1 + b2 + b3 + b4 + b5 >= 541;
if (overlap[0] || overlap[1]) $step.addHint('no-overlap');
});
$buttons[0].one('click', () => $step.addHint('angles-repeat'));
}
export function regularArea($step: Step) {
const model = $step.model;
model.assign({toWord});
model.set('regular', (c: Point, r: number, n: number) => {
const points = tabulate(i => {
const a = Math.PI * (2 * i / n + 1 / 2 - 1 / n);
return model.point(c.x + r * Math.cos(a), c.y + r * Math.sin(a));
}, n);
return model.polygon(...points);
});
}
// -----------------------------------------------------------------------------
function checkPathMatches(p: GeoElement<Line>, options: Line[]) {
for (let i = 0; i < options.length; ++i) {
if (p.val!.equals(options[i])) return i;
}
p.remove();
return -1;
}
export function midsegments($step: Step) {
const $geopad = $step.$('x-geopad') as Geopad;
$geopad.setActiveTool('point');
let points: GeoPoint[] = [];
let orientation: number;
$geopad.on('add:point', function (p) {
if (points.length === 4) return p.remove();
points.push(p);
if (points.length === 4) $step.score('points');
});
$geopad.on('add:path', function (p) {
const match = checkPathMatches(p, [
new Segment(points[0].val!, points[2].val!),
new Segment(points[1].val!, points[3].val!)
]);
if (match >= 0) {
orientation = match + 1;
p.$el.setAttr('target', 'parallel');
$step.score('diagonal');
} else {
$step.addHint('not-a-diagonal');
}
});
$step.onScore('points', () => {
if (points.length < 4) points = [
$geopad.drawMovablePoint(new Point(50, 40)),
$geopad.drawMovablePoint(new Point(70, 250)),
$geopad.drawMovablePoint(new Point(220, 240)),
$geopad.drawMovablePoint(new Point(260, 90))
];
$geopad.setActiveTool('move');
const [a, b, c, d] = points.map(p => p.val);
if (Segment.intersect(new Segment(a!, b!), new Segment(c!, d!))) {
points = [points[0], points[2], points[1], points[3]];
} else if (Segment.intersect(new Segment(a!, d!), new Segment(b!, c!))) {
points = [points[0], points[1], points[3], points[2]];
}
const pointStr = points.map(p => p.name).join(',');
$geopad.drawPath(`polygon(${pointStr})`, {name: 'quad', animated: 1000});
});
$step.onScore('blank-0 points', () => {
for (let i = 0; i < 4; ++i) {
$geopad.drawPoint(`quad.edges[${i}].midpoint`, {name: `m${i}`, classes: 'red'});
}
$geopad.drawPath(`polygon(m0,m1,m2,m3)`, {classes: 'red', name: 'p0', animated: 1000});
});
$step.onScore('blank-1 points', () => {
$geopad.setActiveTool('line');
$geopad.showGesture(points[0].name, points[2].name);
});
$step.onScore('diagonal', () => {
const [a, b, c, d] = points.map(p => p.name);
$geopad.setActiveTool('move');
if (!orientation) {
// TODO add diagonal
}
const o = (orientation === 1);
$geopad.drawPath(`segment(m0,m1)`, {classes: 'transparent red', target: o ? 'midsegment parallel' : 'other'});
$geopad.drawPath(`segment(m2,m3)`, {classes: 'transparent red', target: o ? 'midsegment parallel' : 'other'});
$geopad.drawPath(`segment(m1,m2)`, {classes: 'transparent red', target: !o ? 'midsegment parallel' : 'other'});
$geopad.drawPath(`segment(m0,m3)`, {classes: 'transparent red', target: !o ? 'midsegment parallel' : 'other'});
$geopad.drawPath(`segment(${o ? b : a},${o ? d : c})`, {classes: 'transparent', target: 'other'});
$geopad.drawPath(`polygon(${b},${o ? c : d},${a})`, {classes: 'fill green transparent', target: 'triangle'});
$geopad.drawPath(`polygon(${c},${d},${o ? a : b})`, {classes: 'fill yellow transparent', target: 'triangle'});
});
}
export function parallelogramsProof($step: Step) {
const $geo1 = $step.$$('x-geopad')[0] as Geopad;
const model = $step.model;
model.set('o', false);
$geo1.on('add:point', p => p.remove());
setTimeout(() => {
if (!$step.scores.has('diagonal')) {
$geo1.showGesture('a', 'c');
$geo1.setActiveTool('line');
}
}, 1000);
$geo1.on('add:path', function (p) {
const match = checkPathMatches(p, [
new Segment(model.b, model.d),
new Segment(model.a, model.c)
]);
if (match >= 0) {
p.$el.setAttr('target', 'diagonal');
if (match === 1) model.set('o', true);
$step.score('diagonal');
$geo1.setActiveTool('move');
} else {
$step.addHint('not-a-diagonal-1');
}
});
}
export function quadrilateralsArea($step: Step) {
const $geopads = $step.$$('x-geopad') as Geopad[];
$geopads[0].setActiveTool('rectangle');
$geopads[0].on('add:path', path => {
if (path.val.w * path.val.h === 48) {
// TODO shift to correct location
$step.addHint('correct');
$step.score('draw-1');
} else {
$step.addHint('incorrect');
path.remove();
}
});
$step.onScore('draw-1', () => $geopads[0].setActiveTool('move'));
$geopads[1].setActiveTool('rectangle');
$geopads[1].on('add:path', path => {
if (path.val.w * path.val.h === 48) {
// TODO shift to correct location
$step.addHint('correct');
$step.score('draw-2');
} else {
$step.addHint('incorrect');
path.remove();
}
});
$step.onScore('draw-2', () => $geopads[1].setActiveTool('move'));
}
// -----------------------------------------------------------------------------
export function tessellationDrawing($step: Step) {
let polygons = 0;
const $tessellation = $step.$('x-tessellation') as Tessellation;
$tessellation.on('add-shape', function () {
polygons += 1;
if (polygons >= 3) $step.score('shapes0');
if (polygons >= 5) $step.score('shapes1');
});
}
export function escher($step: Step) {
const $img = $step.$('.metamorph img')!;
let translate = 0;
let max = 3000;
slide($img, {
move(p, start, last) {
translate += last.x - p.x;
translate = clamp(translate, 0, max);
$img.translate(-translate, 0);
}
});
Browser.onResize(({width}) => {
max = 3000 - Math.min(760, width - 60);
});
}
export function penrose($step: Step) {
let $g = $step.$$('svg g');
$g[1].setAttr('opacity', 0);
$g[2].setAttr('opacity', 0);
const $slider = $step.$('x-slider') as Slider;
$slider.on('move', (n) => {
$g[0].setAttr('opacity', n < 50 ? 1 - n / 77 : 0.35);
$g[1].setAttr('opacity', n < 50 ? n / 50 : 1.5 - n / 100);
$g[2].setAttr('opacity', n < 50 ? 0 : n / 50 - 1);
});
}
// -----------------------------------------------------------------------------
export function polyhedra($step: Step) {
$step.addHint('move-polyhedra');
}
export function platonicOverview($step: Step) {
$step.addHint('use-euler');
}
function makePolyhedronGeo(data: PolyhedronDataItem) {
const vertices = data.vertex.map(v => new THREE.Vector3(v[0], v[1], v[2]));
const geometry = new THREE.Geometry();
geometry.vertices = vertices;
for (let f of data.face) {
for (let i = 1; i < f.length - 1; i++) {
geometry.faces.push(new THREE.Face3(f[0], f[i], f[i + 1]));
}
}
geometry.computeFaceNormals();
geometry.computeVertexNormals();
return geometry;
}
export function platonicDual($step: Step) {
const $solids = $step.$$('x-solid') as Solid[];
const $sliders = $step.$$('x-slider');
$solids[0].addMesh((scene: Graphics3D) => {
const octahedronGeo = makePolyhedronGeo(PolyhedronData.Octahedron);
const octahedron = $solids[0].addSolid(octahedronGeo, 0xfd8c00, 5, true);
const cubeGeo = makePolyhedronGeo(PolyhedronData.Cube);
const cube = $solids[0].addSolid(cubeGeo, 0x22ab24, 5, true);
cube.setRotationFromEuler(new THREE.Euler(0.71, 0.63, -0.97));
function update(n: number) {
cube.scale.setScalar(lerp(1.67, 0.95, n / 100));
octahedron.scale.setScalar(lerp(0.83, 1.43, n / 100));
scene.draw();
}
scene.camera.fov = 45;
$solids[0].object.rotateX(0.2).rotateY(-0.6);
$sliders[0].on('move', update);
update(0);
});
$solids[1].addMesh((scene) => {
const icosahedronGeo = makePolyhedronGeo(PolyhedronData.Icosahedron);
const icosahedron = $solids[1].addSolid(icosahedronGeo, 0xf7aff, 5, true);
icosahedron.setRotationFromEuler(new THREE.Euler(-0.47, 0, 0.3));
const dodecahedronGeo = makePolyhedronGeo(PolyhedronData.Dodecahedron);
const dodecahedron = $solids[1].addSolid(dodecahedronGeo, 0xcd0e66, 5,
true);
dodecahedron.setRotationFromEuler(new THREE.Euler(0.17, 0, 0.52));
function update(n: number) {
dodecahedron.scale.setScalar(lerp(1.88, 1.48, n / 100));
icosahedron.scale.setScalar(lerp(1.35, 1.7, n / 100));
scene.draw();
}
$solids[1].object.rotateX(0.2).rotateY(-0.25);
$sliders[1].on('move', update);
update(0);
});
}