[ Video ]
[ About ]
円周上のランダムウォーク
Random walk on circumference.
[ 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 "Agent.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) {}; vector<Agent> agents; ofEasyCam cam; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetColor(39); ofNoFill(); ofSetLineWidth(2); ofEnableDepthTest(); } //-------------------------------------------------------------- void ofApp::update() { if (ofGetFrameNum() % 120 == 0) { for (int i = 0; i < 50; i++) { this->agents.push_back(Agent(glm::vec2())); } } for(int i = this->agents.size() - 1; i >= 0; i--){ this->agents[i].update(); if (this->agents[i].is_dead()) { this->agents.erase(this->agents.begin() + i); } } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); for (auto& agent : this->agents) { agent.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 |
#pragma once #include "ofMain.h" class Agent { public: Agent(glm::vec2 location); void update(); void draw(); bool is_dead(); private: glm::vec2 start_location; deque<glm::vec2> log; int life; int radius; int deg; int radius_step; int deg_step; }; |
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 |
#include "Agent.h" //-------------------------------------------------------------- Agent::Agent(glm::vec2 location) { this->start_location = location; this->radius = 10; this->deg = ofRandom(360); this->life = 0; } //-------------------------------------------------------------- void Agent::update() { if (this->life % 15 == 0) { int r = ofRandom(4); switch (r){ case 0: case 3: this->radius_step = 2; this->deg_step = 0; break; case 1: this->radius_step = 0; this->deg_step = 2; break; case 2: this->radius_step = 0; this->deg_step = -2; break; } } else { this->radius += this->radius_step; this->deg += this->deg_step; } auto location = this->start_location + glm::vec2(this->radius * cos(this->deg * DEG_TO_RAD), this->radius * sin(this->deg * DEG_TO_RAD)); this->log.push_front(location); while (this->log.size() > 20) { this->log.pop_back(); } this->life++; } //-------------------------------------------------------------- void Agent::draw() { ofBeginShape(); for (int i = 0; i < this->log.size(); i++) { ofVertex(this->log[i]); } ofEndShape(); } //-------------------------------------------------------------- bool Agent::is_dead() { return this->radius > 150; } |