[ 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 |
#pragma once #include "ofMain.h" #include "RecuRect.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 windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; RecuRect r_rect; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(0); ofSetWindowTitle("Insta"); ofSetRectMode(ofRectMode::OF_RECTMODE_CENTER); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); ofSetLineWidth(0.3); } //-------------------------------------------------------------- void ofApp::update() { if (ofGetFrameNum() % 180 == 0) { this->r_rect = RecuRect(6, ofVec2f(ofGetWidth() / 2, ofGetHeight() / 2), 720, true); } this->r_rect.update(); } //-------------------------------------------------------------- void ofApp::draw() { r_rect.draw(); } //======================================================================== 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 22 23 24 |
#pragma once #include "ofMain.h" class RecuRect { public: RecuRect(); RecuRect(int level, ofVec2f location, float size, bool origin = false); ~RecuRect(); void update(); void draw(); private: int level; ofVec2f location; float size; int type; bool origin; ofColor color; float alpha; float alpha_vec; std::vector<RecuRect> rects; }; |
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 |
#include "RecuRect.h" RecuRect::RecuRect() { } RecuRect::RecuRect(int level, ofVec2f location, float size, bool origin) { this->type = ofRandom(100); if (origin || (level > 0 && this->type > 20)) { this->rects.push_back(RecuRect(level - 1, ofVec2f(location.x - size / 4, location.y - size / 4), size / 2)); this->rects.push_back(RecuRect(level - 1, ofVec2f(location.x - size / 4, location.y + size / 4), size / 2)); this->rects.push_back(RecuRect(level - 1, ofVec2f(location.x + size / 4, location.y - size / 4), size / 2)); this->rects.push_back(RecuRect(level - 1, ofVec2f(location.x + size / 4, location.y + size / 4), size / 2)); } this->level = level; this->location = location; this->size = size; this->origin = origin; this->color.setHsb(ofRandom(255), 230, 230); this->alpha = 0; this->alpha_vec = ofRandom(2, 5); } RecuRect::~RecuRect() { } void RecuRect::update() { for (int i = 0; i < this->rects.size(); i++) { this->rects[i].update(); } this->alpha += this->alpha_vec; } void RecuRect::draw() { for (int i = 0; i < this->rects.size(); i++) { this->rects[i].draw(); } if (this->type > 60) { ofNoFill(); ofSetColor(255, this->alpha); ofDrawRectangle(this->location, size, size); } if (this->type < 3) { ofFill(); ofSetColor(this->color, this->alpha); ofDrawRectangle(this->location, size, size); } } |