Java Code Examples for processing.core.PApplet#random()

The following examples show how to use processing.core.PApplet#random() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PVector.java    From CPE552-Java with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Make a new 3D unit vector with a random direction
 * @return the random PVector
 */
static public PVector random3D(PVector target, PApplet parent) {
  float angle;
  float vz;
  if (parent == null) {
    angle = (float) (Math.random()*Math.PI*2);
    vz    = (float) (Math.random()*2-1);
  } else {
    angle = parent.random(PConstants.TWO_PI);
    vz    = parent.random(-1,1);
  }
  float vx = (float) (Math.sqrt(1-vz*vz)*Math.cos(angle));
  float vy = (float) (Math.sqrt(1-vz*vz)*Math.sin(angle));
  if (target == null) {
    target = new PVector(vx, vy, vz);
    //target.normalize(); // Should be unnecessary
  } else {
    target.set(vx,vy,vz);
  }
  return target;
}
 
Example 2
Source File: Voronoi3D.java    From haxademic with MIT License 4 votes vote down vote up
public static ArrayList<WETriangleMesh> getShatteredBox( PApplet p, float scale ) {
	
	// from example: http://www.wblut.com/2010/10/20/hemesh-voronoi-example/
	ArrayList<WETriangleMesh> meshes = new ArrayList<WETriangleMesh>();

	// or a box
	HEC_Box box=new HEC_Box();
	box.setWidth( 100 );
	box.setHeight( 100 );
	box.setDepth( 100 );
	HE_Mesh container = new HE_Mesh(box);

	// generate fracture points
	int numpoints = 15;
	float[][] points = new float[numpoints][3];
	for(int i=0;i<numpoints;i++) {
		points[i][0] = p.random(-100,100);
		points[i][1] = p.random(-100,100);
		points[i][2] = p.random(-100,100);
	}

	// generate voronoi cells
	HEMC_VoronoiCells vcmc = new HEMC_VoronoiCells();
	vcmc.setPoints(points).setContainer(container).setOffset(5);
	HE_MeshCollection cells = vcmc.create();
	int numcells = cells.size();
	
	
	// convert to toxiclibs mesh
	HE_Mesh cell;
	HE_Face[] faces;
	for( int i=0; i < numcells; i++ ) {
		cell = cells.getMesh(i);
		faces = cell.getFacesAsArray();
		
		WETriangleMesh toxiMesh = new WETriangleMesh(); 
		meshes.add( toxiMesh );
		
		for(int j=0;j<faces.length;j++) {
			int numVertices = faces[j].getFaceVertices().size();
			if( numVertices == 3 ) {
				// straight triangle conversion 
				toxiMesh.addFace( 
					new Vec3D( faces[j].getFaceVertices().get( 0 ).xf(), faces[j].getFaceVertices().get( 0 ).yf(), faces[j].getFaceVertices().get( 0 ).zf() ), 
					new Vec3D( faces[j].getFaceVertices().get( 1 ).xf(), faces[j].getFaceVertices().get( 1 ).yf(), faces[j].getFaceVertices().get( 1 ).zf() ), 
					new Vec3D( faces[j].getFaceVertices().get( 2 ).xf(), faces[j].getFaceVertices().get( 2 ).yf(), faces[j].getFaceVertices().get( 2 ).zf() )
				);
			} else if( numVertices > 3 ) {
				// simple subdivision of polygons to triangles
				int trisLeft = 1 + numVertices - 3;
				int firstVertex = 1;
				while( trisLeft > 0 ) {
					toxiMesh.addFace(
							new Vec3D( faces[j].getFaceVertices().get( 0 ).xf(), faces[j].getFaceVertices().get( 0 ).yf(), faces[j].getFaceVertices().get( 0 ).zf() ), 
							new Vec3D( faces[j].getFaceVertices().get( firstVertex ).xf(), faces[j].getFaceVertices().get( firstVertex ).yf(), faces[j].getFaceVertices().get( firstVertex ).zf() ), 
							new Vec3D( faces[j].getFaceVertices().get( firstVertex + 1 ).xf(), faces[j].getFaceVertices().get( firstVertex + 1 ).yf(), faces[j].getFaceVertices().get( firstVertex + 1 ).zf() )
					);
					trisLeft--;
					firstVertex++;
				}
			}
		}
		toxiMesh.scale( 0.01f * scale );	// make up for the fact that we did the fracture on a 100 size box 
	}
	return meshes;
}