Guest User

Giles Whitaker

a guest
Feb 12th, 2010
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. PROCESSING PROGRAM:
  2.  
  3. int numparticles =10000;
  4. particle[] particles;
  5.  
  6. void setup()
  7. {
  8. size(500, 500);
  9. background(255);
  10. noStroke();
  11. //smooth();
  12. particles = new particle[numparticles];
  13. for(int i=0; i<numparticles; i++)
  14. {
  15. particles[i] = new particle(random(450, width-20), random(450, height-20), 10, 10, 5, 0.9,
  16. color(0, 0, 0, 255));
  17. }
  18. }
  19.  
  20. void draw()
  21. {
  22. background(255);
  23. for(int i=0; i<numparticles; i++)
  24. {
  25. particles[i].collide();
  26. particles[i].move();
  27. particles[i].render();
  28.  
  29. particles[i].xspeed*=particles[i].dampfactor;
  30. particles[i].yspeed*=particles[i].dampfactor;
  31. }
  32. if (frameCount%30==0) println(frameRate);
  33. }
  34.  
  35. class particle
  36. {
  37. float xpos, ypos;
  38. float xspeed, yspeed;
  39. int ewidth;
  40. int eheight;
  41. float speedfactor;
  42. float dampfactor;
  43. color col;
  44.  
  45. particle(float x, float y,int ew, int eh, float sf, float df,color c)
  46. {
  47. xpos=x;
  48. ypos=y;
  49. xspeed=0;
  50. yspeed=0;
  51. ewidth=ew;
  52. eheight=eh;
  53. speedfactor=sf;
  54. dampfactor=df;
  55. col=c;
  56. }
  57.  
  58. void collide()
  59. {
  60. int leftcols = (int)random(0, eheight+1);
  61. int rightcols=(int)random(0, eheight+1);
  62. int topcols= (int)random(0, ewidth+1);
  63. int botcols= (int)random(0, ewidth+1);
  64.  
  65. xspeed+= (leftcols-rightcols)/speedfactor;
  66. yspeed+= (topcols-botcols)/speedfactor;
  67. }
  68.  
  69. void move()
  70. {
  71. xpos+=xspeed;
  72. ypos+=yspeed;
  73. if (xpos+ewidth> width || xpos-ewidth<0) xspeed*=-1;
  74. if (ypos+eheight> height || ypos-eheight<0) yspeed*=-1;
  75. }
  76.  
  77. void render()
  78. {
  79. fill(col);
  80. ellipse(xpos, ypos, ewidth, eheight);
  81. }
  82.  
  83. }
  84. //---------------------------------------------------------------------------------------------------------
  85.  
  86.  
  87. OPENFRAMEWORKS PROGRAM
  88.  
  89.  
  90. main.cpp:
  91.  
  92. #include "ofMain.h"
  93. #include "brownian.h"
  94. //#include "ofAppGlutWindow.h"
  95.  
  96. //========================================================================
  97. int main()
  98. {
  99.  
  100. //ofAppGlutWindow window;
  101. ofSetupOpenGL(500,500, OF_WINDOW); // <-------- setup the GL context
  102.  
  103. // this kicks off the running of my app
  104. // can be OF_WINDOW or OF_FULLSCREEN
  105. // pass in width and height too:
  106. ofRunApp(new brownian());
  107. }
  108.  
  109. brownian.h:
  110.  
  111. #ifndef _BROWNIAN
  112. #define _BROWNIAN
  113.  
  114.  
  115. #include "ofMain.h"
  116. #include "particle.h"
  117.  
  118. #define numparticles 10000
  119.  
  120.  
  121. class brownian : public ofBaseApp
  122. {
  123. public:
  124. brownian() {};
  125. void setup();
  126. void update();
  127. void draw();
  128. particle particles[numparticles];
  129. };
  130.  
  131. #endif
  132.  
  133. brownian.cpp:
  134.  
  135. #include "ofMain.h"
  136. #include "brownian.h"
  137. #include <iostream>
  138.  
  139.  
  140. //--------------------------------------------------------------
  141. void brownian::setup()
  142. {
  143. ofBackground(255,255,255);
  144. ofEnableSmoothing();
  145. int i;
  146. for(i=0; i<numparticles; i++)
  147. {
  148. particles[i] = particle(ofRandom(450, ofGetWidth()-20), ofRandom(450, ofGetHeight()-20), 10, 10, 5, 0.9);
  149.  
  150. }
  151.  
  152. }
  153. //------------------------------------------------------------
  154. void brownian::update()
  155. {
  156. int i;
  157. for(i=0; i<numparticles; i++)
  158. {
  159. particles[i].collide();
  160. particles[i].move();
  161. particles[i].xspeed*=particles[i].dampfactor;
  162. particles[i].yspeed*=particles[i].dampfactor;
  163. }
  164. if (ofGetFrameNum()%30 ==0) {std::cout << (ofToString(ofGetFrameRate())) <<" ";}
  165.  
  166. }
  167. //--------------------------------------------------------------
  168. void brownian::draw()
  169. {
  170. int i;
  171. for(i=0; i<numparticles; i++)
  172. {
  173. particles[i].render();
  174. }
  175. }
  176. //--------------------------------------------------------------
  177.  
  178. particle.h:
  179.  
  180. #ifndef _PARTICLE
  181. #define _PARTICLE
  182.  
  183.  
  184.  
  185. class particle
  186. {
  187.  
  188. public:
  189. float xpos, ypos;
  190. float xspeed, yspeed;
  191. float ewidth;
  192. float eheight;
  193. float speedfactor;
  194. float dampfactor;
  195. particle() {};
  196. particle(float x, float y,int ew, int eh, float sf, float df);
  197. void collide();
  198. void move();
  199. void render();
  200.  
  201. };
  202.  
  203. #endif
  204.  
  205.  
  206. particle.cpp:
  207.  
  208. #include "particle.h"
  209. #include "ofMain.h"
  210.  
  211. particle::particle(float x, float y,int ew, int eh, float sf, float df)
  212. {
  213. xpos=x;
  214. ypos=y;
  215. xspeed=0;
  216. yspeed=0;
  217. ewidth=ew;
  218. eheight=eh;
  219. speedfactor=sf;
  220. dampfactor=df;
  221. }
  222.  
  223. void particle::collide()
  224. {
  225. float leftcols = ofRandom(0, eheight+1);
  226. float rightcols= ofRandom(0, eheight+1);
  227. float topcols= ofRandom(0, ewidth+1);
  228. float botcols= ofRandom(0, ewidth+1);
  229.  
  230. xspeed+= (leftcols-rightcols)/speedfactor;
  231. yspeed+= (topcols-botcols)/speedfactor;
  232. }
  233.  
  234. void particle::move()
  235. {
  236. xpos+=xspeed;
  237. ypos+=yspeed;
  238. if (xpos+ewidth> ofGetWidth() || xpos-ewidth<0) xspeed*=-1;
  239. if (ypos+eheight> ofGetHeight() || ypos-eheight<0) yspeed*=-1;
  240. }
  241.  
  242. void particle::render()
  243. {
  244. ofFill();
  245. ofSetColor(0,0,0);
  246. ofEllipse(xpos, ypos, ewidth, eheight);
  247. }
  248.  
  249.  
  250.  
  251.  
Advertisement
Add Comment
Please, Sign In to add comment