Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROCESSING PROGRAM:
- int numparticles =10000;
- particle[] particles;
- void setup()
- {
- size(500, 500);
- background(255);
- noStroke();
- //smooth();
- particles = new particle[numparticles];
- for(int i=0; i<numparticles; i++)
- {
- particles[i] = new particle(random(450, width-20), random(450, height-20), 10, 10, 5, 0.9,
- color(0, 0, 0, 255));
- }
- }
- void draw()
- {
- background(255);
- for(int i=0; i<numparticles; i++)
- {
- particles[i].collide();
- particles[i].move();
- particles[i].render();
- particles[i].xspeed*=particles[i].dampfactor;
- particles[i].yspeed*=particles[i].dampfactor;
- }
- if (frameCount%30==0) println(frameRate);
- }
- class particle
- {
- float xpos, ypos;
- float xspeed, yspeed;
- int ewidth;
- int eheight;
- float speedfactor;
- float dampfactor;
- color col;
- particle(float x, float y,int ew, int eh, float sf, float df,color c)
- {
- xpos=x;
- ypos=y;
- xspeed=0;
- yspeed=0;
- ewidth=ew;
- eheight=eh;
- speedfactor=sf;
- dampfactor=df;
- col=c;
- }
- void collide()
- {
- int leftcols = (int)random(0, eheight+1);
- int rightcols=(int)random(0, eheight+1);
- int topcols= (int)random(0, ewidth+1);
- int botcols= (int)random(0, ewidth+1);
- xspeed+= (leftcols-rightcols)/speedfactor;
- yspeed+= (topcols-botcols)/speedfactor;
- }
- void move()
- {
- xpos+=xspeed;
- ypos+=yspeed;
- if (xpos+ewidth> width || xpos-ewidth<0) xspeed*=-1;
- if (ypos+eheight> height || ypos-eheight<0) yspeed*=-1;
- }
- void render()
- {
- fill(col);
- ellipse(xpos, ypos, ewidth, eheight);
- }
- }
- //---------------------------------------------------------------------------------------------------------
- OPENFRAMEWORKS PROGRAM
- main.cpp:
- #include "ofMain.h"
- #include "brownian.h"
- //#include "ofAppGlutWindow.h"
- //========================================================================
- int main()
- {
- //ofAppGlutWindow window;
- ofSetupOpenGL(500,500, OF_WINDOW); // <-------- setup the GL context
- // this kicks off the running of my app
- // can be OF_WINDOW or OF_FULLSCREEN
- // pass in width and height too:
- ofRunApp(new brownian());
- }
- brownian.h:
- #ifndef _BROWNIAN
- #define _BROWNIAN
- #include "ofMain.h"
- #include "particle.h"
- #define numparticles 10000
- class brownian : public ofBaseApp
- {
- public:
- brownian() {};
- void setup();
- void update();
- void draw();
- particle particles[numparticles];
- };
- #endif
- brownian.cpp:
- #include "ofMain.h"
- #include "brownian.h"
- #include <iostream>
- //--------------------------------------------------------------
- void brownian::setup()
- {
- ofBackground(255,255,255);
- ofEnableSmoothing();
- int i;
- for(i=0; i<numparticles; i++)
- {
- particles[i] = particle(ofRandom(450, ofGetWidth()-20), ofRandom(450, ofGetHeight()-20), 10, 10, 5, 0.9);
- }
- }
- //------------------------------------------------------------
- void brownian::update()
- {
- int i;
- for(i=0; i<numparticles; i++)
- {
- particles[i].collide();
- particles[i].move();
- particles[i].xspeed*=particles[i].dampfactor;
- particles[i].yspeed*=particles[i].dampfactor;
- }
- if (ofGetFrameNum()%30 ==0) {std::cout << (ofToString(ofGetFrameRate())) <<" ";}
- }
- //--------------------------------------------------------------
- void brownian::draw()
- {
- int i;
- for(i=0; i<numparticles; i++)
- {
- particles[i].render();
- }
- }
- //--------------------------------------------------------------
- particle.h:
- #ifndef _PARTICLE
- #define _PARTICLE
- class particle
- {
- public:
- float xpos, ypos;
- float xspeed, yspeed;
- float ewidth;
- float eheight;
- float speedfactor;
- float dampfactor;
- particle() {};
- particle(float x, float y,int ew, int eh, float sf, float df);
- void collide();
- void move();
- void render();
- };
- #endif
- particle.cpp:
- #include "particle.h"
- #include "ofMain.h"
- particle::particle(float x, float y,int ew, int eh, float sf, float df)
- {
- xpos=x;
- ypos=y;
- xspeed=0;
- yspeed=0;
- ewidth=ew;
- eheight=eh;
- speedfactor=sf;
- dampfactor=df;
- }
- void particle::collide()
- {
- float leftcols = ofRandom(0, eheight+1);
- float rightcols= ofRandom(0, eheight+1);
- float topcols= ofRandom(0, ewidth+1);
- float botcols= ofRandom(0, ewidth+1);
- xspeed+= (leftcols-rightcols)/speedfactor;
- yspeed+= (topcols-botcols)/speedfactor;
- }
- void particle::move()
- {
- xpos+=xspeed;
- ypos+=yspeed;
- if (xpos+ewidth> ofGetWidth() || xpos-ewidth<0) xspeed*=-1;
- if (ypos+eheight> ofGetHeight() || ypos-eheight<0) yspeed*=-1;
- }
- void particle::render()
- {
- ofFill();
- ofSetColor(0,0,0);
- ofEllipse(xpos, ypos, ewidth, eheight);
- }
Advertisement
Add Comment
Please, Sign In to add comment