SHOW:
|
|
- or go back to the newest paste.
| 1 | //------------------------------------------------- | |
| 2 | // Processing (processing.org) | |
| 3 | // rotating rectangles with red dots (by iomikron) | |
| 4 | //------------------------------------------------- | |
| 5 | ||
| 6 | int numOfRectangles = 250; | |
| 7 | Rectangle [] rectArray = new Rectangle[numOfRectangles]; | |
| 8 | ||
| 9 | void setup() | |
| 10 | {
| |
| 11 | size(300, 300); | |
| 12 | - | gifExport.setRepeat(0); //make it an 'endless' animation |
| 12 | + | |
| 13 | for(int i = 0; i <= numOfRectangles-1; i++) | |
| 14 | {
| |
| 15 | rectArray[i] = | |
| 16 | new Rectangle( | |
| 17 | random(20, width - 20), //coordinate X | |
| 18 | random(20,height - 20), //coordinate Y | |
| 19 | 133, //color | |
| 20 | 20., //width | |
| 21 | 20., //height | |
| 22 | PI/(random(i,i*i)), //angle (initial) | |
| 23 | random(2.75,5.8), //angular Velocity (initial) | |
| 24 | random(0.1,0.25)//rotate speed (initial) | |
| 25 | ); | |
| 26 | } | |
| 27 | } | |
| 28 | ||
| 29 | void draw() | |
| 30 | {
| |
| 31 | background(0); | |
| 32 | for(int i = 0; i <= numOfRectangles-1; i++) | |
| 33 | {
| |
| 34 | rectArray[i].move(); | |
| 35 | } | |
| 36 | } | |
| 37 | ||
| 38 | - | if(keyPressed == true) |
| 38 | + | |
| 39 | //Rectangle CLASS DEFINITION | |
| 40 | - | gifExport.finish(); |
| 40 | + | |
| 41 | class Rectangle | |
| 42 | - | else |
| 42 | + | |
| 43 | float coordX;//horizontal coordinate for the center | |
| 44 | - | gifExport.addFrame(); |
| 44 | + | |
| 45 | float rectColor;//color filling | |
| 46 | // float rectAlpha; Uncomment for opacity | |
| 47 | float rectWidth;//parameter for the width | |
| 48 | float widthConst; | |
| 49 | float rectHeight;//parameter for the height | |
| 50 | float angle; | |
| 51 | float angularVelocity; | |
| 52 | float rotationSpeed; | |
| 53 | ||
| 54 | //Constructor | |
| 55 | Rectangle(float X, float Y, | |
| 56 | float C, | |
| 57 | float W, float H, | |
| 58 | float a, float angV, float rotS) | |
| 59 | {
| |
| 60 | coordX = X; | |
| 61 | coordY = Y; | |
| 62 | rectColor = C; | |
| 63 | rectWidth = W; | |
| 64 | widthConst = W; | |
| 65 | rectHeight = H; | |
| 66 | angle = a; | |
| 67 | angularVelocity = angV; | |
| 68 | rotationSpeed = rotS; | |
| 69 | } | |
| 70 | //FUNCTIONS FOR THE CLASS | |
| 71 | void display() | |
| 72 | {
| |
| 73 | rectMode(CENTER); | |
| 74 | //define color and opacity | |
| 75 | noStroke(); | |
| 76 | fill(changeColor()); | |
| 77 | //create the rectangle | |
| 78 | rect(0, 0, widthConst*changeWidth(), rectHeight); | |
| 79 | } | |
| 80 | ||
| 81 | void move() | |
| 82 | {
| |
| 83 | pushMatrix(); | |
| 84 | translate(coordX, coordY); | |
| 85 | rotate(radians(angle)); | |
| 86 | ||
| 87 | display(); | |
| 88 | ||
| 89 | stroke(color(255,0,0)); | |
| 90 | strokeWeight(2.5); | |
| 91 | point(0,0); | |
| 92 | ||
| 93 | popMatrix(); | |
| 94 | ||
| 95 | changeAngle(); | |
| 96 | ||
| 97 | changeWidth(); | |
| 98 | } | |
| 99 | ||
| 100 | float changeAngle() | |
| 101 | {
| |
| 102 | angle = angle + angularVelocity; | |
| 103 | return(angle); | |
| 104 | } | |
| 105 | ||
| 106 | float changeWidth() | |
| 107 | {
| |
| 108 | float w; | |
| 109 | rectWidth = rectWidth + rotationSpeed; | |
| 110 | w = cos(rectWidth); | |
| 111 | return(w); | |
| 112 | } | |
| 113 | ||
| 114 | float changeColor() | |
| 115 | {
| |
| 116 | float c; | |
| 117 | c = rectColor*cos(rectWidth)*cos(rectWidth) + 120; | |
| 118 | return(c); | |
| 119 | } | |
| 120 | ||
| 121 | } |