[work 11] Trochoid
Movie
Source code
about
- アームを組み合わせてトロコイドの曲線を描く
- FBOを使って、フレーム毎に更新する部分(アーム)と軌跡を残す部分(トロコイド)を分けて描く
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 *t[3]; };
#include "ofApp.h" ofApp::ofApp(){ t[0] = new Trochoid(ofVec2f(ofGetWidth() / 2, ofGetHeight() / 2), false); t[1] = new Trochoid(t[0], false); t[2] = new Trochoid(t[1], true); } ofApp::~ofApp(){ for (int i = 0; i < 3; i++) { delete t[i]; } } //-------------------------------------------------------------- void ofApp::setup(){ double fps = 30; ofSetFrameRate(fps); ofBackground(255,255,255); ofSetBackgroundAuto(true); for (int i = 0; i < 3; i++) { t[i]->setup(); } } //-------------------------------------------------------------- void ofApp::update(){ for (int i = 0; i < 3; i++) { t[i]->update(); } } //-------------------------------------------------------------- void ofApp::draw(){ for (int i = 0; i < 3; i++) { t[i]->display(); } }
#ifndef Trochoid_hpp #define Trochoid_hpp #include <stdio.h> #include "ofMain.h" #define ARM_MIN (30) #define ARM_MAX (150) #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); 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 ofColor c; c.setHsb(ofMap(ofNoise(time), 0, 1, 0, 255), 255, 255); time += 0.1; locColor = c; 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); }
Link to the API reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
category | API |
openframeworks | ofFbo |
openframeworks | ofGraphics ofClear |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode