Toxiclibs voronoi polygon hashmaps
in
Contributed Library Questions
•
22 days ago
I have a program that takes info from an XML file, makes a force-directed graph from it using the toxicLibs physics library, generates a voronoi diagram on each frame from the points in the graph, then trims the voronoi with a metaball... I am trying to find a way to color specific polygon regions of the voronoi. I have a "Node" class which is an XML object, and generates the verletparticles for my flowgraph. Each "Node" has a color value, so I would like use that value to color the corresponding polygon. Here is an image of the current program without the metaball:
There are two sets of VerletParticles in this, one is the set from the directed graph, the other is a set of perimeter particles to constrain the graph. All of these particles feed into my voronoi, and only the poly's containing graph particles are drawn.
Right now, I am checking each polygon in the voronoi to see if it contains a Vec2D from my list of graph particles, but it is terribly inefficient:
- public void displayVoronoi() {
if (App.UPDATE_VORONOI) {voronoi = new Voronoi();voronoi.addPoints(App.PSYS.getAttractorParticles());voronoi.addPoints(App.PSYS.getParticles());} if (App.SHOW_VORONOI && voronoi != null) {cells = new ArrayList<>();for (Polygon2D poly : voronoi.getRegions()) {poly = clipper.clipPolygon(poly);for (Vec2D v : cellSites) {if (poly.containsPoint(v)) {cells.add(poly);}}} for (Polygon2D poly : cells) {List<Vec2D> vec = poly.vertices;int count = vec.size();p5.beginShape();p5.vertex((vec.get(count - 1).x + vec.get(0).x) / 2,(vec.get(count - 1).y + vec.get(0).y) / 2);for (int i = 0; i < count; i++) {p5.bezierVertex(vec.get(i).x, vec.get(i).y,vec.get(i).x, vec.get(i).y,(vec.get((i + 1) % count).x + vec.get(i).x) / 2,(vec.get((i + 1) % count).y + vec.get(i).y) / 2);} p5.endShape(PApplet.CLOSE);}}}
I would rather make a hashmap from the graph particles to the voronoi polys, but since the voronoi is re-instantiated on each draw call, the index values are always changing.
How can I link the polygons to the particles without checking to see if the polygon contains the vector?
I have no formal training in programming, so I'm kind of winging it here...
Can anybody point me in the right direction?
Thanks,
Ben
1