[work 53] Fractals – Snowflake –

[work 53] Fractals – Snowflake –

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(255);
    ofSetBackgroundAuto(true);
    ofSetVerticalSync(true);
    
    frac = make_shared<Fractals>();
    frac->setup();
}

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

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

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


struct kochSnowflake {
    ofVec2f baseDir;
    int ngon;
    float length;
    bool isDead;
};

class  Fractals {
public:
    Fractals();
    ~Fractals();
    void setup();
    void update();
    void display();
    
    static bool shouldRemoveSnowflake(shared_ptr<kochSnowflake> &s);
private:
    void initKoch();
    void setKoch(ofVec2f start, ofVec2f end);
    void updateKoch();
    void drawKoch();
    
    ofFbo fbo;
    
    std::vector<std::shared_ptr<kochSnowflake>> snowflake;
    ofVec2f center;
    float limit;
};
#endif /* Fractals_hpp */
#include "Fractals.hpp"


Fractals::Fractals()
{
    
}


Fractals::~Fractals()
{
    
}


void Fractals::setup()
{
    fbo.allocate(ofGetWidth(), ofGetHeight());
    center = ofVec2f(ofGetWidth() / 2, ofGetHeight() / 2);
    limit = (center - ofVec2f(0, 0)).length();
    
    initKoch();
}


void Fractals::update()
{
    updateKoch();
    
    if ((ofGetFrameNum() != 0) && (ofGetFrameNum() % 30 == 0)) {
        initKoch();
    }
    
    for (std::shared_ptr<kochSnowflake> &k : snowflake) {
        for (int i = 0; i < k->ngon; i++) {
            float rotate = 360.0 / (float)k->ngon;
            ofVec2f base1 = k->baseDir * k->length;
            ofVec2f base2 = base1;
            ofVec2f p1 = base1.rotate(rotate * i);
            ofVec2f p2 = base2.rotate(rotate * (i + 1));
            if ((p1 + p2).length() / 2 > limit) {
                k->isDead = true;
                break;
            }
        }
    }
    
    ofRemove(snowflake, Fractals::shouldRemoveSnowflake);
}


void Fractals::display()
{
    drawKoch();
}


void Fractals::initKoch()
{
    auto s = make_shared<kochSnowflake>();
    s->ngon = (int)ofRandom(3, 9);
    s->baseDir = ofVec2f(0, 1);
    s->length = 10;
    s->isDead = false;
    snowflake.push_back(s);
}


void Fractals::setKoch(ofVec2f start, ofVec2f end)
{
    ofVec2f dir = (end - start);
    float length = dir.length();
    dir.normalize();
    ofVec2f orgDir = dir;
    
    if (length > 9) {
        std::array<ofVec2f, 5> p;
        p.at(0) = start;
        p.at(1) = start + orgDir * (length / 3);
        p.at(2) = p.at(1) + dir.rotate(-60) * (length / 3);
        p.at(3) = start + orgDir * (length * 2 / 3);
        p.at(4) = end;
        
        for (int i = 0; i < p.size() - 1; i++) {
            setKoch(p.at(i), p.at(i + 1));
        }
    } else {
        fbo.begin();
        ofSetColor(0);
        ofNoFill();
        ofSetLineWidth(1);
        ofDrawLine(start, end);
        fbo.end();
    }
}


void Fractals::updateKoch()
{
    fbo.begin();
    ofClear(0);
    fbo.end();
    
    for (std::shared_ptr<kochSnowflake> &k : snowflake) {
        for (int i = 0; i < k->ngon; i++) {
            float rotate = 360.0 / (float)k->ngon;
            ofVec2f base1 = k->baseDir * k->length;
            ofVec2f base2 = base1;
            ofVec2f p1 = center + base1.rotate(rotate * i);
            ofVec2f p2 = center + base2.rotate(rotate * (i + 1));
            setKoch(p1, p2);
        }
        k->length = k->length + 4;
    }
}


void Fractals::drawKoch()
{
    fbo.draw(0, 0);
}



bool Fractals::shouldRemoveSnowflake(shared_ptr<kochSnowflake> &s)
{
    return s->isDead;
}

Link to the reference page

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

categoryAPI/Lib
openframeworksofVec2f

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode