-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(geom): initial import ComplexPolygon & impls (#464)
- add complexPolygon() factory fn - add bounds() & centroid() impls
- Loading branch information
1 parent
2235202
commit ded007c
Showing
5 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { ensureArray } from "@thi.ng/arrays/ensure-array"; | ||
import type { Attribs, IHiccupShape } from "@thi.ng/geom-api"; | ||
import { __copyAttribs } from "../internal/copy.js"; | ||
import { Polygon } from "./polygon.js"; | ||
|
||
export class ComplexPolygon implements IHiccupShape { | ||
children: Polygon[]; | ||
|
||
constructor( | ||
public boundary: Polygon = new Polygon(), | ||
children?: Iterable<Polygon>, | ||
public attribs?: Attribs | ||
) { | ||
this.children = children ? ensureArray(children) : []; | ||
} | ||
|
||
get type() { | ||
return "complexpoly"; | ||
} | ||
|
||
addChild(poly: Polygon) { | ||
this.children.push(poly); | ||
} | ||
|
||
copy(): ComplexPolygon { | ||
return new ComplexPolygon( | ||
this.boundary.copy(), | ||
this.children.map((h) => h.copy()), | ||
__copyAttribs(this) | ||
); | ||
} | ||
|
||
withAttribs(attribs: Attribs): ComplexPolygon { | ||
return new ComplexPolygon(this.boundary, this.children, attribs); | ||
} | ||
|
||
toHiccup() { | ||
const segments: any[] = []; | ||
const $hiccupSegments = ({ points }: Polygon) => { | ||
if (!points.length) return; | ||
segments.push(["M", points[0]]); | ||
for (let i = 1, n = points.length; i < n; i++) { | ||
segments.push(["L", points[i]]); | ||
} | ||
segments.push(["Z"]); | ||
}; | ||
$hiccupSegments(this.boundary); | ||
for (let c of this.children) $hiccupSegments(c); | ||
return ["path", this.attribs, segments]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { Attribs } from "@thi.ng/geom-api"; | ||
import { ComplexPolygon } from "./api/complex-polygon.js"; | ||
import { Polygon } from "./api/polygon.js"; | ||
|
||
export const complexPolygon = ( | ||
boundary?: Polygon, | ||
children?: Iterable<Polygon>, | ||
attribs?: Attribs | ||
) => new ComplexPolygon(boundary, children, attribs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters