[ Video ]
[ About ]
トルネード・ランダム・ウォーク。
[ 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#pragma once #include "ofMain.h" class Walker { public: Walker(glm::vec2 location, ofColor color); void update(); void change_rotate(); void draw_1(); void draw_2(); private: glm::vec2 rotate_location; float rotate_radius; float rotate_deg; int rotate_direction; vector<glm::vec2> log; ofColor color; }; 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) {}; vector<Walker> walker_list; }; |
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
#include "ofApp.h" //-------------------------------------------------------------- Walker::Walker(glm::vec2 location, ofColor color) { this->log.push_back(location); this->change_rotate(); this->color = color; } //-------------------------------------------------------------- void Walker::update() { if (glm::length(this->log.back()) > 720) { this->log.clear(); auto location = glm::vec2(ofRandom(-300, 300), ofRandom(-300, 300)); this->log.push_back(location); this->change_rotate(); } for (int i = 0; i < 3; i++) { auto location = this->rotate_location + glm::vec2(this->rotate_radius * cos(this->rotate_deg * DEG_TO_RAD), this->rotate_radius * sin(this->rotate_deg * DEG_TO_RAD)); this->rotate_deg += this->rotate_direction * 3; this->rotate_radius -= 0.15; this->log.push_back(location); } if (this->rotate_radius < 0 || ofRandom(100) < 0.15) { this->change_rotate(); } while (this->log.size() > 250) { this->log.erase(this->log.begin()); } } //-------------------------------------------------------------- void Walker::change_rotate() { auto tmp_radius = ofRandom(30, 110); auto tmp_deg = this->rotate_deg + ofRandom(-90, 90); this->rotate_location = this->log.back() + glm::vec2(tmp_radius * cos(tmp_deg * DEG_TO_RAD), tmp_radius * sin(tmp_deg * DEG_TO_RAD)); this->rotate_deg = 180 + tmp_deg; this->rotate_radius = tmp_radius; this->rotate_direction = ofRandom(100) < 50 ? 1 : -1; } //-------------------------------------------------------------- void Walker::draw_1() { ofSetColor(0); for (int i = 0; i < this->log.size() - 1; i++) { ofDrawCircle(this->log[i], 7); } } //-------------------------------------------------------------- void Walker::draw_2() { for (int i = 0; i < this->log.size() - 1; i++) { ofSetColor(ofColor(color)); ofDrawCircle(this->log[i], 5); } } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(255); ofSetLineWidth(2); ofColor color; int number_of_actor = 17; for (int i = 0; i < number_of_actor; i++) { color.setHsb(ofMap(i, 0, number_of_actor, 0, 255), 130, 255); this->walker_list.push_back(Walker(glm::vec2(ofRandom(-300, 300), ofRandom(-300, 300)), color)); } } //-------------------------------------------------------------- void ofApp::update() { for (auto& walker : this->walker_list) { walker.update(); } } //-------------------------------------------------------------- void ofApp::draw() { ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5); for (auto& walker : this->walker_list) { walker.draw_1(); } for (auto& walker : this->walker_list) { walker.draw_2(); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |