[ Video ]
[ About ]
ぐるぐるウォーク。
Circular walk.
[ 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 |
#pragma once #include "ofMain.h" class Walker { public: Walker(glm::vec2 location, ofColor color); void update(); void change_rotate(); void draw(); 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 |
#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(); } 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 * 10; this->rotate_radius -= 0.25; this->log.push_back(location); if (this->rotate_radius < 0 || ofRandom(100) < 0.5) { this->change_rotate(); } while (this->log.size() > 150) { this->log.erase(this->log.begin()); } } //-------------------------------------------------------------- void Walker::change_rotate() { auto tmp_radius = ofRandom(30, 80); 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() { for (int i = 0; i < this->log.size() - 1; i++) { if (i < this->log.size() * 0.5) { ofSetColor(ofColor(color, ofMap(i, 0, this->log.size() * 0.5, 0, 255))); } else { ofSetColor(ofColor(color)); } ofDrawLine(this->log[i], this->log[i + 1]); } } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(0); ofSetLineWidth(2); vector<ofColor> base_color_list; ofColor color; vector<int> hex_list = { 0x247BA0, 0x70C1B3, 0xB2DBBF, 0xF3FFBD, 0xFF1654 }; for (auto hex : hex_list) { color.setHex(hex); base_color_list.push_back(color); } for (int i = 0; i < 20; i++) { this->walker_list.push_back(Walker(glm::vec2(ofRandom(-300, 300), ofRandom(-300, 300)), base_color_list[ofRandom(base_color_list.size())])); } } //-------------------------------------------------------------- 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(); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |