Advertisement
iomikron

Rotating rectangles(blue stroke)

Oct 5th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. //-----------------------------------------------
  2. // Processing - processing.org
  3. // Rotating rectangles(blue stroke) - by iomikron
  4. //-----------------------------------------------
  5.  
  6. int numOfRectangles = 250;
  7. Rectangle [] rectArray = new Rectangle[numOfRectangles];
  8.  
  9. void setup()
  10. {
  11. size(300, 300);
  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. //--------------------------
  39. //Rectangle CLASS DEFINITION
  40. //--------------------------
  41. class Rectangle
  42. {
  43. float coordX;//horizontal coordinate for the center
  44. float coordY;//vertical coordinate for the center
  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. stroke(color(0,133,255));
  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. popMatrix();
  90.  
  91. changeAngle();
  92.  
  93. changeWidth();
  94. }
  95.  
  96. float changeAngle()
  97. {
  98. angle = angle + angularVelocity;
  99. return(angle);
  100. }
  101.  
  102. float changeWidth()
  103. {
  104. float w;
  105. rectWidth = rectWidth + rotationSpeed;
  106. w = cos(rectWidth);
  107. return(w);
  108. }
  109.  
  110. float changeColor()
  111. {
  112. float c;
  113. c = rectColor*cos(rectWidth)*cos(rectWidth) + 120;
  114. return(c);
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement