@motionscript/math 0.1.0-alpha.7 → 0.1.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -48
- package/README.md +390 -391
- package/dist/annotate/angle-mark.d.ts +6 -6
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.d.ts +9 -9
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.d.ts +9 -9
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/{chunk-ABYBW64J.js → chunk-IRZCG6PM.js} +2 -2
- package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
- package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
- package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
- package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
- package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/browser/tex.js +1 -1
- package/dist/browser/tex.js.map +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/brace.js.map +1 -1
- package/dist/kit/cache.js.map +1 -1
- package/dist/kit/component.d.ts.map +1 -1
- package/dist/kit/component.js.map +1 -1
- package/dist/kit/contour.js.map +1 -1
- package/dist/kit/coords.js.map +1 -1
- package/dist/kit/curve.js.map +1 -1
- package/dist/kit/equation.js.map +1 -1
- package/dist/kit/equation3d.js.map +1 -1
- package/dist/kit/expression.js.map +1 -1
- package/dist/kit/field-mark.d.ts.map +1 -1
- package/dist/kit/field-mark.js.map +1 -1
- package/dist/kit/field.js.map +1 -1
- package/dist/kit/follow.d.ts +2 -2
- package/dist/kit/follow.d.ts.map +1 -1
- package/dist/kit/follow.js.map +1 -1
- package/dist/kit/furniture.js.map +1 -1
- package/dist/kit/geometry.js.map +1 -1
- package/dist/kit/graph.js.map +1 -1
- package/dist/kit/mark.js.map +1 -1
- package/dist/kit/part-fills.js.map +1 -1
- package/dist/kit/place.js.map +1 -1
- package/dist/kit/sampler.js +1 -1
- package/dist/kit/sampler.js.map +1 -1
- package/dist/kit/scale.js.map +1 -1
- package/dist/kit/source.js.map +1 -1
- package/dist/kit/system.js.map +1 -1
- package/dist/kit/ticks.js.map +1 -1
- package/dist/kit/withdrawn.js.map +1 -1
- package/dist/marks/curve-area.js.map +1 -1
- package/dist/marks/drop-lines.d.ts +6 -6
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.d.ts +3 -3
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.d.ts +3 -3
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.d.ts +6 -6
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.d.ts +6 -6
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts +8 -8
- package/dist/plots/function-plot.d.ts.map +1 -1
- package/dist/plots/function-plot.js.map +1 -1
- package/dist/plots/points-plot.js.map +1 -1
- package/dist/plots/stream-lines.js.map +1 -1
- package/dist/plots/traced-path.js.map +1 -1
- package/dist/plots/vector-field.js.map +1 -1
- package/dist/structures/graph-layout.js.map +1 -1
- package/dist/structures/network-graph.js.map +1 -1
- package/dist/structures/sample-space.js.map +1 -1
- package/dist/systems/axes2d.d.ts.map +1 -1
- package/dist/systems/axes2d.js.map +1 -1
- package/dist/systems/number-line.js.map +1 -1
- package/dist/systems/number-plane.js.map +1 -1
- package/dist/systems/polar-plane.js.map +1 -1
- package/dist/tex/geometry.js.map +1 -1
- package/dist/tex/index.js.map +1 -1
- package/dist/tex/mathjax.js.map +1 -1
- package/dist/tex/matrix.d.ts +2 -2
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/node.d.ts +2 -2
- package/dist/tex/node.js.map +1 -1
- package/dist/tex/tween.js.map +1 -1
- package/dist/tex/typeset.d.ts +4 -4
- package/dist/tex/typeset.d.ts.map +1 -1
- package/dist/tex/typeset.js.map +1 -1
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.d.ts +27 -27
- package/dist/three/curve-plot3d.d.ts.map +1 -1
- package/dist/three/curve-plot3d.js +3 -2
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/entries.js.map +1 -1
- package/dist/three/label3d.d.ts.map +1 -1
- package/dist/three/label3d.js +3 -1
- package/dist/three/label3d.js.map +1 -1
- package/dist/three/plot-point3d.d.ts +27 -27
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.d.ts +36 -36
- package/dist/three/surface-plot3d.js +6 -1
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.d.ts +27 -27
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +5 -4
- package/registry.json +251 -251
- package/src/annotate/angle-mark.ts +137 -137
- package/src/annotate/curly-brace.ts +148 -148
- package/src/annotate/emphasis-mark.ts +118 -118
- package/src/engine.ts +86 -86
- package/src/index.ts +29 -29
- package/src/kit/brace.ts +37 -37
- package/src/kit/cache.ts +61 -61
- package/src/kit/component.ts +57 -57
- package/src/kit/contour.ts +94 -94
- package/src/kit/coords.ts +278 -278
- package/src/kit/curve.ts +47 -47
- package/src/kit/equation.ts +181 -181
- package/src/kit/equation3d.ts +137 -137
- package/src/kit/expression.ts +159 -159
- package/src/kit/field-mark.ts +171 -171
- package/src/kit/field.ts +50 -50
- package/src/kit/follow.ts +229 -229
- package/src/kit/furniture.ts +113 -113
- package/src/kit/geometry.ts +49 -49
- package/src/kit/graph.ts +116 -116
- package/src/kit/mark.ts +152 -152
- package/src/kit/part-fills.ts +61 -61
- package/src/kit/place.ts +77 -77
- package/src/kit/sampler.ts +281 -281
- package/src/kit/scale.ts +39 -39
- package/src/kit/source.ts +57 -57
- package/src/kit/system.ts +105 -105
- package/src/kit/ticks.ts +85 -85
- package/src/kit/withdrawn.ts +8 -8
- package/src/marks/curve-area.ts +89 -89
- package/src/marks/drop-lines.ts +71 -71
- package/src/marks/plot-point.ts +102 -102
- package/src/marks/riemann-sum.ts +99 -99
- package/src/marks/tangent-line.ts +119 -119
- package/src/marks/vector-arrow.ts +98 -98
- package/src/nodes.ts +99 -99
- package/src/plots/function-plot.ts +220 -220
- package/src/plots/points-plot.ts +103 -103
- package/src/plots/stream-lines.ts +160 -160
- package/src/plots/traced-path.ts +70 -70
- package/src/plots/vector-field.ts +107 -107
- package/src/structures/graph-layout.ts +145 -145
- package/src/structures/network-graph.ts +291 -291
- package/src/structures/sample-space.ts +210 -210
- package/src/systems/axes2d.ts +226 -226
- package/src/systems/number-line.ts +130 -130
- package/src/systems/number-plane.ts +232 -232
- package/src/systems/polar-plane.ts +177 -177
- package/src/tex/geometry.ts +703 -703
- package/src/tex/index.ts +15 -15
- package/src/tex/mathjax.ts +82 -82
- package/src/tex/matrix.ts +97 -97
- package/src/tex/node.ts +74 -74
- package/src/tex/tween.ts +308 -308
- package/src/tex/typeset.ts +461 -461
- package/src/three/axes3d.ts +267 -267
- package/src/three/curve-plot3d.ts +146 -144
- package/src/three/entries.ts +42 -42
- package/src/three/label3d.ts +52 -50
- package/src/three/plot-point3d.ts +63 -63
- package/src/three/space.ts +92 -92
- package/src/three/surface-plot3d.ts +318 -312
- package/src/three/vector-arrow3d.ts +88 -88
- package/dist/browser/chunks/chunk-ABYBW64J.js.map +0 -7
- package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
- package/dist/kit/placement.d.ts +0 -8
- package/dist/kit/placement.d.ts.map +0 -1
- package/dist/kit/placement.js +0 -8
- package/dist/kit/placement.js.map +0 -1
package/src/plots/traced-path.ts
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
import {
|
|
2
|
-
component2d,
|
|
3
|
-
Types,
|
|
4
|
-
type NodeOf,
|
|
5
|
-
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
6
|
-
type Node2D as _Node2D,
|
|
7
|
-
type Node2DProps as _Node2DProps,
|
|
8
|
-
type Shadow as _Shadow,
|
|
9
|
-
type ShadowResolved as _ShadowResolved,
|
|
10
|
-
type Stroke as _Stroke,
|
|
11
|
-
type StrokeResolved as _StrokeResolved,
|
|
12
|
-
} from "@motionscript/sdk";
|
|
13
|
-
import { omit } from "../kit/component";
|
|
14
|
-
import type { Interval } from "../kit/coords";
|
|
15
|
-
import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
16
|
-
import { PLOT_COMMANDS, PLOT_STATE, PLOT_STROKE, tracePlot, type FunctionPlotProps, type PlotValues } from "./function-plot";
|
|
17
|
-
|
|
18
|
-
export interface TracedPathProps extends FunctionPlotProps {
|
|
19
|
-
from?: number;
|
|
20
|
-
value?: number;
|
|
21
|
-
tail?: number;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const state = {
|
|
25
|
-
// It reveals itself by its value, so it trims no outline of its own.
|
|
26
|
-
...omit(PLOT_STATE, "start", "end"),
|
|
27
|
-
/** The value the trail starts at. */
|
|
28
|
-
from: Types.number(0, { step: 0.1 }),
|
|
29
|
-
/** The value the point is at now: where the trail ends. */
|
|
30
|
-
value: Types.number(0, { step: 0.1 }),
|
|
31
|
-
/** How much of the trail is kept, in the value's own units; zero keeps all of it. */
|
|
32
|
-
tail: Types.number(0, { min: 0, step: 0.1 }),
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
interface TrailValues extends PlotValues {
|
|
36
|
-
readonly from: number;
|
|
37
|
-
readonly value: number;
|
|
38
|
-
readonly tail: number;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* The trail a point leaves as a value changes: the curve of its equation from
|
|
43
|
-
* where the value began to where it is now. Bind `value` to a variable
|
|
44
|
-
* (`"=[t]"`) and the trail grows as the variable is driven, or drive it with
|
|
45
|
-
* `traceTo`; a `tail` keeps only its most recent stretch.
|
|
46
|
-
*/
|
|
47
|
-
export const TracedPath = traced(component2d({
|
|
48
|
-
key: "tracedPath",
|
|
49
|
-
state,
|
|
50
|
-
defaults: { stroke: PLOT_STROKE, width: "hug", height: "hug" },
|
|
51
|
-
commands: {
|
|
52
|
-
...omit(PLOT_COMMANDS, "progressTo", "flash"),
|
|
53
|
-
/** Carry the point on to another value, the trail following it. */
|
|
54
|
-
traceTo: { args: ["value"] },
|
|
55
|
-
},
|
|
56
|
-
measure: (_measurer, s) => inkSize(traceTrail(s, reading(s))),
|
|
57
|
-
render: (ctx, s) => drawMark(ctx, s, traceTrail(s, reading(s))),
|
|
58
|
-
}), traceTrail);
|
|
59
|
-
|
|
60
|
-
export type TracedPath = NodeOf<typeof TracedPath>;
|
|
61
|
-
|
|
62
|
-
function traceTrail(s: TrailValues, read: MarkReading) {
|
|
63
|
-
return tracePlot(s, read, span(s));
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** The span of the free variable the trail covers: from where the value began, or its tail, to where it is. */
|
|
67
|
-
function span({ from, value, tail }: TrailValues): Interval {
|
|
68
|
-
const kept = tail > 0 ? Math.min(Math.abs(value - from), tail) : Math.abs(value - from);
|
|
69
|
-
return value >= from ? [value - kept, value] : [value, value + kept];
|
|
70
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
6
|
+
type Node2D as _Node2D,
|
|
7
|
+
type Node2DProps as _Node2DProps,
|
|
8
|
+
type Shadow as _Shadow,
|
|
9
|
+
type ShadowResolved as _ShadowResolved,
|
|
10
|
+
type Stroke as _Stroke,
|
|
11
|
+
type StrokeResolved as _StrokeResolved,
|
|
12
|
+
} from "@motionscript/sdk";
|
|
13
|
+
import { omit } from "../kit/component";
|
|
14
|
+
import type { Interval } from "../kit/coords";
|
|
15
|
+
import { drawMark, inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
16
|
+
import { PLOT_COMMANDS, PLOT_STATE, PLOT_STROKE, tracePlot, type FunctionPlotProps, type PlotValues } from "./function-plot";
|
|
17
|
+
|
|
18
|
+
export interface TracedPathProps extends FunctionPlotProps {
|
|
19
|
+
from?: number;
|
|
20
|
+
value?: number;
|
|
21
|
+
tail?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const state = {
|
|
25
|
+
// It reveals itself by its value, so it trims no outline of its own.
|
|
26
|
+
...omit(PLOT_STATE, "start", "end"),
|
|
27
|
+
/** The value the trail starts at. */
|
|
28
|
+
from: Types.number(0, { step: 0.1 }),
|
|
29
|
+
/** The value the point is at now: where the trail ends. */
|
|
30
|
+
value: Types.number(0, { step: 0.1 }),
|
|
31
|
+
/** How much of the trail is kept, in the value's own units; zero keeps all of it. */
|
|
32
|
+
tail: Types.number(0, { min: 0, step: 0.1 }),
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
interface TrailValues extends PlotValues {
|
|
36
|
+
readonly from: number;
|
|
37
|
+
readonly value: number;
|
|
38
|
+
readonly tail: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The trail a point leaves as a value changes: the curve of its equation from
|
|
43
|
+
* where the value began to where it is now. Bind `value` to a variable
|
|
44
|
+
* (`"=[t]"`) and the trail grows as the variable is driven, or drive it with
|
|
45
|
+
* `traceTo`; a `tail` keeps only its most recent stretch.
|
|
46
|
+
*/
|
|
47
|
+
export const TracedPath = traced(component2d({
|
|
48
|
+
key: "tracedPath",
|
|
49
|
+
state,
|
|
50
|
+
defaults: { stroke: PLOT_STROKE, width: "hug", height: "hug" },
|
|
51
|
+
commands: {
|
|
52
|
+
...omit(PLOT_COMMANDS, "progressTo", "flash"),
|
|
53
|
+
/** Carry the point on to another value, the trail following it. */
|
|
54
|
+
traceTo: { args: ["value"] },
|
|
55
|
+
},
|
|
56
|
+
measure: (_measurer, s) => inkSize(traceTrail(s, reading(s))),
|
|
57
|
+
render: (ctx, s) => drawMark(ctx, s, traceTrail(s, reading(s))),
|
|
58
|
+
}), traceTrail);
|
|
59
|
+
|
|
60
|
+
export type TracedPath = NodeOf<typeof TracedPath>;
|
|
61
|
+
|
|
62
|
+
function traceTrail(s: TrailValues, read: MarkReading) {
|
|
63
|
+
return tracePlot(s, read, span(s));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The span of the free variable the trail covers: from where the value began, or its tail, to where it is. */
|
|
67
|
+
function span({ from, value, tail }: TrailValues): Interval {
|
|
68
|
+
const kept = tail > 0 ? Math.min(Math.abs(value - from), tail) : Math.abs(value - from);
|
|
69
|
+
return value >= from ? [value - kept, value] : [value, value + kept];
|
|
70
|
+
}
|
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
import {
|
|
2
|
-
component2d,
|
|
3
|
-
Types,
|
|
4
|
-
type NodeOf,
|
|
5
|
-
type Vector2,
|
|
6
|
-
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
7
|
-
type Fill as _Fill,
|
|
8
|
-
type FillResolved as _FillResolved,
|
|
9
|
-
type Node2D as _Node2D,
|
|
10
|
-
type Node2DProps as _Node2DProps,
|
|
11
|
-
type Shadow as _Shadow,
|
|
12
|
-
type ShadowResolved as _ShadowResolved,
|
|
13
|
-
type Stroke as _Stroke,
|
|
14
|
-
type StrokeResolved as _StrokeResolved,
|
|
15
|
-
} from "@motionscript/sdk";
|
|
16
|
-
import type { CoordinateMap } from "../kit/coords";
|
|
17
|
-
import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField, type FieldDrawing, type FieldProps, type FieldValues, type FieldSampler, type FieldStroke } from "../kit/field-mark";
|
|
18
|
-
import { PLOT_COLOR } from "../kit/furniture";
|
|
19
|
-
import { inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
20
|
-
|
|
21
|
-
export interface VectorFieldProps extends FieldProps {
|
|
22
|
-
spacing?: number;
|
|
23
|
-
arrowScale?: number;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Past this many arrows the spacing is widened, so a field over a huge range is still a field and not a wash. */
|
|
27
|
-
const MAX_ARROWS = 2400;
|
|
28
|
-
/** An arrow's head, as a share of its length and in pixels at most, and the half-angle it opens to. */
|
|
29
|
-
const HEAD_SHARE = 0.38;
|
|
30
|
-
const HEAD_MAX = 11;
|
|
31
|
-
const HEAD_ANGLE = (26 * Math.PI) / 180;
|
|
32
|
-
|
|
33
|
-
const state = {
|
|
34
|
-
...FIELD_STATE,
|
|
35
|
-
/** How far apart the arrows stand, in graph units. */
|
|
36
|
-
spacing: Types.number(1, { min: 0.01, step: 0.1 }),
|
|
37
|
-
/** How long an arrow is drawn for a vector of length one, in graph units. */
|
|
38
|
-
arrowScale: Types.number(0.4, { min: 0, step: 0.05 }),
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
interface ArrowsValues extends FieldValues {
|
|
42
|
-
readonly spacing: number;
|
|
43
|
-
readonly arrowScale: number;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const ARROWS: FieldDrawing<ArrowsValues> = {
|
|
47
|
-
own: (s) => `${s.spacing}|${s.arrowScale}`,
|
|
48
|
-
strokes: (s, map, field) => arrows(s, map, field),
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const traceArrows = (s: ArrowsValues, read: MarkReading) => traceField("vectorField", ARROWS, s, read);
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* A grid of arrows showing a vector field, two parts in x and y: `(-y, x)`.
|
|
55
|
-
* Each arrow is centred on its point and is as long as the field is strong
|
|
56
|
-
* there, up to the spacing.
|
|
57
|
-
*/
|
|
58
|
-
export const VectorField = traced(component2d({
|
|
59
|
-
key: "vectorField",
|
|
60
|
-
state,
|
|
61
|
-
defaults: { stroke: { weight: 2.5, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
|
|
62
|
-
commands: FIELD_COMMANDS,
|
|
63
|
-
measure: (_measurer, s) => inkSize(traceArrows(s, reading(s)).runs),
|
|
64
|
-
render: (ctx, s) => drawField(ctx, s, traceArrows(s, reading(s))),
|
|
65
|
-
}), (s: ArrowsValues, read: MarkReading) => traceArrows(s, read).runs);
|
|
66
|
-
|
|
67
|
-
export type VectorField = NodeOf<typeof VectorField>;
|
|
68
|
-
|
|
69
|
-
function arrows(s: ArrowsValues, map: CoordinateMap, field: FieldSampler): FieldStroke[] {
|
|
70
|
-
const [x0, x1] = map.xRange;
|
|
71
|
-
const [y0, y1] = map.yRange;
|
|
72
|
-
let spacing = Math.max(s.spacing, 1e-6);
|
|
73
|
-
const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
|
|
74
|
-
while (count() > MAX_ARROWS) spacing *= 2;
|
|
75
|
-
|
|
76
|
-
const drawn: FieldStroke[] = [];
|
|
77
|
-
const v: Vector2 = { x: 0, y: 0 };
|
|
78
|
-
const longest = spacing * 0.9;
|
|
79
|
-
for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
|
|
80
|
-
for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
|
|
81
|
-
const gx = i * spacing;
|
|
82
|
-
const gy = j * spacing;
|
|
83
|
-
if (!field(gx, gy, v)) continue;
|
|
84
|
-
const magnitude = Math.hypot(v.x, v.y);
|
|
85
|
-
const length = Math.min(magnitude * s.arrowScale, longest);
|
|
86
|
-
if (!(length > 0)) continue;
|
|
87
|
-
const hx = ((v.x / magnitude) * length) / 2;
|
|
88
|
-
const hy = ((v.y / magnitude) * length) / 2;
|
|
89
|
-
const tail = map.toPoint(gx - hx, gy - hy);
|
|
90
|
-
const head = map.toPoint(gx + hx, gy + hy);
|
|
91
|
-
drawn.push({ runs: arrow(tail, head), magnitude });
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return drawn;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** A shaft and the two barbs of its head, in pixels. */
|
|
98
|
-
function arrow(tail: Vector2, head: Vector2): Vector2[][] {
|
|
99
|
-
const dx = head.x - tail.x;
|
|
100
|
-
const dy = head.y - tail.y;
|
|
101
|
-
const length = Math.hypot(dx, dy);
|
|
102
|
-
if (!(length > 0)) return [];
|
|
103
|
-
const barb = Math.min(length * HEAD_SHARE, HEAD_MAX);
|
|
104
|
-
const back = Math.atan2(dy, dx) + Math.PI;
|
|
105
|
-
const at = (angle: number): Vector2 => ({ x: head.x + Math.cos(angle) * barb, y: head.y + Math.sin(angle) * barb });
|
|
106
|
-
return [[tail, head], [at(back - HEAD_ANGLE), head, at(back + HEAD_ANGLE)]];
|
|
107
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
component2d,
|
|
3
|
+
Types,
|
|
4
|
+
type NodeOf,
|
|
5
|
+
type Vector2,
|
|
6
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
7
|
+
type Fill as _Fill,
|
|
8
|
+
type FillResolved as _FillResolved,
|
|
9
|
+
type Node2D as _Node2D,
|
|
10
|
+
type Node2DProps as _Node2DProps,
|
|
11
|
+
type Shadow as _Shadow,
|
|
12
|
+
type ShadowResolved as _ShadowResolved,
|
|
13
|
+
type Stroke as _Stroke,
|
|
14
|
+
type StrokeResolved as _StrokeResolved,
|
|
15
|
+
} from "@motionscript/sdk";
|
|
16
|
+
import type { CoordinateMap } from "../kit/coords";
|
|
17
|
+
import { drawField, FIELD_COMMANDS, FIELD_STATE, traceField, type FieldDrawing, type FieldProps, type FieldValues, type FieldSampler, type FieldStroke } from "../kit/field-mark";
|
|
18
|
+
import { PLOT_COLOR } from "../kit/furniture";
|
|
19
|
+
import { inkSize, reading, traced, type MarkReading } from "../kit/mark";
|
|
20
|
+
|
|
21
|
+
export interface VectorFieldProps extends FieldProps {
|
|
22
|
+
spacing?: number;
|
|
23
|
+
arrowScale?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Past this many arrows the spacing is widened, so a field over a huge range is still a field and not a wash. */
|
|
27
|
+
const MAX_ARROWS = 2400;
|
|
28
|
+
/** An arrow's head, as a share of its length and in pixels at most, and the half-angle it opens to. */
|
|
29
|
+
const HEAD_SHARE = 0.38;
|
|
30
|
+
const HEAD_MAX = 11;
|
|
31
|
+
const HEAD_ANGLE = (26 * Math.PI) / 180;
|
|
32
|
+
|
|
33
|
+
const state = {
|
|
34
|
+
...FIELD_STATE,
|
|
35
|
+
/** How far apart the arrows stand, in graph units. */
|
|
36
|
+
spacing: Types.number(1, { min: 0.01, step: 0.1 }),
|
|
37
|
+
/** How long an arrow is drawn for a vector of length one, in graph units. */
|
|
38
|
+
arrowScale: Types.number(0.4, { min: 0, step: 0.05 }),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
interface ArrowsValues extends FieldValues {
|
|
42
|
+
readonly spacing: number;
|
|
43
|
+
readonly arrowScale: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const ARROWS: FieldDrawing<ArrowsValues> = {
|
|
47
|
+
own: (s) => `${s.spacing}|${s.arrowScale}`,
|
|
48
|
+
strokes: (s, map, field) => arrows(s, map, field),
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const traceArrows = (s: ArrowsValues, read: MarkReading) => traceField("vectorField", ARROWS, s, read);
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A grid of arrows showing a vector field, two parts in x and y: `(-y, x)`.
|
|
55
|
+
* Each arrow is centred on its point and is as long as the field is strong
|
|
56
|
+
* there, up to the spacing.
|
|
57
|
+
*/
|
|
58
|
+
export const VectorField = traced(component2d({
|
|
59
|
+
key: "vectorField",
|
|
60
|
+
state,
|
|
61
|
+
defaults: { stroke: { weight: 2.5, fill: PLOT_COLOR, align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
|
|
62
|
+
commands: FIELD_COMMANDS,
|
|
63
|
+
measure: (_measurer, s) => inkSize(traceArrows(s, reading(s)).runs),
|
|
64
|
+
render: (ctx, s) => drawField(ctx, s, traceArrows(s, reading(s))),
|
|
65
|
+
}), (s: ArrowsValues, read: MarkReading) => traceArrows(s, read).runs);
|
|
66
|
+
|
|
67
|
+
export type VectorField = NodeOf<typeof VectorField>;
|
|
68
|
+
|
|
69
|
+
function arrows(s: ArrowsValues, map: CoordinateMap, field: FieldSampler): FieldStroke[] {
|
|
70
|
+
const [x0, x1] = map.xRange;
|
|
71
|
+
const [y0, y1] = map.yRange;
|
|
72
|
+
let spacing = Math.max(s.spacing, 1e-6);
|
|
73
|
+
const count = () => (Math.floor((x1 - x0) / spacing) + 1) * (Math.floor((y1 - y0) / spacing) + 1);
|
|
74
|
+
while (count() > MAX_ARROWS) spacing *= 2;
|
|
75
|
+
|
|
76
|
+
const drawn: FieldStroke[] = [];
|
|
77
|
+
const v: Vector2 = { x: 0, y: 0 };
|
|
78
|
+
const longest = spacing * 0.9;
|
|
79
|
+
for (let i = Math.ceil(x0 / spacing - 1e-9); i * spacing <= x1 + 1e-9; i++) {
|
|
80
|
+
for (let j = Math.ceil(y0 / spacing - 1e-9); j * spacing <= y1 + 1e-9; j++) {
|
|
81
|
+
const gx = i * spacing;
|
|
82
|
+
const gy = j * spacing;
|
|
83
|
+
if (!field(gx, gy, v)) continue;
|
|
84
|
+
const magnitude = Math.hypot(v.x, v.y);
|
|
85
|
+
const length = Math.min(magnitude * s.arrowScale, longest);
|
|
86
|
+
if (!(length > 0)) continue;
|
|
87
|
+
const hx = ((v.x / magnitude) * length) / 2;
|
|
88
|
+
const hy = ((v.y / magnitude) * length) / 2;
|
|
89
|
+
const tail = map.toPoint(gx - hx, gy - hy);
|
|
90
|
+
const head = map.toPoint(gx + hx, gy + hy);
|
|
91
|
+
drawn.push({ runs: arrow(tail, head), magnitude });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return drawn;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A shaft and the two barbs of its head, in pixels. */
|
|
98
|
+
function arrow(tail: Vector2, head: Vector2): Vector2[][] {
|
|
99
|
+
const dx = head.x - tail.x;
|
|
100
|
+
const dy = head.y - tail.y;
|
|
101
|
+
const length = Math.hypot(dx, dy);
|
|
102
|
+
if (!(length > 0)) return [];
|
|
103
|
+
const barb = Math.min(length * HEAD_SHARE, HEAD_MAX);
|
|
104
|
+
const back = Math.atan2(dy, dx) + Math.PI;
|
|
105
|
+
const at = (angle: number): Vector2 => ({ x: head.x + Math.cos(angle) * barb, y: head.y + Math.sin(angle) * barb });
|
|
106
|
+
return [[tail, head], [at(back - HEAD_ANGLE), head, at(back + HEAD_ANGLE)]];
|
|
107
|
+
}
|
|
@@ -1,145 +1,145 @@
|
|
|
1
|
-
import type { Vector2 } from "@motionscript/sdk";
|
|
2
|
-
|
|
3
|
-
/** Where a graph's vertices are placed, inside a square from -1 to 1 each way, y up. */
|
|
4
|
-
|
|
5
|
-
export type GraphLayout = "circular" | "spring" | "tree" | "grid";
|
|
6
|
-
|
|
7
|
-
export const GRAPH_LAYOUTS: readonly GraphLayout[] = ["circular", "spring", "tree", "grid"];
|
|
8
|
-
|
|
9
|
-
export interface GraphEdge {
|
|
10
|
-
from: string;
|
|
11
|
-
to: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Each vertex's place under `layout`, by name. */
|
|
15
|
-
export function layOut(layout: GraphLayout, vertices: readonly string[], edges: readonly GraphEdge[], root: string): Map<string, Vector2> {
|
|
16
|
-
const n = vertices.length;
|
|
17
|
-
if (n === 0) return new Map();
|
|
18
|
-
if (n === 1) return new Map([[vertices[0], { x: 0, y: 0 }]]);
|
|
19
|
-
switch (layout) {
|
|
20
|
-
case "grid":
|
|
21
|
-
return grid(vertices);
|
|
22
|
-
case "tree":
|
|
23
|
-
return fill(tree(vertices, edges, root));
|
|
24
|
-
case "spring":
|
|
25
|
-
return fill(spring(vertices, edges));
|
|
26
|
-
default:
|
|
27
|
-
return circular(vertices);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function circular(vertices: readonly string[]): Map<string, Vector2> {
|
|
32
|
-
const n = vertices.length;
|
|
33
|
-
// The first at the top, the rest clockwise, as a clock is read.
|
|
34
|
-
return new Map(vertices.map((name, i) => {
|
|
35
|
-
const angle = Math.PI / 2 - (2 * Math.PI * i) / n;
|
|
36
|
-
return [name, { x: Math.cos(angle), y: Math.sin(angle) }];
|
|
37
|
-
}));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function grid(vertices: readonly string[]): Map<string, Vector2> {
|
|
41
|
-
const columns = Math.ceil(Math.sqrt(vertices.length));
|
|
42
|
-
const rows = Math.ceil(vertices.length / columns);
|
|
43
|
-
const at = (i: number, count: number) => (count === 1 ? 0 : -1 + (2 * i) / (count - 1));
|
|
44
|
-
return new Map(vertices.map((name, i) => [name, { x: at(i % columns, columns), y: -at(Math.floor(i / columns), rows) }]));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function neighbours(vertices: readonly string[], edges: readonly GraphEdge[]): Map<string, string[]> {
|
|
48
|
-
const near = new Map(vertices.map((name) => [name, [] as string[]]));
|
|
49
|
-
for (const { from, to } of edges) {
|
|
50
|
-
if (from === to || !near.has(from) || !near.has(to)) continue;
|
|
51
|
-
near.get(from)!.push(to);
|
|
52
|
-
near.get(to)!.push(from);
|
|
53
|
-
}
|
|
54
|
-
return near;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Levels down from `root`, and across by the leaves under each, every component its own tree beside the last. */
|
|
58
|
-
function tree(vertices: readonly string[], edges: readonly GraphEdge[], root: string): Map<string, Vector2> {
|
|
59
|
-
const near = neighbours(vertices, edges);
|
|
60
|
-
const placed = new Map<string, Vector2>();
|
|
61
|
-
let column = 0;
|
|
62
|
-
const visit = (name: string, depth: number, seen: Set<string>): number => {
|
|
63
|
-
seen.add(name);
|
|
64
|
-
const children = near.get(name)!.filter((next) => !seen.has(next));
|
|
65
|
-
for (const child of children) seen.add(child);
|
|
66
|
-
if (children.length === 0) {
|
|
67
|
-
placed.set(name, { x: column++, y: -depth });
|
|
68
|
-
return placed.get(name)!.x;
|
|
69
|
-
}
|
|
70
|
-
const xs = children.map((child) => visit(child, depth + 1, seen));
|
|
71
|
-
const x = (xs[0] + xs[xs.length - 1]) / 2;
|
|
72
|
-
placed.set(name, { x, y: -depth });
|
|
73
|
-
return x;
|
|
74
|
-
};
|
|
75
|
-
const seen = new Set<string>();
|
|
76
|
-
const first = near.has(root) ? root : vertices[0];
|
|
77
|
-
for (const start of [first, ...vertices]) if (!seen.has(start)) visit(start, 0, seen);
|
|
78
|
-
return placed;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Edges pull and every pair pushes, from the circle, for a fixed number of cooling steps: the same answer every time. */
|
|
82
|
-
function spring(vertices: readonly string[], edges: readonly GraphEdge[]): Map<string, Vector2> {
|
|
83
|
-
const n = vertices.length;
|
|
84
|
-
const index = new Map(vertices.map((name, i) => [name, i]));
|
|
85
|
-
const start = circular(vertices);
|
|
86
|
-
const x = vertices.map((name) => start.get(name)!.x);
|
|
87
|
-
const y = vertices.map((name) => start.get(name)!.y);
|
|
88
|
-
const links = edges.flatMap(({ from, to }) => {
|
|
89
|
-
const a = index.get(from);
|
|
90
|
-
const b = index.get(to);
|
|
91
|
-
return a === undefined || b === undefined || a === b ? [] : [[a, b] as const];
|
|
92
|
-
});
|
|
93
|
-
const k = Math.sqrt(4 / n);
|
|
94
|
-
const steps = 160;
|
|
95
|
-
for (let step = 0; step < steps; step++) {
|
|
96
|
-
const dx = new Float64Array(n);
|
|
97
|
-
const dy = new Float64Array(n);
|
|
98
|
-
for (let i = 0; i < n; i++) {
|
|
99
|
-
for (let j = i + 1; j < n; j++) {
|
|
100
|
-
let ex = x[i] - x[j];
|
|
101
|
-
let ey = y[i] - y[j];
|
|
102
|
-
let d = Math.hypot(ex, ey);
|
|
103
|
-
// Two on one spot are parted along a fixed, if arbitrary, line.
|
|
104
|
-
if (d < 1e-6) [ex, ey, d] = [1e-3 * (i - j), 1e-3, 1e-3];
|
|
105
|
-
const push = (k * k) / d;
|
|
106
|
-
dx[i] += (ex / d) * push;
|
|
107
|
-
dy[i] += (ey / d) * push;
|
|
108
|
-
dx[j] -= (ex / d) * push;
|
|
109
|
-
dy[j] -= (ey / d) * push;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
for (const [a, b] of links) {
|
|
113
|
-
const ex = x[a] - x[b];
|
|
114
|
-
const ey = y[a] - y[b];
|
|
115
|
-
const d = Math.max(Math.hypot(ex, ey), 1e-6);
|
|
116
|
-
const pull = (d * d) / k;
|
|
117
|
-
dx[a] -= (ex / d) * pull;
|
|
118
|
-
dy[a] -= (ey / d) * pull;
|
|
119
|
-
dx[b] += (ex / d) * pull;
|
|
120
|
-
dy[b] += (ey / d) * pull;
|
|
121
|
-
}
|
|
122
|
-
const limit = 0.12 * (1 - step / steps);
|
|
123
|
-
for (let i = 0; i < n; i++) {
|
|
124
|
-
const d = Math.hypot(dx[i], dy[i]);
|
|
125
|
-
if (d > 0) {
|
|
126
|
-
x[i] += (dx[i] / d) * Math.min(d, limit);
|
|
127
|
-
y[i] += (dy[i] / d) * Math.min(d, limit);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return new Map(vertices.map((name, i) => [name, { x: x[i], y: y[i] }]));
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** `placed` stretched to reach -1 and 1 on each axis it spreads along, and centred on the one it does not. */
|
|
135
|
-
function fill(placed: Map<string, Vector2>): Map<string, Vector2> {
|
|
136
|
-
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
137
|
-
for (const p of placed.values()) {
|
|
138
|
-
minX = Math.min(minX, p.x);
|
|
139
|
-
maxX = Math.max(maxX, p.x);
|
|
140
|
-
minY = Math.min(minY, p.y);
|
|
141
|
-
maxY = Math.max(maxY, p.y);
|
|
142
|
-
}
|
|
143
|
-
const scale = (v: number, lo: number, hi: number) => (hi - lo < 1e-9 ? 0 : -1 + (2 * (v - lo)) / (hi - lo));
|
|
144
|
-
return new Map([...placed].map(([name, p]) => [name, { x: scale(p.x, minX, maxX), y: scale(p.y, minY, maxY) }]));
|
|
145
|
-
}
|
|
1
|
+
import type { Vector2 } from "@motionscript/sdk";
|
|
2
|
+
|
|
3
|
+
/** Where a graph's vertices are placed, inside a square from -1 to 1 each way, y up. */
|
|
4
|
+
|
|
5
|
+
export type GraphLayout = "circular" | "spring" | "tree" | "grid";
|
|
6
|
+
|
|
7
|
+
export const GRAPH_LAYOUTS: readonly GraphLayout[] = ["circular", "spring", "tree", "grid"];
|
|
8
|
+
|
|
9
|
+
export interface GraphEdge {
|
|
10
|
+
from: string;
|
|
11
|
+
to: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Each vertex's place under `layout`, by name. */
|
|
15
|
+
export function layOut(layout: GraphLayout, vertices: readonly string[], edges: readonly GraphEdge[], root: string): Map<string, Vector2> {
|
|
16
|
+
const n = vertices.length;
|
|
17
|
+
if (n === 0) return new Map();
|
|
18
|
+
if (n === 1) return new Map([[vertices[0], { x: 0, y: 0 }]]);
|
|
19
|
+
switch (layout) {
|
|
20
|
+
case "grid":
|
|
21
|
+
return grid(vertices);
|
|
22
|
+
case "tree":
|
|
23
|
+
return fill(tree(vertices, edges, root));
|
|
24
|
+
case "spring":
|
|
25
|
+
return fill(spring(vertices, edges));
|
|
26
|
+
default:
|
|
27
|
+
return circular(vertices);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function circular(vertices: readonly string[]): Map<string, Vector2> {
|
|
32
|
+
const n = vertices.length;
|
|
33
|
+
// The first at the top, the rest clockwise, as a clock is read.
|
|
34
|
+
return new Map(vertices.map((name, i) => {
|
|
35
|
+
const angle = Math.PI / 2 - (2 * Math.PI * i) / n;
|
|
36
|
+
return [name, { x: Math.cos(angle), y: Math.sin(angle) }];
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function grid(vertices: readonly string[]): Map<string, Vector2> {
|
|
41
|
+
const columns = Math.ceil(Math.sqrt(vertices.length));
|
|
42
|
+
const rows = Math.ceil(vertices.length / columns);
|
|
43
|
+
const at = (i: number, count: number) => (count === 1 ? 0 : -1 + (2 * i) / (count - 1));
|
|
44
|
+
return new Map(vertices.map((name, i) => [name, { x: at(i % columns, columns), y: -at(Math.floor(i / columns), rows) }]));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function neighbours(vertices: readonly string[], edges: readonly GraphEdge[]): Map<string, string[]> {
|
|
48
|
+
const near = new Map(vertices.map((name) => [name, [] as string[]]));
|
|
49
|
+
for (const { from, to } of edges) {
|
|
50
|
+
if (from === to || !near.has(from) || !near.has(to)) continue;
|
|
51
|
+
near.get(from)!.push(to);
|
|
52
|
+
near.get(to)!.push(from);
|
|
53
|
+
}
|
|
54
|
+
return near;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Levels down from `root`, and across by the leaves under each, every component its own tree beside the last. */
|
|
58
|
+
function tree(vertices: readonly string[], edges: readonly GraphEdge[], root: string): Map<string, Vector2> {
|
|
59
|
+
const near = neighbours(vertices, edges);
|
|
60
|
+
const placed = new Map<string, Vector2>();
|
|
61
|
+
let column = 0;
|
|
62
|
+
const visit = (name: string, depth: number, seen: Set<string>): number => {
|
|
63
|
+
seen.add(name);
|
|
64
|
+
const children = near.get(name)!.filter((next) => !seen.has(next));
|
|
65
|
+
for (const child of children) seen.add(child);
|
|
66
|
+
if (children.length === 0) {
|
|
67
|
+
placed.set(name, { x: column++, y: -depth });
|
|
68
|
+
return placed.get(name)!.x;
|
|
69
|
+
}
|
|
70
|
+
const xs = children.map((child) => visit(child, depth + 1, seen));
|
|
71
|
+
const x = (xs[0] + xs[xs.length - 1]) / 2;
|
|
72
|
+
placed.set(name, { x, y: -depth });
|
|
73
|
+
return x;
|
|
74
|
+
};
|
|
75
|
+
const seen = new Set<string>();
|
|
76
|
+
const first = near.has(root) ? root : vertices[0];
|
|
77
|
+
for (const start of [first, ...vertices]) if (!seen.has(start)) visit(start, 0, seen);
|
|
78
|
+
return placed;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Edges pull and every pair pushes, from the circle, for a fixed number of cooling steps: the same answer every time. */
|
|
82
|
+
function spring(vertices: readonly string[], edges: readonly GraphEdge[]): Map<string, Vector2> {
|
|
83
|
+
const n = vertices.length;
|
|
84
|
+
const index = new Map(vertices.map((name, i) => [name, i]));
|
|
85
|
+
const start = circular(vertices);
|
|
86
|
+
const x = vertices.map((name) => start.get(name)!.x);
|
|
87
|
+
const y = vertices.map((name) => start.get(name)!.y);
|
|
88
|
+
const links = edges.flatMap(({ from, to }) => {
|
|
89
|
+
const a = index.get(from);
|
|
90
|
+
const b = index.get(to);
|
|
91
|
+
return a === undefined || b === undefined || a === b ? [] : [[a, b] as const];
|
|
92
|
+
});
|
|
93
|
+
const k = Math.sqrt(4 / n);
|
|
94
|
+
const steps = 160;
|
|
95
|
+
for (let step = 0; step < steps; step++) {
|
|
96
|
+
const dx = new Float64Array(n);
|
|
97
|
+
const dy = new Float64Array(n);
|
|
98
|
+
for (let i = 0; i < n; i++) {
|
|
99
|
+
for (let j = i + 1; j < n; j++) {
|
|
100
|
+
let ex = x[i] - x[j];
|
|
101
|
+
let ey = y[i] - y[j];
|
|
102
|
+
let d = Math.hypot(ex, ey);
|
|
103
|
+
// Two on one spot are parted along a fixed, if arbitrary, line.
|
|
104
|
+
if (d < 1e-6) [ex, ey, d] = [1e-3 * (i - j), 1e-3, 1e-3];
|
|
105
|
+
const push = (k * k) / d;
|
|
106
|
+
dx[i] += (ex / d) * push;
|
|
107
|
+
dy[i] += (ey / d) * push;
|
|
108
|
+
dx[j] -= (ex / d) * push;
|
|
109
|
+
dy[j] -= (ey / d) * push;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
for (const [a, b] of links) {
|
|
113
|
+
const ex = x[a] - x[b];
|
|
114
|
+
const ey = y[a] - y[b];
|
|
115
|
+
const d = Math.max(Math.hypot(ex, ey), 1e-6);
|
|
116
|
+
const pull = (d * d) / k;
|
|
117
|
+
dx[a] -= (ex / d) * pull;
|
|
118
|
+
dy[a] -= (ey / d) * pull;
|
|
119
|
+
dx[b] += (ex / d) * pull;
|
|
120
|
+
dy[b] += (ey / d) * pull;
|
|
121
|
+
}
|
|
122
|
+
const limit = 0.12 * (1 - step / steps);
|
|
123
|
+
for (let i = 0; i < n; i++) {
|
|
124
|
+
const d = Math.hypot(dx[i], dy[i]);
|
|
125
|
+
if (d > 0) {
|
|
126
|
+
x[i] += (dx[i] / d) * Math.min(d, limit);
|
|
127
|
+
y[i] += (dy[i] / d) * Math.min(d, limit);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return new Map(vertices.map((name, i) => [name, { x: x[i], y: y[i] }]));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** `placed` stretched to reach -1 and 1 on each axis it spreads along, and centred on the one it does not. */
|
|
135
|
+
function fill(placed: Map<string, Vector2>): Map<string, Vector2> {
|
|
136
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
137
|
+
for (const p of placed.values()) {
|
|
138
|
+
minX = Math.min(minX, p.x);
|
|
139
|
+
maxX = Math.max(maxX, p.x);
|
|
140
|
+
minY = Math.min(minY, p.y);
|
|
141
|
+
maxY = Math.max(maxY, p.y);
|
|
142
|
+
}
|
|
143
|
+
const scale = (v: number, lo: number, hi: number) => (hi - lo < 1e-9 ? 0 : -1 + (2 * (v - lo)) / (hi - lo));
|
|
144
|
+
return new Map([...placed].map(([name, p]) => [name, { x: scale(p.x, minX, maxX), y: scale(p.y, minY, maxY) }]));
|
|
145
|
+
}
|