[work 50] Rotate quadrangles
![[work 50] Rotate quadrangles](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/06/outFrameImg0450-1.png?fit=1280%2C720&ssl=1)
Movie
Source code
about
- 四角形Aの各辺を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 myRect {
std::array<ofVec2f, 4> rect;
bool locus;
};
class Fractals {
public:
Fractals();
~Fractals();
void setup();
void update();
void display();
private:
ofFbo fbo, fbolocus;
void setRect(myRect r);
std::vector<myRect> rects;
int index;
};
#endif /* Fractals_hpp */
#include "Fractals.hpp"
Fractals::Fractals()
{
}
Fractals::~Fractals()
{
}
void Fractals::setup()
{
fbo.allocate(ofGetWidth(), ofGetHeight());
fbolocus.allocate(ofGetWidth(), ofGetHeight());
index = 0;
ofRectangle base = ofRectangle(0, 0, ofGetWidth(), ofGetHeight());
base.scaleFromCenter(0.8);
myRect mr;
mr.rect.at(0) = base.getTopLeft();
mr.rect.at(1) = base.getTopRight();
mr.rect.at(2) = base.getBottomRight();
mr.rect.at(3) = base.getBottomLeft();
mr.locus = false;
setRect(mr);
}
void Fractals::update()
{
if (index < rects.size()) {
if (rects.at(index).locus) {
fbolocus.begin();
ofClear(0);
} else {
fbo.begin();
}
ofEnableAntiAliasing();
ofSetPolyMode(OF_POLY_WINDING_NONZERO);
ofSetColor(0);
ofNoFill();
ofSetLineWidth(1);
ofBeginShape();
ofVertex(rects.at(index).rect.at(0));
ofVertex(rects.at(index).rect.at(1));
ofVertex(rects.at(index).rect.at(2));
ofVertex(rects.at(index).rect.at(3));
ofEndShape();
if (rects.at(index).locus) {
fbolocus.end();
} else {
fbo.end();
}
index++;
}
}
void Fractals::display()
{
fbolocus.draw(0, 0);
fbo.draw(0, 0);
}
void Fractals::setRect(myRect r)
{
float w = (r.rect.at(0) - r.rect.at(1)).length();
float h = (r.rect.at(1) - r.rect.at(2)).length();
float l = std::max(w, h);
if (l > 10) {
rects.push_back(r);
float step = 0.1;
float stepNum = 10;
myRect tmp;
for (int i = 1; i < stepNum; i++) {
float d = step / stepNum;
tmp.rect.at(0) = r.rect.at(3) * (d * i) + r.rect.at(0) * (1 - (d * i));
tmp.rect.at(1) = r.rect.at(0) * (d * i) + r.rect.at(1) * (1 - (d * i));
tmp.rect.at(2) = r.rect.at(1) * (d * i) + r.rect.at(2) * (1 - (d * i));
tmp.rect.at(3) = r.rect.at(2) * (d * i) + r.rect.at(3) * (1 - (d * i));
tmp.locus = true;
rects.push_back(tmp);
}
tmp.rect.at(0) = r.rect.at(3) * step + r.rect.at(0) * (1 - step);
tmp.rect.at(1) = r.rect.at(0) * step + r.rect.at(1) * (1 - step);
tmp.rect.at(2) = r.rect.at(1) * step + r.rect.at(2) * (1 - step);
tmp.rect.at(3) = r.rect.at(2) * step + r.rect.at(3) * (1 - step);
tmp.locus = false;
setRect(tmp);
}
}
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API/Lib |
| openframeworks | ofRectangle |
| openframeworks | ofFbo |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode