[ Video ]
[ About ]
ドーナッツの上をランダムウォーク
Random walk on torus.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#pragma once #include "ofMain.h" #include "Particle.h" class ofApp : public ofBaseApp { public: void setup(); void update(); void draw(); void keyPressed(int key) {}; void keyReleased(int key) {}; void mouseMoved(int x, int y) {}; void mouseDragged(int x, int y, int button) {}; void mousePressed(int x, int y, int button) {}; void mouseReleased(int x, int y, int button) {}; void mouseEntered(int x, int y) {}; void mouseExited(int x, int y) {}; void windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; ofEasyCam cam; vector<Particle> particles; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(239); ofSetColor(39); ofEnableDepthTest(); for (int i = 0; i < 512; i++) { this->particles.push_back(Particle()); } } //-------------------------------------------------------------- void ofApp::update() { for (Particle& particle : particles) { particle.Upate(); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); ofRotateY(ofGetFrameNum()); for (Particle& particle : particles) { particle.Draw(); } this->cam.end(); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); ~Particle(); void Upate(); void Draw(); private: ofPoint make_point(); float radius; float deg; float small_radius; float small_deg; float deg_step; float small_deg_step; std::deque<ofPoint> logs; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#include "Particle.h" //-------------------------------------------------------------- Particle::Particle() { this->radius = 300; this->deg = (int)(ofRandom(360) / 10) * 10; this->small_radius = 80; this->small_deg = (int)(ofRandom(360) / 20) * 20; } //-------------------------------------------------------------- Particle::~Particle() { } //-------------------------------------------------------------- void Particle::Upate() { if (ofGetFrameNum() % 20 == 0) { int r = ofRandom(4); switch (r) { case 0: this->deg_step = 0.5; this->small_deg_step = 0.0; break; case 1: this->deg_step = -0.5; this->small_deg_step = 0.0; break; case 2: this->deg_step = 0.0; this->small_deg_step = 1.0; break; case 3: this->deg_step = 0.0; this->small_deg_step = -1.0; break; default: this->deg_step = 0.0; this->small_deg_step = 0.0; break; } } this->deg += this->deg_step; this->small_deg += this->small_deg_step; ofPoint point = this->make_point(); this->logs.push_front(point); while (this->logs.size() > 45) { this->logs.pop_back(); } } //-------------------------------------------------------------- void Particle::Draw() { ofFill(); ofDrawSphere(this->logs.front(), 3); ofNoFill(); ofBeginShape(); for (ofPoint& log : this->logs) { ofVertex(log); } ofEndShape(); } //-------------------------------------------------------------- ofPoint Particle::make_point() { float x_1 = this->radius * cos(this->deg * DEG_TO_RAD); float y_1 = this->radius * sin(this->deg * DEG_TO_RAD); float x_2 = this->small_radius * cos(this->small_deg * DEG_TO_RAD) * cos(this->deg * DEG_TO_RAD); float y_2 = this->small_radius * cos(this->small_deg * DEG_TO_RAD) * sin(this->deg * DEG_TO_RAD); float x = x_1 + x_2; float y = y_1 + y_2; float z = this->small_radius * sin(this->small_deg * DEG_TO_RAD); return ofPoint(x, y, z); } |