Hylograph.Forces.Contain
- Package
- purescript-hylograph-forces
- Repository
- afcondon/purescript-hylograph-forces
Containment: keep each node inside a region. A soft force steers a node back in before the nodes move; an optional hard confine puts it back in after they have.
Not in d3. The rules are the Rust kernel's (Region::project,
apply_contain and constrain in force-kernel/src/forces.rs), and the
two are held to each other bit for bit by test/differential.mjs.
The body. A node is kept inside as a rectangle of half-width w and
half-height h, grown all round by a radius r: a circle when w and
h are zero, a plain rectangle when r is, a rounded rectangle
otherwise.
Projection. project region r w h x y is where such a body centred
on (x, y) has to move its centre to, to lie inside the region. A centre
already far enough inside comes back unchanged.
- Rectangle: clamp x to
[x0 + (r + w), x1 - (r + w)]and y to[y0 + (r + h), y1 - (r + h)]. If the rectangle is narrower than the body on an axis, that coordinate goes to the middle of the rectangle. - Circle: the body counts as the circle round it, of radius
r + sqrt(w*w + h*h)(exact for a round body, a little cautious for a rectangle). WithR' = max(R - r - sqrt(w*w + h*h), 0), a centre further thanR'from the region's centrecmoves toc + (p - c) * (R' / |p - c|). - Convex polygon: one pass visits the edges in order; for each, with
inward unit normal
nanddthe signed distance from the centre (as it now is) to the edge's line, positive inside, the body reachess = (r + |n.x| * w) + |n.y| * htowards the edge, and a centre withd < smoves along the normal bys - d. Passes repeat until one moves nothing, eight at most.
Soft. For every node with a region, with a = position + velocity
finite and c = project region r w h a: v += (c - a) * strength. Not
scaled by alpha: it is a constraint, like collide.
Hard. After the nodes have moved, every node with a region and a
finite position is put at project region r w h position, and its
velocity changes by the same amount, so that the velocity is still the
step the node actually took. Pinned coordinates are then put back: a
pin beats a region.
#Region Source
data RegionA region nodes can be confined to. A polygon is held as its edges: a point on each and the edge's inward unit normal, in the order the vertices were given.
#Confinement Source
data ConfinementWhether a node is confined, and to what.
#free Source
free :: ConfinementNot confined.
#within Source
within :: Region -> ConfinementConfined to this region.
#Config Source
type Config :: Row Type -> Typetype Config row = { halfHeight :: Node row -> Number, halfWidth :: Node row -> Number, hard :: Boolean, radius :: Node row -> Number, region :: Node row -> Confinement, strength :: Number }
region, radius, halfWidth and halfHeight are asked once per node
when the force is initialized; NaN for any of the three sizes counts
as zero. strength is the soft
force's (zero turns it off); hard adds the confine after the nodes move.
- Modules
- Hylograph.
Forces - Hylograph.
Forces. Center - Hylograph.
Forces. Cohesion - Hylograph.
Forces. Collide - Hylograph.
Forces. Contain - Hylograph.
Forces. Directional - Hylograph.
Forces. Group - Hylograph.
Forces. Link - Hylograph.
Forces. Linkage - Hylograph.
Forces. ManyBody - Hylograph.
Forces. Measure - Hylograph.
Forces. Node - Hylograph.
Forces. Position - Hylograph.
Forces. Quadtree - Hylograph.
Forces. Radial - Hylograph.
Forces. Random - Hylograph.
Forces. RectCollide - Hylograph.
Forces. Snapshot - Hylograph.
Forces. Tick - Hylograph.
Forces. Towards