Ice Cream
The first assignment for Printing Code was to design an ice cream cone, using only the ellipse(), rect(), and triangle() functions in Processing, and using them only once each. Looping, of course, was allowed. I decided that the best way to build the cream was to use a recursive function. The function draws one circle, then below it two smaller circles, and then each of those circles has two smaller circles below it. I used a simple triangle to build the cone, and then a set of for loops to make the square pattern on the cone to give a suggestion of waffling. This is the result:
Here’s the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | //Kim Ash //Printing Code //draws an ice cream cone in black and white, using only rect, triangle, and ellipse /* Properties _________________________________________________________________ */ PGraphics canvas; int canvas_width = 3508; int canvas_height = 4961; float ratioWidth = 1; float ratioHeight = 1; float ratio = 1; /* Setup _________________________________________________________________ */ void setup() { size(1300, 850); background(30); canvas = createGraphics(canvas_width, canvas_height, P2D); calculateResizeRatio(); canvas.beginDraw(); canvas.background(255); //canvas.scale(2.0); canvas.smooth(); canvas.stroke(0); canvas.strokeWeight(4); //cone canvas.fill(255); canvas.triangle(canvas.width/2 + 75, canvas.height/2, canvas.width/2 - 75, canvas.height/2, canvas.width/2, canvas.height/2 + 350); //ice cream canvas.fill(0); drawCircle(canvas.width/2, canvas.height/2 - 150, 150); //cone squares canvas.noFill(); int s = 24; for(int i=0; i<2; i++){ for(int j=0; j<3; j++){ canvas.rect(canvas.width/2 + 30 - i*s - j*s, canvas.height/2 + 50 + j*s, s, s); } } canvas.endDraw(); float resizedWidth = (float) canvas.width * ratio; float resizedHeight = (float) canvas.height * ratio; image(canvas, (width / 2) - (resizedWidth / 2), (height / 2) - (resizedHeight / 2), resizedWidth, resizedHeight); canvas.save("grab.png"); } /* Calculate resizing _________________________________________________________________ */ void calculateResizeRatio() { ratioWidth = (float) width / (float) canvas.width; ratioHeight = (float) height / (float) canvas.height; if(ratioWidth < ratioHeight) ratio = ratioWidth; else ratio = ratioHeight; } void drawCircle(float x, float y, float d) { canvas.ellipse(x, y, d, d); if (d >= 90) { drawCircle(x + d/3, y + (d*sqrt(3))/3, 3*d/4); drawCircle(x - d/3, y + (d*sqrt(3))/3, 3*d/4); } } |