Created
June 29, 2016 15:50
-
-
Save micuat/24e3678b2fb7b85dba8e370fc148cfa5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofMain.h" | |
#include "ofApp.h" | |
//======================================================================== | |
int main( ){ | |
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context | |
// this kicks off the running of my app | |
// can be OF_WINDOW or OF_FULLSCREEN | |
// pass in width and height too: | |
ofRunApp(new ofApp()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
// curve trajectory | |
// adopted from https://github.com/openframeworks/openFrameworks/blob/master/examples/graphics/polygonExample/src/ofApp.cpp | |
//-------------------------------------------------------------- | |
void ofApp::setup(){ | |
path.resize(7); | |
path[0].p.x = 326; | |
path[0].p.y = 209; | |
path[1].p.x = 306; | |
path[1].p.y = 279; | |
path[2].p.x = 265; | |
path[2].p.y = 331; | |
path[3].p.x = 304; | |
path[3].p.y = 383; | |
path[4].p.x = 374; | |
path[4].p.y = 383; | |
path[5].p.x = 418; | |
path[5].p.y = 309; | |
path[6].p.x = 345; | |
path[6].p.y = 279; | |
for (int i = 0; i < path.size(); i++){ | |
path[i].bOver = false; | |
path[i].bBeingDragged = false; | |
path[i].radius = 4; | |
} | |
currentPhase = 0; | |
phaseDelta = 0.25f; | |
} | |
//-------------------------------------------------------------- | |
void ofApp::update(){ | |
currentPhase += phaseDelta * ofGetLastFrameTime(); | |
while(currentPhase > 1) currentPhase -= 1; | |
} | |
//-------------------------------------------------------------- | |
void ofApp::draw(){ | |
ofBackground(255); | |
// re-generate polyline | |
ofPolyline polyline; | |
polyline.addVertex(path[0].p); | |
for(int i = 0; i < path.size(); i++) { | |
polyline.curveTo(path.at(i).p); | |
} | |
polyline.curveTo(path.at(path.size() - 1).p); | |
ofNoFill(); | |
ofSetColor(ofColor::skyBlue); | |
polyline.draw(); | |
ofSetColor(ofColor::steelBlue); | |
ofDrawCircle(polyline.getPointAtPercent(currentPhase), 10); | |
// show a faint the non-curve version of the same polygon: | |
ofEnableAlphaBlending(); | |
ofNoFill(); | |
ofSetColor(0,0,0,80); | |
for (int i = 0; i < path.size(); i++){ | |
if (path[i].bOver == true) ofFill(); | |
else ofNoFill(); | |
ofDrawCircle(path[i].p,4); | |
} | |
ofDisableAlphaBlending(); | |
} | |
//-------------------------------------------------------------- | |
void ofApp::keyPressed(int key){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::keyReleased(int key){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseMoved(int x, int y ){ | |
for (int i = 0; i < path.size(); i++){ | |
ofPoint diff = ofPoint(x, y) - path[i].p; | |
float dist = diff.length(); | |
if (dist < path[i].radius){ | |
path[i].bOver = true; | |
} else { | |
path[i].bOver = false; | |
} | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseDragged(int x, int y, int button){ | |
for (int i = 0; i < path.size(); i++){ | |
if (path[i].bBeingDragged == true){ | |
path[i].p.x = x; | |
path[i].p.y = y; | |
} | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mousePressed(int x, int y, int button){ | |
for (int i = 0; i < path.size(); i++){ | |
ofPoint diff = ofPoint(x, y) - path[i].p; | |
float dist = diff.length(); | |
if (dist < path[i].radius){ | |
path[i].bBeingDragged = true; | |
} else { | |
path[i].bBeingDragged = false; | |
} | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseReleased(int x, int y, int button){ | |
for (int i = 0; i < path.size(); i++){ | |
path[i].bBeingDragged = false; | |
} | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseEntered(int x, int y){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseExited(int x, int y){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::windowResized(int w, int h){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::gotMessage(ofMessage msg){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::dragEvent(ofDragInfo dragInfo){ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
typedef struct { | |
ofPoint p; | |
bool bBeingDragged; | |
bool bOver; | |
float radius; | |
}draggableVertex; | |
class ofApp : public ofBaseApp{ | |
public: | |
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); | |
vector<draggableVertex> path; | |
float currentPhase; | |
float phaseDelta; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment