@thi.ng/geom 6.0.6 → 6.0.8
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 +1 -1
- package/README.md +1 -1
- package/aabb.js +31 -32
- package/api/aabb.js +34 -29
- package/api/apc.js +15 -13
- package/api/arc.js +82 -66
- package/api/bpatch.js +79 -96
- package/api/circle.js +21 -21
- package/api/cubic.js +28 -25
- package/api/ellipse.js +26 -21
- package/api/group.js +45 -47
- package/api/line.js +26 -27
- package/api/path.js +62 -58
- package/api/plane.js +21 -21
- package/api/points.js +30 -26
- package/api/polygon.js +19 -16
- package/api/polyline.js +25 -22
- package/api/quad.js +20 -17
- package/api/quad3.js +20 -17
- package/api/quadratic.js +28 -25
- package/api/ray.js +30 -26
- package/api/rect.js +34 -29
- package/api/sphere.js +21 -21
- package/api/text.js +21 -28
- package/api/triangle.js +20 -17
- package/apply-transforms.js +28 -48
- package/arc-length.js +17 -30
- package/arc.js +17 -6
- package/area.js +19 -45
- package/as-cubic.js +38 -57
- package/as-path.js +4 -8
- package/as-polygon.js +12 -29
- package/as-polyline.js +18 -38
- package/as-svg.js +32 -63
- package/bounds.js +46 -48
- package/bpatch.js +22 -14
- package/center.js +22 -17
- package/centroid.js +14 -34
- package/circle.js +11 -6
- package/classify-point.js +20 -27
- package/clip-convex.js +40 -39
- package/closest-point.js +28 -34
- package/convex-hull.js +12 -27
- package/cubic.js +13 -5
- package/edges.js +31 -60
- package/ellipse.js +5 -2
- package/fit-into-bounds.js +78 -61
- package/flip.js +24 -41
- package/group.js +4 -1
- package/internal/args.js +18 -51
- package/internal/bounds.js +23 -38
- package/internal/collate.js +33 -17
- package/internal/copy.js +12 -23
- package/internal/dispatch.js +6 -4
- package/internal/edges.js +4 -1
- package/internal/pclike.js +8 -4
- package/internal/points-as-shape.js +4 -3
- package/internal/rotate.js +6 -2
- package/internal/scale.js +8 -2
- package/internal/split.js +9 -6
- package/internal/transform.js +24 -12
- package/internal/translate.js +6 -2
- package/internal/vertices.js +15 -31
- package/intersects.js +15 -39
- package/line.js +11 -9
- package/map-point.js +10 -18
- package/offset.js +31 -29
- package/package.json +33 -31
- package/path-builder.js +152 -143
- package/path-from-svg.js +129 -131
- package/path.js +28 -25
- package/plane.js +10 -5
- package/point-at.js +12 -30
- package/point-inside.js +20 -25
- package/points.js +6 -2
- package/polygon.js +15 -47
- package/polyline.js +18 -22
- package/quad.js +23 -15
- package/quadratic.js +7 -3
- package/ray.js +4 -1
- package/rect.js +47 -46
- package/resample.js +12 -27
- package/rotate.js +30 -42
- package/scale.js +66 -64
- package/scatter.js +18 -30
- package/simplify.js +38 -55
- package/sphere.js +7 -3
- package/split-arclength.js +53 -108
- package/split-at.js +46 -26
- package/split-near.js +28 -22
- package/subdiv-curve.js +33 -72
- package/tangent-at.js +12 -24
- package/tessellate.js +22 -47
- package/text.js +4 -1
- package/transform-vertices.js +31 -55
- package/transform.js +31 -54
- package/translate.js +39 -46
- package/triangle.js +7 -3
- package/union.js +12 -15
- package/unmap-point.js +21 -25
- package/vertices.js +91 -113
- package/volume.js +10 -14
- package/warp-points.js +15 -25
- package/with-attribs.js +4 -11
package/api/quadratic.js
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
1
|
import { __copyShape } from "../internal/copy.js";
|
|
2
2
|
import { __ensureNumVerts } from "../internal/pclike.js";
|
|
3
3
|
import { APC } from "./apc.js";
|
|
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
|
-
|
|
4
|
+
class Quadratic extends APC {
|
|
5
|
+
constructor(points, attribs) {
|
|
6
|
+
super(points, attribs);
|
|
7
|
+
__ensureNumVerts(this.points.length, 3);
|
|
8
|
+
}
|
|
9
|
+
get type() {
|
|
10
|
+
return "quadratic";
|
|
11
|
+
}
|
|
12
|
+
copy() {
|
|
13
|
+
return __copyShape(Quadratic, this);
|
|
14
|
+
}
|
|
15
|
+
withAttribs(attribs) {
|
|
16
|
+
return new Quadratic(this.points, attribs);
|
|
17
|
+
}
|
|
18
|
+
toHiccup() {
|
|
19
|
+
return [
|
|
20
|
+
"path",
|
|
21
|
+
this.attribs,
|
|
22
|
+
[["M", this.points[0]], ...this.toHiccupPathSegments()]
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
toHiccupPathSegments() {
|
|
26
|
+
const pts = this.points;
|
|
27
|
+
return [["Q", pts[1], pts[2]]];
|
|
28
|
+
}
|
|
29
29
|
}
|
|
30
|
+
export {
|
|
31
|
+
Quadratic
|
|
32
|
+
};
|
package/api/ray.js
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
import { maddN2 } from "@thi.ng/vectors/maddn";
|
|
2
2
|
import { set } from "@thi.ng/vectors/set";
|
|
3
3
|
import { __copyAttribs } from "../internal/copy.js";
|
|
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
|
-
|
|
4
|
+
class Ray {
|
|
5
|
+
constructor(pos, dir, attribs) {
|
|
6
|
+
this.pos = pos;
|
|
7
|
+
this.dir = dir;
|
|
8
|
+
this.attribs = attribs;
|
|
9
|
+
}
|
|
10
|
+
get type() {
|
|
11
|
+
return "ray";
|
|
12
|
+
}
|
|
13
|
+
copy() {
|
|
14
|
+
return new Ray(
|
|
15
|
+
set([], this.pos),
|
|
16
|
+
set([], this.dir),
|
|
17
|
+
__copyAttribs(this)
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
withAttribs(attribs) {
|
|
21
|
+
return new Ray(this.pos, this.dir, attribs);
|
|
22
|
+
}
|
|
23
|
+
toHiccup() {
|
|
24
|
+
return [
|
|
25
|
+
"line",
|
|
26
|
+
this.attribs,
|
|
27
|
+
this.pos,
|
|
28
|
+
maddN2([], this.dir, 1e6, this.pos)
|
|
29
|
+
];
|
|
30
|
+
}
|
|
30
31
|
}
|
|
32
|
+
export {
|
|
33
|
+
Ray
|
|
34
|
+
};
|
package/api/rect.js
CHANGED
|
@@ -6,33 +6,38 @@ import { set2 } from "@thi.ng/vectors/set";
|
|
|
6
6
|
import { subN2 } from "@thi.ng/vectors/subn";
|
|
7
7
|
import { __asVec } from "../internal/args.js";
|
|
8
8
|
import { __copyAttribs } from "../internal/copy.js";
|
|
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
|
-
|
|
9
|
+
class Rect {
|
|
10
|
+
constructor(pos = [0, 0], size = 1, attribs) {
|
|
11
|
+
this.pos = pos;
|
|
12
|
+
this.attribs = attribs;
|
|
13
|
+
this.size = __asVec(size);
|
|
14
|
+
}
|
|
15
|
+
size;
|
|
16
|
+
get type() {
|
|
17
|
+
return "rect";
|
|
18
|
+
}
|
|
19
|
+
copy() {
|
|
20
|
+
return new Rect(
|
|
21
|
+
set2([], this.pos),
|
|
22
|
+
set2([], this.size),
|
|
23
|
+
__copyAttribs(this)
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
withAttribs(attribs) {
|
|
27
|
+
return new Rect(this.pos, this.size, attribs);
|
|
28
|
+
}
|
|
29
|
+
max() {
|
|
30
|
+
return add2([], this.pos, this.size);
|
|
31
|
+
}
|
|
32
|
+
offset(offset) {
|
|
33
|
+
subN2(null, this.pos, offset);
|
|
34
|
+
max2(null, addN2(null, this.size, offset * 2), ZERO2);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
toHiccup() {
|
|
38
|
+
return ["rect", this.attribs, this.pos, this.size[0], this.size[1]];
|
|
39
|
+
}
|
|
38
40
|
}
|
|
41
|
+
export {
|
|
42
|
+
Rect
|
|
43
|
+
};
|
package/api/sphere.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import { set3 } from "@thi.ng/vectors/set";
|
|
2
2
|
import { __copyAttribs } from "../internal/copy.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
toHiccup() {
|
|
22
|
-
return ["sphere", this.attribs, this.pos, this.r];
|
|
23
|
-
}
|
|
3
|
+
class Sphere {
|
|
4
|
+
constructor(pos = [0, 0, 0], r = 1, attribs) {
|
|
5
|
+
this.pos = pos;
|
|
6
|
+
this.r = r;
|
|
7
|
+
this.attribs = attribs;
|
|
8
|
+
}
|
|
9
|
+
get type() {
|
|
10
|
+
return "sphere";
|
|
11
|
+
}
|
|
12
|
+
copy() {
|
|
13
|
+
return new Sphere(set3([], this.pos), this.r, __copyAttribs(this));
|
|
14
|
+
}
|
|
15
|
+
withAttribs(attribs) {
|
|
16
|
+
return new Sphere(this.pos, this.r, attribs);
|
|
17
|
+
}
|
|
18
|
+
toHiccup() {
|
|
19
|
+
return ["sphere", this.attribs, this.pos, this.r];
|
|
20
|
+
}
|
|
24
21
|
}
|
|
22
|
+
export {
|
|
23
|
+
Sphere
|
|
24
|
+
};
|
package/api/text.js
CHANGED
|
@@ -1,31 +1,24 @@
|
|
|
1
1
|
import { set } from "@thi.ng/vectors/set";
|
|
2
2
|
import { __copyAttribs } from "../internal/copy.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
copy() {
|
|
23
|
-
return new Text(set([], this.pos), this.body, __copyAttribs(this));
|
|
24
|
-
}
|
|
25
|
-
withAttribs(attribs) {
|
|
26
|
-
return new Text(this.pos, this.body, attribs);
|
|
27
|
-
}
|
|
28
|
-
toHiccup() {
|
|
29
|
-
return ["text", this.attribs, this.pos, this.body];
|
|
30
|
-
}
|
|
3
|
+
class Text {
|
|
4
|
+
constructor(pos, body, attribs) {
|
|
5
|
+
this.pos = pos;
|
|
6
|
+
this.body = body;
|
|
7
|
+
this.attribs = attribs;
|
|
8
|
+
}
|
|
9
|
+
get type() {
|
|
10
|
+
return "text";
|
|
11
|
+
}
|
|
12
|
+
copy() {
|
|
13
|
+
return new Text(set([], this.pos), this.body, __copyAttribs(this));
|
|
14
|
+
}
|
|
15
|
+
withAttribs(attribs) {
|
|
16
|
+
return new Text(this.pos, this.body, attribs);
|
|
17
|
+
}
|
|
18
|
+
toHiccup() {
|
|
19
|
+
return ["text", this.attribs, this.pos, this.body];
|
|
20
|
+
}
|
|
31
21
|
}
|
|
22
|
+
export {
|
|
23
|
+
Text
|
|
24
|
+
};
|
package/api/triangle.js
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
import { __copyShape } from "../internal/copy.js";
|
|
2
2
|
import { __ensureNumVerts } from "../internal/pclike.js";
|
|
3
3
|
import { APC } from "./apc.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
4
|
+
class Triangle extends APC {
|
|
5
|
+
constructor(points, attribs) {
|
|
6
|
+
super(points, attribs);
|
|
7
|
+
__ensureNumVerts(this.points.length, 3);
|
|
8
|
+
}
|
|
9
|
+
get type() {
|
|
10
|
+
return "tri";
|
|
11
|
+
}
|
|
12
|
+
copy() {
|
|
13
|
+
return __copyShape(Triangle, this);
|
|
14
|
+
}
|
|
15
|
+
withAttribs(attribs) {
|
|
16
|
+
return new Triangle(this.points, attribs);
|
|
17
|
+
}
|
|
18
|
+
toHiccup() {
|
|
19
|
+
return ["polygon", this.attribs, this.points];
|
|
20
|
+
}
|
|
21
21
|
}
|
|
22
|
+
export {
|
|
23
|
+
Triangle
|
|
24
|
+
};
|
package/apply-transforms.js
CHANGED
|
@@ -6,55 +6,35 @@ import { scale } from "./scale.js";
|
|
|
6
6
|
import { transform } from "./transform.js";
|
|
7
7
|
import { translate } from "./translate.js";
|
|
8
8
|
const TX_ATTRIBS = ["transform", "translate", "rotate", "scale"];
|
|
9
|
-
/** @internal */
|
|
10
9
|
const __apply = ($) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return $;
|
|
14
|
-
const { transform: tx, translate: t, rotate: r, scale: s } = attribs;
|
|
15
|
-
if (tx)
|
|
16
|
-
return transform($.withAttribs(withoutKeysObj(attribs, TX_ATTRIBS)), tx);
|
|
17
|
-
if (!(t || r || s))
|
|
18
|
-
return $;
|
|
19
|
-
$ = $.withAttribs(withoutKeysObj(attribs, TX_ATTRIBS));
|
|
20
|
-
if (r)
|
|
21
|
-
$ = rotate($, r);
|
|
22
|
-
if (s)
|
|
23
|
-
$ = scale($, s);
|
|
24
|
-
if (t)
|
|
25
|
-
$ = translate($, t);
|
|
10
|
+
let attribs = $.attribs;
|
|
11
|
+
if (!attribs)
|
|
26
12
|
return $;
|
|
13
|
+
const { transform: tx, translate: t, rotate: r, scale: s } = attribs;
|
|
14
|
+
if (tx)
|
|
15
|
+
return transform(
|
|
16
|
+
$.withAttribs(withoutKeysObj(attribs, TX_ATTRIBS)),
|
|
17
|
+
tx
|
|
18
|
+
);
|
|
19
|
+
if (!(t || r || s))
|
|
20
|
+
return $;
|
|
21
|
+
$ = $.withAttribs(withoutKeysObj(attribs, TX_ATTRIBS));
|
|
22
|
+
if (r)
|
|
23
|
+
$ = rotate($, r);
|
|
24
|
+
if (s)
|
|
25
|
+
$ = scale($, s);
|
|
26
|
+
if (t)
|
|
27
|
+
$ = translate($, t);
|
|
28
|
+
return $;
|
|
27
29
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
* @remarks
|
|
33
|
-
* The following attributes are considered:
|
|
34
|
-
*
|
|
35
|
-
* - transform: A 2x3 (for 2D) or 4x4 (for 3D) transformation matrix
|
|
36
|
-
* - translate: Translation/offset vector
|
|
37
|
-
* - scale: A scale factor (scalar or vector)
|
|
38
|
-
* - rotate: Rotation angle (in radians)
|
|
39
|
-
*
|
|
40
|
-
* If the `transform` attrib is given, the others will be ignored. If any of the
|
|
41
|
-
* other 3 attribs is provided, the order of application is: rotate, scale,
|
|
42
|
-
* translate. Any of these relevant attributes will be removed from the
|
|
43
|
-
* transformed shapes to ensure idempotent behavior.
|
|
44
|
-
*
|
|
45
|
-
* For (@link group} shapes, the children are processed in depth-first order
|
|
46
|
-
* with any transformations to the group itself applied last.
|
|
47
|
-
*
|
|
48
|
-
* Note: Where possible, this function delegates to {@link rotate},
|
|
49
|
-
* {@link scale}, {@link translate} to realize individual/partial transformation
|
|
50
|
-
* aspects to increase the likelihodd of retaining original shape types. E.g.
|
|
51
|
-
* uniformly scaling a circle with a scalar factor retains a circle, but scaling
|
|
52
|
-
* non-uniformly will convert it to an ellipse... Similarly, rotating a rect
|
|
53
|
-
* will convert it to a quad etc.
|
|
54
|
-
*
|
|
55
|
-
* @param shape
|
|
56
|
-
*/
|
|
57
|
-
export const applyTransforms = defmulti(__dispatch, {}, {
|
|
30
|
+
const applyTransforms = defmulti(
|
|
31
|
+
__dispatch,
|
|
32
|
+
{},
|
|
33
|
+
{
|
|
58
34
|
[DEFAULT]: __apply,
|
|
59
|
-
group: ($) => __apply($.copyTransformed((x) => applyTransforms(x)))
|
|
60
|
-
}
|
|
35
|
+
group: ($) => __apply($.copyTransformed((x) => applyTransforms(x)))
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
export {
|
|
39
|
+
applyTransforms
|
|
40
|
+
};
|
package/arc-length.js
CHANGED
|
@@ -3,40 +3,27 @@ import { perimeter } from "@thi.ng/geom-poly-utils/perimeter";
|
|
|
3
3
|
import { PI, TAU } from "@thi.ng/math/api";
|
|
4
4
|
import { dist } from "@thi.ng/vectors/dist";
|
|
5
5
|
import { __dispatch } from "./internal/dispatch.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* sum of results.
|
|
10
|
-
*
|
|
11
|
-
* Implemented for:
|
|
12
|
-
*
|
|
13
|
-
* - {@link Circle}
|
|
14
|
-
* - {@link Ellipse}
|
|
15
|
-
* - {@link Group} (total sum of child circumferences)
|
|
16
|
-
* - {@link Line}
|
|
17
|
-
* - {@link Polygon}
|
|
18
|
-
* - {@link Polyline}
|
|
19
|
-
* - {@link Quad}
|
|
20
|
-
* - {@link Rect}
|
|
21
|
-
* - {@link Triangle}
|
|
22
|
-
*
|
|
23
|
-
* @param shape
|
|
24
|
-
*/
|
|
25
|
-
export const arcLength = defmulti(__dispatch, {
|
|
6
|
+
const arcLength = defmulti(
|
|
7
|
+
__dispatch,
|
|
8
|
+
{
|
|
26
9
|
quad: "poly",
|
|
27
|
-
tri: "poly"
|
|
28
|
-
},
|
|
10
|
+
tri: "poly"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
29
13
|
circle: ($) => TAU * $.r,
|
|
30
|
-
ellipse: ({ r: [a, b] }) =>
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
14
|
+
ellipse: ({ r: [a, b] }) => (
|
|
15
|
+
// Ramanujan approximation
|
|
16
|
+
// https://www.mathsisfun.com/geometry/ellipse-perimeter.html
|
|
17
|
+
PI * (3 * (a + b) - Math.sqrt((3 * a + b) * (3 * b + a)))
|
|
18
|
+
),
|
|
34
19
|
group: ({ children }) => children.reduce((sum, $) => sum + arcLength($), 0),
|
|
35
20
|
line: ({ points }) => dist(points[0], points[1]),
|
|
36
21
|
poly: ({ points }) => perimeter(points, points.length, true),
|
|
37
22
|
polyline: ({ points }) => perimeter(points, points.length),
|
|
38
23
|
rect: ({ size: [w, h] }) => 2 * (w + h),
|
|
39
|
-
tri: ({ points }) => dist(points[0], points[1]) +
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
24
|
+
tri: ({ points }) => dist(points[0], points[1]) + dist(points[1], points[2]) + dist(points[2], points[0])
|
|
25
|
+
}
|
|
26
|
+
);
|
|
27
|
+
export {
|
|
28
|
+
arcLength
|
|
29
|
+
};
|
package/arc.js
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
import { isNumber } from "@thi.ng/checks/is-number";
|
|
2
2
|
import { fromEndPoints } from "@thi.ng/geom-arc/from-endpoints";
|
|
3
3
|
import { Arc } from "./api/arc.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
const arc = (pos, r, axis, start, end, xl = false, clockwise = false) => new Arc(pos, isNumber(r) ? [r, r] : r, axis, start, end, xl, clockwise);
|
|
5
|
+
const arcFrom2Points = (a, b, radii, axis = 0, xl = false, cw = false, attribs) => {
|
|
6
|
+
const res = fromEndPoints(a, b, radii, axis, xl, cw);
|
|
7
|
+
return res ? new Arc(
|
|
8
|
+
res.center,
|
|
9
|
+
res.r,
|
|
10
|
+
res.axis,
|
|
11
|
+
res.start,
|
|
12
|
+
res.end,
|
|
13
|
+
res.xl,
|
|
14
|
+
res.cw,
|
|
15
|
+
attribs
|
|
16
|
+
) : void 0;
|
|
17
|
+
};
|
|
18
|
+
export {
|
|
19
|
+
arc,
|
|
20
|
+
arcFrom2Points
|
|
10
21
|
};
|
package/area.js
CHANGED
|
@@ -4,59 +4,33 @@ import { PI } from "@thi.ng/math/api";
|
|
|
4
4
|
import { signedArea2 } from "@thi.ng/vectors/signed-area";
|
|
5
5
|
import { asPolygon } from "./as-polygon.js";
|
|
6
6
|
import { __dispatch } from "./internal/dispatch.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*
|
|
12
|
-
* @remarks
|
|
13
|
-
* In general, for polygons and triangles, the sign of the result can be used as
|
|
14
|
-
* indication of the shapes orientation (clockwise / counterclockwise).
|
|
15
|
-
*
|
|
16
|
-
* For curves, lines, point clouds and rays the function returns 0.
|
|
17
|
-
*
|
|
18
|
-
* Currently implemented for:
|
|
19
|
-
*
|
|
20
|
-
* - {@link AABB}
|
|
21
|
-
* - {@link Circle}
|
|
22
|
-
* - {@link Cubic}
|
|
23
|
-
* - {@link Ellipse}
|
|
24
|
-
* - {@link Group}
|
|
25
|
-
* - {@link Line}
|
|
26
|
-
* - {@link Path} (closed only & via poly conversion)
|
|
27
|
-
* - {@link Plane}
|
|
28
|
-
* - {@link Points}
|
|
29
|
-
* - {@link Polygon}
|
|
30
|
-
* - {@link Polyline}
|
|
31
|
-
* - {@link Quad}
|
|
32
|
-
* - {@link Quadratic}
|
|
33
|
-
* - {@link Ray}
|
|
34
|
-
* - {@link Rect}
|
|
35
|
-
* - {@link Sphere}
|
|
36
|
-
* - {@link Triangle}
|
|
37
|
-
*
|
|
38
|
-
* @param shape - shape to operate on
|
|
39
|
-
* @param signed - true, if signed area
|
|
40
|
-
*/
|
|
41
|
-
export const area = defmulti(__dispatch, { quad: "poly" }, {
|
|
7
|
+
const area = defmulti(
|
|
8
|
+
__dispatch,
|
|
9
|
+
{ quad: "poly" },
|
|
10
|
+
{
|
|
42
11
|
aabb: ({ size: [w, h, d] }) => 2 * (w * h + w * d + h * d),
|
|
43
|
-
arc:
|
|
44
|
-
|
|
45
|
-
|
|
12
|
+
arc: (
|
|
13
|
+
// http://cut-the-knot.org/Generalization/Cavalieri2.shtml
|
|
14
|
+
($) => 0.5 * Math.abs($.start - $.end) * $.r[0] * $.r[1]
|
|
15
|
+
),
|
|
46
16
|
circle: ($) => PI * $.r ** 2,
|
|
47
17
|
ellipse: ($) => PI * $.r[0] * $.r[1],
|
|
48
18
|
group: ({ children }) => children.reduce((sum, $) => sum + area($, false), 0),
|
|
49
|
-
path: ($) =>
|
|
19
|
+
path: ($) => $.closed ? area(asPolygon($)) : 0,
|
|
50
20
|
plane: () => Infinity,
|
|
51
21
|
poly: ($, signed) => {
|
|
52
|
-
|
|
53
|
-
|
|
22
|
+
const area2 = polyArea2($.points);
|
|
23
|
+
return signed ? area2 : Math.abs(area2);
|
|
54
24
|
},
|
|
55
25
|
rect: ($) => $.size[0] * $.size[1],
|
|
56
26
|
sphere: ($) => 4 * PI * $.r ** 2,
|
|
57
27
|
tri: ($, signed) => {
|
|
58
|
-
|
|
59
|
-
|
|
28
|
+
const area2 = 0.5 * signedArea2(...$.points);
|
|
29
|
+
return signed ? area2 : Math.abs(area2);
|
|
60
30
|
},
|
|
61
|
-
[DEFAULT]: () => 0
|
|
62
|
-
}
|
|
31
|
+
[DEFAULT]: () => 0
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
export {
|
|
35
|
+
area
|
|
36
|
+
};
|