//-------------------------------------------------------------- void ofxVoronoi::generate(bool ordered) { voro::container_2d* con = new voro::container_2d(bounds.x, bounds.x+bounds.getWidth(), bounds.y, bounds.y+bounds.getHeight(), 10, 10, false, false, 16); voro::c_loop_all_2d* vl = new voro::c_loop_all_2d(*con); voro::voronoicell_2d conCell; for(int i=0; i<points.size(); i++) { con->put(i, points[i].x, points[i].y); } if(vl->start()) { do { con->compute_cell(conCell, *vl); int k = 0; if(conCell.p) { ofxVoronoiCell newCell; // Get the current point of the cell double* currentPoint = con->p[vl->ij]+con->ps*vl->q; newCell.centroid = ofDefaultVec3(currentPoint[0], currentPoint[1], 0); // Get the edgepoints of the cell do { float x = currentPoint[0] + 0.5 * conCell.pts[2*k]; float y = currentPoint[1] + 0.5 * conCell.pts[2*k+1]; ofDefaultVec3 pt = ofDefaultVec3(x, y, 0); newCell.points.push_back(pt); k = conCell.ed[2*k]; } while(k!=0); cells.push_back(newCell); } } while(vl->inc()); } // free up the memory delete con, vl; if(ordered) { std::vector<ofxVoronoiCell> orderedCells; for(auto& p : points) { // ofLog() << p; orderedCells.push_back(getCell(p)); } cells = orderedCells; } }
//-------------------------------------------------------------- void ofApp::update() { if (numToAdd) { ofDefaultVec3 v = ofDefaultVec3(ofRandom(rangeMin.x, rangeMax.x), ofRandom(rangeMin.y, rangeMax.y), ofRandom(rangeMin.z, rangeMax.z)); mesh.addVertex(v); set3f.add(v); --numToAdd; } }
//-------------------------------------------------------------- void ofApp::keyPressed(int key) { if (key == OF_KEY_BACKSPACE) { mesh.clear(); set3f.clear(); numToAdd = 0; } else if (key == ' ') { numToAdd += 100; // Adjust the range for fun. rangeMin = ofDefaultVec3(ofRandom(-kWorldSize * 0.5f, 0.0f), ofRandom(-kWorldSize * 0.5f, 0.0f), ofRandom(-kWorldSize * 0.5f, 0.0f)); rangeMax = ofDefaultVec3(ofRandom(0.0f, kWorldSize * 0.5f), ofRandom(0.0f, kWorldSize * 0.5f), ofRandom(0.0f, kWorldSize * 0.5f)); } }
//-------------------------------------------------------------- void ofApp::setup(){ int totalVertices = 5; float yInc = (ofGetHeight()-100)/totalVertices; for (int i = 0; i<= totalVertices; i++) { points.push_back(ofDefaultVec3(ofGetWidth()*0.5f, ofGetHeight() - (yInc * i + 50),0)); vertices.push_back(draggableVertex(points.back().x, points.back().y)); ofFloatColor c; c.setHsb(ofRandom(1), 1, 1 ); colors.push_back(c); weights.push_back(100); } fatLine.setFeather(2); fatLine.add(points, colors, weights); fatLine.printDebug(); bDrawDebug = false; }