Last active
July 29, 2019 22:22
-
-
Save postspectacular/063b190b5540a1615f3e43a6fcb82fdd to your computer and use it in GitHub Desktop.
https://demo.thi.ng/umbrella/geom-convex-hull/ (Refactored & simplified version of code from: http://www.petecorey.com/blog/2019/07/29/clipping-convex-hulls-with-thing/)
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
import { | |
area, | |
centroid, | |
clipConvex, | |
convexHull, | |
points, | |
polygon, | |
rect, | |
scatter, | |
withAttribs | |
} from "@thi.ng/geom"; | |
import { IShape } from "@thi.ng/geom-api"; | |
import { renderOnce } from "@thi.ng/hdom"; | |
import { canvas } from "@thi.ng/hdom-canvas"; | |
const width = 600; | |
const height = 600; | |
const bounds = rect([width, height]); | |
// generate random points within given shape | |
const pts1 = scatter(bounds, 5)!; | |
const pts2 = scatter(bounds, 5)!; | |
// complex hull for shape | |
const hull1 = convexHull(polygon(pts1)); | |
const hull2 = convexHull(polygon(pts2)); | |
// Sutherland-Hodgman | |
const clip = clipConvex(hull1, hull2); | |
const COL1 = (a: number) => `rgba(245,93,62,${a})`; | |
const COL2 = (a: number) => `rgba(118,190,208,${a})`; | |
const COL3 = (a: number) => `rgba(102,102,102,${a})`; | |
// render shapes with thi.ng/hdom & thi.ng/hdom-canvas | |
renderOnce([ | |
canvas, | |
{ width, height }, | |
points(pts1, { shape: "circle", fill: COL1(1), size: 5 }), | |
points(pts2, { shape: "circle", fill: COL2(1), size: 5 }), | |
withAttribs(hull1, { fill: COL1(0.5) }), | |
withAttribs(hull2, { fill: COL2(0.5) }), | |
withAttribs(clip, { | |
fill: COL3(0.5), | |
stroke: COL3(1), | |
weight: 3 | |
}), | |
...(<[IShape, string][]>[ | |
[hull1, COL1(1)], | |
[hull2, COL2(1)], | |
[clip, COL3(1)] | |
]).map(([shape, col]) => [ | |
"text", | |
{ fill: col }, | |
centroid(shape), | |
area(shape).toFixed(2) | |
]) | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment