[work 52] Rotate shapes

[work 52] Rotate shapes

Movie

Source code

about

  • ランダムな形状のn角形(n = 3…8)を生成する。
  • n角形の各頂点を0 – (n-1)とする。
  • n角形の図形Aの頂点0-1,1-2,2-3, …(n-1)-0の各線分を9:1に分割する点を頂点とする図形Bを描く
  • 図形Aから図形Bまでの変形の過程をアニメーション
  • これらを再帰的に実行

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 myShape {
    std::vector<ofVec2f> shape;
    ofColor color;
    bool locus;
};

class  Fractals {
public:
    Fractals();
    ~Fractals();
    void setup();
    void update();
    void display();
    void reset();
private:
    ofFbo fbo, fbolocus;
    void setShape(std::shared_ptr<myShape> &r);
    void updateShape();
    
    std::vector<std::shared_ptr<myShape>> shapes;
    int index;
    bool reverse;
};
#endif /* Fractals_hpp */
#include "Fractals.hpp"


Fractals::Fractals()
{
    
}


Fractals::~Fractals()
{
    
}


void Fractals::setup()
{
    fbo.allocate(ofGetWidth(), ofGetHeight());
    fbolocus.allocate(ofGetWidth(), ofGetHeight());
    
    index = 0;
    
    auto ms = make_shared<myShape>();
    
    ofVec2f center = ofVec2f(ofGetWidth() / 2, ofGetHeight() / 2);
    int size = (int)ofRandom(3, 9);
    
    for (int i = 0; i < size; i++) {
        ofVec2f dir = ofVec2f(1, 0);
        dir.rotate(ofRandom(0, 360));
        dir *= ofRandom(100, 300);
        
        ms->shape.push_back(center + dir);
    }
    ms->color = ofColor(0);
    ms->locus = false;
    
    setShape(ms);
}


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


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


void Fractals::reset()
{
    fbolocus.begin();
    ofClear(0);
    fbolocus.end();
    fbo.begin();
    ofClear(0);
    fbo.end();
    
    shapes.clear();
    
    reverse = false;
    index = 0;
    
    auto ms = make_shared<myShape>();
    
    ofVec2f center = ofVec2f(ofGetWidth() / 2, ofGetHeight() / 2);
    int size = (int)ofRandom(3, 9);
    
    for (int i = 0; i < size; i++) {
        ofVec2f dir = ofVec2f(1, 0);
        dir.rotate(ofRandom(0, 360));
        dir *= ofRandom(100, 300);
        
        ms->shape.push_back(center + dir);
    }
    ms->color = ofColor(0);
    ms->locus = false;
    
    setShape(ms);
}


void Fractals::setShape(std::shared_ptr<myShape> &s)
{
    float size = (s->shape.at(0) - s->shape.at(1)).length();
    for (int i = 1; i < s->shape.size() - 1; i++) {
        size = std::min(size, (s->shape.at(i) - s->shape.at(i + 1)).length());
    }
    
    if (size > 10) {
        shapes.push_back(s);
        
        float step = 0.1;
        float stepNum = 10;
        ofVec2f p;
        
        for (int i = 1; i < stepNum; i++) {
            auto tmp_locus = make_shared<myShape>();
            float d = step / stepNum;
            
            p = s->shape.at(s->shape.size() - 1) * (d * i) + s->shape.at(0) * (1 - (d * i));
            tmp_locus->shape.push_back(p);
            for (int j = 0; j < (s->shape.size() - 1); j++) {
                p = s->shape.at(j) * (d * i) + s->shape.at(j + 1) * (1 - (d * i));
                tmp_locus->shape.push_back(p);
            }
            tmp_locus->color = ofColor(0);
            tmp_locus->locus = true;
            shapes.push_back(tmp_locus);
        }
        
        auto tmp_shape = make_shared<myShape>();
        p = s->shape.at(s->shape.size() - 1) * step + s->shape.at(0) * (1 - step);
        tmp_shape->shape.push_back(p);
        for (int j = 0; j < (s->shape.size() - 1); j++) {
            p = s->shape.at(j) * step + s->shape.at(j + 1) * (1 - step);
            tmp_shape->shape.push_back(p);
        }
        tmp_shape->color = ofColor(0);
        tmp_shape->locus = false;
        setShape(tmp_shape);
    }
}


void Fractals::updateShape()
{
    if (index >= 0 && index < shapes.size()) {
        fbolocus.begin();
        ofClear(0);
        fbolocus.end();
        fbo.begin();
        ofClear(0);
        fbo.end();
        
        for (int i = 0; i <= index; i++) {
            if (shapes.at(i)->locus) {
                fbolocus.begin();
                ofClear(0);
            } else {
                fbo.begin();
            }
            
            ofEnableAntiAliasing();
            ofSetPolyMode(OF_POLY_WINDING_NONZERO);
            ofSetColor(shapes.at(i)->color);
            ofNoFill();
            ofSetLineWidth(1);
            ofBeginShape();
            for (ofVec2f p : shapes.at(i)->shape) {
                ofVertex(p);
            }
            ofEndShape();
            
            if (shapes.at(i)->locus) {
                fbolocus.end();
            } else {
                fbo.end();
            }
        }
        if (reverse) {
            index--;
        } else {
            index++;
        }
    } else if (index < 0) {
        reverse = false;
        index = 0;
    } else {
        reverse = true;
        index = shapes.size() - 1;
    }
}

Link to the reference page

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

categoryAPI/Lib
openframeworksofVertex
openframeworksofFbo
c++std::shared_ptr

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode