[work 12] Trochoid x 4

[work 12] Trochoid x 4

Movie

Source code

about

  • トロコイドを4つ同時に描画する。

file

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

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

    // 4K:4096x2160
    // 2K:2048x1080
    // FullHD:1920x1080
    // HD:1440x1080
    // HD720p:1280x720
    // DVD:720x480
	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 <random>

#include "Trochoid.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:
    Trochoid *t1[3];
    Trochoid *t2[3];
    Trochoid *t3[3];
    Trochoid *t4[3];
};
#include "ofApp.h"


ofApp::ofApp(){
    
    t1[0] = new Trochoid(ofVec2f(ofGetWidth() / 4, ofGetHeight() / 4), false);
    t1[1] = new Trochoid(t1[0], false);
    t1[2] = new Trochoid(t1[1], true);

    t2[0] = new Trochoid(ofVec2f(ofGetWidth() * 3 / 4, ofGetHeight() / 4), false);
    t2[1] = new Trochoid(t2[0], false);
    t2[2] = new Trochoid(t2[1], true);
    
    t3[0] = new Trochoid(ofVec2f(ofGetWidth() * 3 / 4, ofGetHeight() * 3 / 4), false);
    t3[1] = new Trochoid(t3[0], false);
    t3[2] = new Trochoid(t3[1], true);
    
    t4[0] = new Trochoid(ofVec2f(ofGetWidth() / 4, ofGetHeight() * 3 / 4), false);
    t4[1] = new Trochoid(t4[0], false);
    t4[2] = new Trochoid(t4[1], true);
}

ofApp::~ofApp(){
    for (int i = 0; i < 3; i++) {
        delete t1[i];
        delete t2[i];
        delete t3[i];
        delete t4[i];
    }
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    ofSetFrameRate(fps);
    ofBackground(255,255,255);
    ofSetBackgroundAuto(true);
    
    for (int i = 0; i < 3; i++) {
        t1[i]->setup();
        t2[i]->setup();
        t3[i]->setup();
        t4[i]->setup();
    }
}

//--------------------------------------------------------------
void ofApp::update(){
    for (int i = 0; i < 3; i++) {
        t1[i]->update();
        t2[i]->update();
        t3[i]->update();
        t4[i]->update();
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    for (int i = 0; i < 3; i++) {
        t1[i]->display();
        t2[i]->display();
        t3[i]->display();
        t4[i]->display();
    }
}
#ifndef Trochoid_hpp
#define Trochoid_hpp

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


#define ARM_MIN  (15)
#define ARM_MAX  (60)
#define ANGLE_V_MAX (0.2)

class Trochoid {
public:
    Trochoid(Trochoid *p, bool draw);
    Trochoid(ofVec2f org, bool draw);
    ~Trochoid();
    ofVec2f getLocation();
    void setup();
    void update();
    void display();
private:
    Trochoid();
    
    bool drawCurve;
    ofVec2f origin;
    ofVec2f location;
    ofVec2f preLocation;
    float armLength;
    float armAngle;
    float armAngleVelocity;
    Trochoid *parent;
    ofFbo f1;
    ofFbo f2;
    ofColor armColor;
    ofColor locColor;
    
    float time;
};
#endif /* Trochoid_hpp */
#include "Trochoid.hpp"


Trochoid::Trochoid()
{
    
}


Trochoid::Trochoid(ofVec2f org, bool draw)
{
    drawCurve = draw;
    
    origin = org;
    parent = nullptr;
    
    armLength = ofRandom(ARM_MIN, ARM_MAX);
    armAngle = 0;
    location.x = origin.x + armLength * std::cos(armAngle);
    location.y = origin.y + armLength * std::sin(armAngle);
}


Trochoid::Trochoid(Trochoid *p, bool draw)
{
    drawCurve = draw;
    
    parent = p;
    origin = parent->getLocation();
    
    armLength = ofRandom(ARM_MIN, ARM_MAX);
    armAngle = 0;
    location.x = origin.x + armLength * std::cos(armAngle);
    location.y = origin.y + armLength * std::sin(armAngle);
}


Trochoid::~Trochoid()
{
    
}


ofVec2f Trochoid::getLocation()
{
    return location;
}


void Trochoid::setup()
{
    armAngleVelocity = ofRandom(ANGLE_V_MAX);
    f1.allocate(ofGetWidth(), ofGetHeight());
    f2.allocate(ofGetWidth(), ofGetHeight());
    
    armColor = ofColor(0, 0, 0);
    locColor = ofColor(ofRandom(255), ofRandom(255), ofRandom(255));
    time = 0;
}


void Trochoid::update()
{
    if (parent != nullptr) {
        origin = parent->getLocation();
    }
    preLocation = location;
    
    armAngle += armAngleVelocity;
    location.x = origin.x + armLength * std::cos(armAngle);
    location.y = origin.y + armLength * std::sin(armAngle);
    
    // draw arm
    f1.begin();
    ofClear(0);
    ofSetColor(armColor);
    ofFill();
    ofDrawCircle(origin.x, origin.y, 5);
    ofDrawLine(origin.x, origin.y, location.x, location.y);
    ofDrawCircle(location.x, location.y, 5);
    f1.end();
    
    if (drawCurve) {
        // draw trochoid curve
        f2.begin();
        ofNoFill();
        ofSetColor(locColor);
        ofDrawLine(preLocation.x, preLocation.y, location.x, location.y);
        f2.end();
    }
}


void Trochoid::display()
{
    f1.draw(0, 0);
    f2.draw(0, 0);
}

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode