[work 44] Fractals – triangles –

[work 44] Fractals – triangles –

Movie

Source code

about

  • 三角形を描く
  • 三角形の各頂点が中心となるように縮小した三角形を描く。これを再帰的に繰り返す。

file

  • 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
  • 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h"
#include "ofApp.h"

//================================
int main( ){

    // 4K:4096x2160
    // 2K:2048x1080
    // FullHD:1920x1080
    // HD:1440x1080
    // HD720p:1280x720
    // DVD:720x480
    // setup the GL context
    ofSetupOpenGL(1280, 720, OF_WINDOW);

	// this kicks off the running of my app
	// can be OF_WINDOW or OF_FULLSCREEN
	// pass in width and height too:
	ofRunApp( new ofApp());

}
#pragma once

#include "ofMain.h"

#include "Fractals.hpp"


class ofApp : public ofBaseApp{
public:
    ofApp();
    ~ofApp();
    
    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);
    
private:
    std::shared_ptr<Fractals> frac;
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    
    ofSetFrameRate(fps);
    ofBackground(0);
    ofSetBackgroundAuto(true);
    ofSetVerticalSync(true);
    
    frac = make_shared<Fractals>(ofVec2f(ofGetWidth() / 2, ofGetHeight() / 2), 150);
    frac->setup();
}

//--------------------------------------------------------------
void ofApp::update(){
    frac->update();
}

//--------------------------------------------------------------
void ofApp::draw(){
    frac->draw();
}
#ifndef Fractals_hpp
#define Fractals_hpp

#include <stdio.h>
#include "ofMain.h"

struct Triangle {
    std::array<ofVec2f, 3> point;
    ofColor color;
};

class  Fractals {
public:
    Fractals(ofVec2f c, float r);
    ~Fractals();
    void setup();
    void update();
    void draw();
private:
    void setTriangle(ofVec2f c, float);
    void updateTriangle();
    
    ofVec2f center;
    float radius;
    
    std::vector<Triangle> tris;
    
    int index;
    shared_ptr<ofFbo> fbo;
};
#endif /* Fractals_hpp */
#include "Fractals.hpp"


Fractals::Fractals(ofVec2f c, float r)
{
    center = c;
    radius = r;
}


Fractals::~Fractals()
{
    
}


void Fractals::setup()
{
    fbo = make_shared<ofFbo>();
    fbo->allocate(ofGetWidth(), ofGetHeight());
    fbo->begin();
    ofClear(0);
    fbo->end();
    
    setTriangle(center, radius);
    index = 0;
}


void Fractals::update()
{
    updateTriangle();
}


void Fractals::draw()
{
    fbo->draw(0, 0);
}


void Fractals::setTriangle(ofVec2f c, float r)
{
    Triangle t;
    ofVec2f dir = ofVec2f(0, -1);
    dir *= r;
    t.point[0] = c + dir;
    t.point[1] = c + dir.rotate(120);
    t.point[2] = c + dir.rotate(120);
    t.color = ofColor(ofRandom(255), ofRandom(255), ofRandom(255));
    
    tris.push_back(t);
    
    if (r > 4) {
        float scale = 0.5;
        setTriangle(t.point[0], r * scale);
        setTriangle(t.point[1], r * scale);
        setTriangle(t.point[2], r * scale);
    }
}


void Fractals::updateTriangle()
{
    for (int i = 0; i < 3; i++) {
        if (index < tris.size()) {
            fbo->begin();
            ofEnableAntiAliasing();
            ofSetColor(tris.at(index).color);
            ofNoFill();
            ofDrawTriangle(tris.at(index).point[0], tris.at(index).point[1], tris.at(index).point[2]);
            fbo->end();
            
            index++;
        }
    }
}

Link to the reference page

ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。

categoryAPI/Lib
openframeworksofDrawTriangle
openframeworksofVec2f rotate

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode