[ Video ]
[ About ]
色のランダムウォーク。
Random walk of Colors.
[ 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 |
#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> location_list; vector<ofColor> base_color_list; vector<int> current_index_list; vector<int> next_index_list; vector<ofColor> color_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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(0); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); this->frame_span = 30; this->size = 15; while (this->location_list.size() < 120) { auto location = glm::vec2(ofRandom(80, ofGetWidth() - 80), ofRandom(80, ofGetHeight() - 80)); bool flag = true; for (int i = 0; i < this->location_list.size(); i++) { if (glm::distance(location, this->location_list[i]) < this->size * 3) { flag = false; break; } } if (flag) { this->location_list.push_back(location); } } ofColor color; vector<int> hex_list = { 0xf44336, 0x9C27B0, 0x3F51B5, 0x03A9F4, 0x009688, 0x8BC34A, 0xFFEB3B, 0xFF9800 }; for (auto hex : hex_list) { color.setHex(hex); this->base_color_list.push_back(color); } for (int i = 0; i < 90; i++) { this->current_index_list.push_back(0); this->next_index_list.push_back(ofRandom(this->location_list.size())); this->color_list.push_back(this->base_color_list[ofRandom(this->base_color_list.size())]); } } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->current_index_list.size(); i++) { if (ofGetFrameNum() % (this->frame_span * 2) == 0) { int next_index = this->next_index_list[i]; int current_index = next_index; vector<int> near_indexes; for (int location_index = 0; location_index < this->location_list.size(); location_index++) { if (current_index == location_index) { continue; } if (glm::distance(this->location_list[current_index], this->location_list[location_index]) < this->size * 5) { near_indexes.push_back(location_index); } } if (near_indexes.size() > 0) { next_index = near_indexes[ofRandom(near_indexes.size())]; } this->current_index_list[i] = current_index; this->next_index_list[i] = next_index; } } } //-------------------------------------------------------------- void ofApp::draw() { int frame_param = ofGetFrameNum() % this->frame_span; for (int i = 0; i < this->next_index_list.size(); i++) { ofSetColor(this->color_list[i]); int current_index = this->current_index_list[i]; int next_index = this->next_index_list[i]; auto angle_current = std::atan2(this->location_list[next_index].y - this->location_list[current_index].y, this->location_list[next_index].x - this->location_list[current_index].x); auto satellite_point_current = this->location_list[current_index] + glm::vec2(this->size * cos(angle_current), this->size * sin(angle_current)); auto angle_next = std::atan2(this->location_list[current_index].y - this->location_list[next_index].y, this->location_list[current_index].x - this->location_list[next_index].x); auto satellite_point_next = this->location_list[next_index] + glm::vec2(this->size * cos(angle_next), this->size * sin(angle_next)); if (ofGetFrameNum() % (this->frame_span * 2) < this->frame_span) { ofSetLineWidth(3); auto distance = glm::distance(satellite_point_next, satellite_point_current); distance = ofMap(frame_param, 0, this->frame_span, 0, distance); auto direction = satellite_point_next - satellite_point_current; ofDrawLine(satellite_point_current, satellite_point_current + glm::normalize(direction) * distance); ofSetLineWidth(1.5); for (int location_index = 0; location_index < this->location_list.size(); location_index++) { ofNoFill(); ofSetColor(255); ofDrawCircle(this->location_list[location_index], this->size); if (current_index == location_index) { int deg_param = ofMap(frame_param, 0, this->frame_span, 180, 0); float start_deg = angle_current * RAD_TO_DEG; ofFill(); ofSetColor(this->color_list[i]); ofBeginShape(); for (int deg = start_deg; deg <= start_deg + deg_param; deg++) { ofVertex(this->location_list[location_index] + glm::vec2(this->size * cos(deg * DEG_TO_RAD), this->size * sin(deg * DEG_TO_RAD))); } for (int deg = start_deg - deg_param; deg <= start_deg; deg++) { ofVertex(this->location_list[location_index] + glm::vec2(this->size * cos(deg * DEG_TO_RAD), this->size * sin(deg * DEG_TO_RAD))); } ofEndShape(); } if (next_index == location_index) { int deg_param = ofMap(frame_param, 0, this->frame_span, 0, 90); float start_deg = angle_next * RAD_TO_DEG; ofFill(); ofSetColor(this->color_list[i]); ofBeginShape(); for (int deg = start_deg; deg <= start_deg + deg_param; deg++) { ofVertex(this->location_list[location_index] + glm::vec2(this->size * cos(deg * DEG_TO_RAD), this->size * sin(deg * DEG_TO_RAD))); } for (int deg = start_deg - deg_param; deg <= start_deg; deg++) { ofVertex(this->location_list[location_index] + glm::vec2(this->size * cos(deg * DEG_TO_RAD), this->size * sin(deg * DEG_TO_RAD))); } ofEndShape(); } } } else { ofSetLineWidth(3); auto distance = glm::distance(satellite_point_next, satellite_point_current); distance = ofMap(frame_param, 0, this->frame_span, distance, 0); auto direction = satellite_point_next - satellite_point_current; ofDrawLine(satellite_point_next, satellite_point_next - glm::normalize(direction) * distance); ofSetLineWidth(1.5); for (int location_index = 0; location_index < this->location_list.size(); location_index++) { ofSetColor(255); ofNoFill(); ofDrawCircle(this->location_list[location_index], this->size); if (next_index == location_index) { int deg_param = ofMap(frame_param, 0, this->frame_span, 90, 180); float start_deg = angle_next * RAD_TO_DEG; ofFill(); ofSetColor(this->color_list[i]); ofBeginShape(); for (int deg = start_deg; deg <= start_deg + deg_param; deg++) { ofVertex(this->location_list[location_index] + glm::vec2(this->size * cos(deg * DEG_TO_RAD), this->size * sin(deg * DEG_TO_RAD))); } for (int deg = start_deg - deg_param; deg <= start_deg; deg++) { ofVertex(this->location_list[location_index] + glm::vec2(this->size * cos(deg * DEG_TO_RAD), this->size * sin(deg * DEG_TO_RAD))); } ofEndShape(); } } } } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |