[work 14] ellipses

[work 14] ellipses

Movie

Source code

about

  • アームを3つ組み合わせる
  • それぞれのアームの長さ、回転角速度はランダム
  • アームの先端を中心とした楕円を描く

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"


#define TROCHOID_ARM_MAX (3)

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[TROCHOID_ARM_MAX];
    float angle;
};
#include "ofApp.h"


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

ofApp::~ofApp(){
    for (int i = 0; i < TROCHOID_ARM_MAX; i++) {
        delete t1[i];
    }
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    ofSetFrameRate(fps);
    ofBackground(0,0,0);
    ofSetBackgroundAuto(true);
    
    for (int i = 0; i < TROCHOID_ARM_MAX; i++) {
        t1[i]->setDraw(true);
        t1[i]->setup();
    }
    angle = 0;
}

//--------------------------------------------------------------
void ofApp::update(){
    angle += 0.007;
    ofVec2f org = ofVec2f(ofGetWidth() / 2 + (std::cos(angle) * 150), ofGetHeight() / 2);
    t1[0]->updateOrg(org);
    
    for (int i = 0; i < TROCHOID_ARM_MAX; i++) {
        t1[i]->update();
        
        if (ofGetFrameNum() > 600) {
            t1[i]->setDraw(false);
        }
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    if (ofGetFrameNum() < 600) {
        ofSetColor(255);
        ofDrawLine((ofGetWidth() / 2) - 150, ofGetHeight() / 2, (ofGetWidth() / 2) + 150, ofGetHeight() / 2);
    }
    
    for (int i = 0; i < TROCHOID_ARM_MAX; i++) {
        t1[i]->display();
    }
}
#ifndef Trochoid_hpp
#define Trochoid_hpp

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


#define ARM_MIN  (50)
#define ARM_MAX  (100)
#define ARM_ANGLE_V_MIN (0.01)
#define ARM_ANGLE_V_MAX (0.15)

class Trochoid {
public:
    Trochoid(Trochoid *p, bool draw);
    Trochoid(ofVec2f org, bool draw);
    ~Trochoid();
    ofVec2f getLocation();
    void setup();
    void setDraw(bool arm);
    void update();
    void updateOrg(ofVec2f org);
    void display();
private:
    Trochoid();
    
    bool drawCurve;
    bool drawArm;
    ofVec2f origin;
    ofVec2f location;
    ofVec2f preLocation;
    float armLength;
    float armAngle;
    float armAngleVelocity;
    Trochoid *parent;
    ofFbo f1;
    ofFbo f2;
    ofColor armColor;
    ofColor locColor;
    
    float time1;
    float time2;
    float time3;
};
#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(ARM_ANGLE_V_MIN, ARM_ANGLE_V_MAX);
    std::cout << "armAngleVelocity: " << armAngleVelocity << std::endl;
    
    f1.allocate(ofGetWidth(), ofGetHeight());
    f2.allocate(ofGetWidth(), ofGetHeight());
    
    armColor = ofColor(255);
    locColor = ofColor(0);
    
    time1 = ofRandom(10000);
    time2 = ofRandom(10000);
    time3 = ofRandom(10000);
}


void Trochoid::update()
{
    if (parent != nullptr) {
        updateOrg(parent->getLocation());
    }
    preLocation = location;
    
    armAngle += armAngleVelocity;
    location.x = origin.x + armLength * std::cos(armAngle);
    location.y = origin.y + armLength * std::sin(armAngle);
    
    if (drawCurve) {
        // draw trochoid curve
        f2.begin();
        
        ofNoFill();
        float h = ofMap(ofNoise(time1), 0, 1, 0, 255);
        float a = ofMap(ofNoise(time1), 0, 1, 64, 255);
        time1 += 0.02;
        // locColor.setHsb(h, 0, 255, a);   // White
        locColor.setHsb(h, 255, 255, a);  // Color
        ofSetColor(locColor);
        float width = ofMap(ofNoise(time2), 0, 1, 5, 300);
        float height = ofMap(ofNoise(time3), 0, 1, 5, 300);
        ofDrawEllipse(location.x, location.y, width, height);
        
        time2 += 0.03;
        time3 += 0.01;
        
        f2.end();
    }
    
    if (drawArm) {
        // 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();
    } else {
        f1.begin();
        ofClear(0);
        f1.end();
    }
}


void Trochoid::updateOrg(ofVec2f org)
{
    origin = org;
}


void Trochoid::setDraw(bool arm)
{
    drawArm = arm;
}


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

Link to the API reference page

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

categoryAPI
openframeworksofFbo
openframeworksofGraphics ofDrawEllipse

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode