1/** 2 * Letter K 3 * by Peter Cho. 4 * 5 * Move the mouse across the screen to fold the "K". 6 */ 7 8color backgroundColor; 9color foregroundColor; 10color foregroundColor2; 11 12float px, py; 13float pfx, pfy; 14float pv2, pvx, pvy; 15float pa2, pax, pay; 16float pMass, pDrag; 17 18void setup() { 19 size(640, 360, P3D); 20 noStroke(); 21 backgroundColor = color(134, 144, 154); 22 foregroundColor = color(235, 235, 30); 23 foregroundColor2 = color(240, 130, 20); 24 initParticle(0.6, 0.9, width/2, height/2); 25} 26 27void draw() { 28 background(backgroundColor); 29 pushMatrix(); 30 31 iterateParticle(0.15*(-px+mouseX), 0.15*(-py+(height-mouseY))); 32 33 translate(width/2, height/2, 0); 34 fill(foregroundColor); 35 drawK(); 36 37 pushMatrix(); 38 translate(0, 0, 1); 39 translate(0.75 * (px-width/2), -0.75 * (py-height/2), 0); 40 translate(0.75 * (px-width/2), -0.75 * (py-height/2), 0); 41 rotateZ(atan2(-(py-height/2), (px-width/2)) + PI/2); 42 rotateX(PI); 43 rotateZ(-(atan2(-(py-height/2), (px-width/2)) + PI/2)); 44 45 fill(foregroundColor2); 46 drawK(); 47 popMatrix(); 48 49 translate(0.75 * (px-width/2), -0.75 * (py-height/2), 2); 50 rotateZ(atan2(-(py-height/2), (px-width/2)) + PI/2); 51 52 fill(backgroundColor); 53 beginShape(QUADS); 54 vertex(-640, 0); 55 vertex( 640, 0); 56 vertex( 640, -360); 57 vertex(-640, -360); 58 endShape(); 59 60 popMatrix(); 61 62} 63 64void initParticle(float _mass, float _drag, float ox, float oy) { 65 px = ox; 66 py = oy; 67 pv2 = 0.0; 68 pvx = 0.0; 69 pvy = 0.0; 70 pa2 = 0.0; 71 pax = 0.0; 72 pay = 0.0; 73 pMass = _mass; 74 pDrag = _drag; 75} 76 77void iterateParticle(float fkx, float fky) { 78 // iterate for a single force acting on the particle 79 pfx = fkx; 80 pfy = fky; 81 pa2 = pfx*pfx + pfy*pfy; 82 if (pa2 < 0.0000001) { 83 return; 84 } 85 pax = pfx/pMass; 86 pay = pfy/pMass; 87 pvx += pax; 88 pvy += pay; 89 pv2 = pvx*pvx + pvy*pvy; 90 if (pv2 < 0.0000001) { 91 return; 92 } 93 pvx *= (1.0 - pDrag); 94 pvy *= (1.0 - pDrag); 95 px += pvx; 96 py += pvy; 97} 98 99void drawK() { 100 pushMatrix(); 101 102 scale(1.5); 103 translate(-63, 71); 104 beginShape(QUADS); 105 vertex(0, 0, 0); 106 vertex(0, -142.7979, 0); 107 vertex(37.1992, -142.7979, 0); 108 vertex(37.1992, 0, 0); 109 110 vertex(37.1992, -87.9990, 0); 111 vertex(84.1987, -142.7979, 0); 112 vertex(130.3979, -142.7979, 0); 113 vertex(37.1992, -43.999, 0); 114 115 vertex(77.5986-.2, -86.5986-.3, 0); 116 vertex(136.998, 0, 0); 117 vertex(90.7988, 0, 0); 118 vertex(52.3994-.2, -59.999-.3, 0); 119 endShape(); 120 //translate(63, -71); 121 popMatrix(); 122} 123 124 125 126