//---------------------------------------- void ofxBox2dPolygon::triangulate(float resampleAmt, int nPointsInside) { triangles.clear(); if(size() > 0) { // copy over the points into a polyline ofPolyline polyOutline; ofPolyline newPoly; // make sure to close the polyline and then // simplify and resample by spacing... setClosed(true); if(!bIsSimplified) simplify(); newPoly = getResampledBySpacing(resampleAmt); // save the outline... polyOutline = newPoly; // add some random points inside then // triangulate the polyline... if(nPointsInside!=-1) addRandomPointsInside(newPoly, nPointsInside); triangles = triangulatePolygonWithOutline(newPoly, polyOutline); clear(); // now add back into polyshape for (int i=0; i<newPoly.size(); i++) { addVertex(newPoly[i]); } } bIsTriangulated = true; }
//-------------------------------------------------------------- void testApp::mouseReleased(int x, int y, int button){ // This is the manual way to triangulate the shape // you can then add many little triangles // first simplify the shape shape.simplify(); // save the outline of the shape ofPolyline outline = shape; // resample shape ofPolyline resampled = shape.getResampledBySpacing(25); // trangleate the shape, return am array of traingles vector <TriangleShape> tris = triangulatePolygonWithOutline(resampled, outline); // add some random points inside addRandomPointsInside(shape, 255); // now loop through all the trainles and make a box2d triangle for (int i=0; i<tris.size(); i++) { ofxBox2dPolygon p; p.addTriangle(tris[i].a, tris[i].b, tris[i].c); p.setPhysics(1.0, 0.3, 0.3); p.setAsEdge(false); if(p.isGoodShape()) { p.create(box2d.getWorld()); triangles.push_back(p); } } // done with shape clear it now shape.clear(); }
//-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button) { if(breakupIntoTriangles) { // This is the manual way to triangulate the shape // you can then add many little triangles // first simplify the shape shape.simplify(); // save the outline of the shape ofPolyline outline = shape; // resample shape ofPolyline resampled = shape.getResampledBySpacing(25); // trangleate the shape, return am array of traingles vector <TriangleShape> tris = triangulatePolygonWithOutline(resampled, outline); // add some random points inside addRandomPointsInside(shape, 255); // now loop through all the trainles and make a box2d triangle for(int i=0; i<tris.size(); i++) { auto triangle = std::make_shared<ofxBox2dPolygon>(); triangle->addTriangle(ofDefaultVertexType(tris[i].a.x, tris[i].a.y, 0), ofDefaultVertexType(tris[i].b.x, tris[i].b.y, 0), ofDefaultVertexType(tris[i].c.x, tris[i].c.y, 0)); triangle->setPhysics(1.0, 0.3, 0.3); triangle->create(box2d.getWorld()); polyShapes.push_back(triangle); } } else { auto poly = std::make_shared<ofxBox2dPolygon>(); poly->addVertices(shape.getVertices()); poly->setPhysics(1.0, 0.3, 0.3); poly->triangulatePoly(); poly->create(box2d.getWorld()); polyShapes.push_back(poly); } // done with shape clear it now shape.clear(); }