All examples By author By category About

larsenmtl

d3 Force Layout Bound to N Sided Random Polygon

Originally coded for this Stackoverflow Question. First, it causes the force layout to converge on a different foci then the default width/2, height/2. The new foci should be the centroid of the triangle computed with this d3 helper method. Second, now that we are converging on the centroid of the polygon, our nodes our bound inside the polygon by calculating the intersections between lines drawn from the centroid to the node and the line of the edge of the polygon (intersection calculation from this question). No intersections on all sides means the circle is in the polygon, and an intersection on any edge means we need to bring the circle onto that edge.

Built with blockbuilder.org