[ Video ]
[ About ]
円から円へ移動。@ryotakobさんの動画からインスパイアッ!
Move from circle to circle.
[ 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 |
#pragma once #include "ofMain.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) {}; int frame_span; int size; vector<glm::vec2> locations; vector<int> current_indexes; vector<int> next_indexes; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(239); ofSetColor(39); ofSetLineWidth(3); ofNoFill(); this->frame_span = 30; this->size = 50; while (this->locations.size() < 100) { auto location = glm::vec2(ofRandom(ofGetWidth()), ofRandom(ofGetHeight())); bool flag = true; for (int i = 0; i < this->locations.size(); i++) { if (glm::distance(location, this->locations[i]) < this->size * 1.2) { flag = false; break; } } if (flag) { this->locations.push_back(location); } } for (int i = 0; i < 8; i++) { this->current_indexes.push_back(0); this->next_indexes.push_back(ofRandom(this->locations.size())); } } //-------------------------------------------------------------- void ofApp::update() { if (ofGetFrameNum() % this->frame_span == 0) { for (int i = 0; i < this->current_indexes.size(); i++) { this->current_indexes[i] = this->next_indexes[i]; vector<int> near_indexes; for (int location_index = 0; location_index < this->locations.size(); location_index++) { if (this->current_indexes[i] == location_index) { continue; } if (glm::distance(this->locations[this->current_indexes[i]], this->locations[location_index]) < this->size * 3) { near_indexes.push_back(location_index); } } if (near_indexes.size() > 0) { this->next_indexes[i] = near_indexes[ofRandom(near_indexes.size())]; } } } } //-------------------------------------------------------------- void ofApp::draw() { for (int i = 0; i < this->current_indexes.size(); i++) { auto distance = glm::distance(this->locations[this->current_indexes[i]], this->locations[this->next_indexes[i]]); int frame_param = ofGetFrameNum() % this->frame_span; distance = ofMap(frame_param, 0, this->frame_span, 0, distance); ofDrawLine(this->locations[this->current_indexes[i]], this->locations[this->next_indexes[i]]); for (int location_index = 0; location_index < this->locations.size(); location_index++) { ofDrawCircle(this->locations[location_index], this->size * 0.45); if (this->current_indexes[i] == location_index) { ofFill(); ofDrawCircle(this->locations[location_index], ofMap(frame_param, 0, this->frame_span, this->size * 0.45, 0)); ofNoFill(); } if (this->next_indexes[i] == location_index) { ofFill(); ofDrawCircle(this->locations[location_index], ofMap(frame_param, 0, this->frame_span, 0, this->size * 0.45)); ofNoFill(); } } } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |