Advertisement
iomikron

Processing - rotating rectangles(red dots)

Oct 5th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  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
  13.  
  14. for(int i = 0; i <= numOfRectangles-1; i++)
  15. {
  16. rectArray[i] =
  17. new Rectangle(
  18. random(20, width - 20), //coordinate X
  19. random(20,height - 20), //coordinate Y
  20. 133, //color
  21. 20., //width
  22. 20., //height
  23. PI/(random(i,i*i)), //angle (initial)
  24. random(2.75,5.8), //angular Velocity (initial)
  25. random(0.1,0.25)//rotate speed (initial)
  26. );
  27. }
  28. }
  29.  
  30. void draw()
  31. {
  32. background(0);
  33. for(int i = 0; i <= numOfRectangles-1; i++)
  34. {
  35. rectArray[i].move();
  36. }
  37.  
  38. if(keyPressed == true)
  39. {
  40. gifExport.finish();
  41. }
  42. else
  43. {
  44. gifExport.addFrame();
  45. }
  46. }
  47. //--------------------------
  48. //Rectangle CLASS DEFINITION
  49. //--------------------------
  50. class Rectangle
  51. {
  52. float coordX;//horizontal coordinate for the center
  53. float coordY;//vertical coordinate for the center
  54. float rectColor;//color filling
  55. // float rectAlpha; Uncomment for opacity
  56. float rectWidth;//parameter for the width
  57. float widthConst;
  58. float rectHeight;//parameter for the height
  59. float angle;
  60. float angularVelocity;
  61. float rotationSpeed;
  62.  
  63. //Constructor
  64. Rectangle(float X, float Y,
  65. float C,
  66. float W, float H,
  67. float a, float angV, float rotS)
  68. {
  69. coordX = X;
  70. coordY = Y;
  71. rectColor = C;
  72. rectWidth = W;
  73. widthConst = W;
  74. rectHeight = H;
  75. angle = a;
  76. angularVelocity = angV;
  77. rotationSpeed = rotS;
  78. }
  79. //FUNCTIONS FOR THE CLASS
  80. void display()
  81. {
  82. rectMode(CENTER);
  83. //define color and opacity
  84. noStroke();
  85. fill(changeColor());
  86. //create the rectangle
  87. rect(0, 0, widthConst*changeWidth(), rectHeight);
  88. }
  89.  
  90. void move()
  91. {
  92. pushMatrix();
  93. translate(coordX, coordY);
  94. rotate(radians(angle));
  95.  
  96. display();
  97.  
  98. stroke(color(255,0,0));
  99. strokeWeight(2.5);
  100. point(0,0);
  101.  
  102. popMatrix();
  103.  
  104. changeAngle();
  105.  
  106. changeWidth();
  107. }
  108.  
  109. float changeAngle()
  110. {
  111. angle = angle + angularVelocity;
  112. return(angle);
  113. }
  114.  
  115. float changeWidth()
  116. {
  117. float w;
  118. rectWidth = rectWidth + rotationSpeed;
  119. w = cos(rectWidth);
  120. return(w);
  121. }
  122.  
  123. float changeColor()
  124. {
  125. float c;
  126. c = rectColor*cos(rectWidth)*cos(rectWidth) + 120;
  127. return(c);
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement