Cone
From Processing
| Versions: | 1.0+ |
| Contributors: | tomc |
| Started: | 2006-03-10 |
Cone isn't a built in graphics primitive like sphere or box are in Processing, however it can be useful, for instance to draw quick and simple voronoi diagrams.
The codeDetail() function uses a static code block to initialize one copy of the base circle coordinates. It then draws a cone with triangles, the number of sides can be set using coneDetail() (best inside setup() for speed reasons). The default coneDetail value is 24.
Paste the following code into a new tab and use the cone() function like you would use Processing's sphere() and box().
Source Code
/** cone taken from http://wiki.processing.org/index.php/Cone @author Tom Carden */ static float unitConeX[]; static float unitConeY[]; static int coneDetail; static { coneDetail(24); } // just inits the points of a circle, // if you're doing lots of cones the same size // then you'll want to cache height and radius too static void coneDetail(int det) { coneDetail = det; unitConeX = new float[det+1]; unitConeY = new float[det+1]; for (int i = 0; i <= det; i++) { float a1 = TWO_PI * i / det; unitConeX[i] = (float)Math.cos(a1); unitConeY[i] = (float)Math.sin(a1); } } // places a cone with it's base centred at (x,y), // beight h in positive z, radius r. void cone(float x, float y, float r, float h) { pushMatrix(); translate(x,y); scale(r,r); beginShape(TRIANGLES); for (int i = 0; i < coneDetail; i++) { vertex(unitConeX[i],unitConeY[i],0.0); vertex(unitConeX[i+1],unitConeY[i+1],0.0); vertex(0,0,h); } endShape(); popMatrix(); }