Skip to content

Instantly share code, notes, and snippets.

@jvcleave
Created July 9, 2012 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvcleave/3073502 to your computer and use it in GitHub Desktop.
Save jvcleave/3073502 to your computer and use it in GitHub Desktop.
draw ofSubpaths over time
//.cpp
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
int circleResolution = 6;
circlePolyline.arc(0,0,0,1,1,0,360,circleResolution);
circlePoints.resize(circlePolyline.size());
path.setFilled(false);
for (int i=0; i< 20; i++)
{
for (int j=0; j< 20; j++)
{
drawCircle(i*40, j*40, 0, 20);
}
}
vector<ofSubPath>& subPaths = path.getSubPaths();
for (int i=0; i<subPaths.size(); i++)
{
vector<ofSubPath::Command>& pathCommands = subPaths[i].getCommands();
for (int j=0; j<pathCommands.size(); j++)
{
commands.push_back(pathCommands[j]);
}
}
commandIndex =0;
}
//--------------------------------------------------------------
void testApp::update(){
ofSetWindowTitle(ofToString(ofGetFrameRate()));
if (ofGetFrameNum()%60 == 0)
{
if (commandIndex+1<commands.size())
{
commandIndex++;
}else {
commandIndex = 0;
}
}
polyline.clear();
int curveResolution = ofGetStyle().curveResolution;
//maybe a better way?
ofPath tempPath;
int arcResolution = tempPath.getArcResolution();
for(int i=0; i<commandIndex; i++)
{
switch(commands[i].type)
{
case ofSubPath::Command::lineTo:
polyline.addVertex(commands[i].to);
break;
case ofSubPath::Command::curveTo:
polyline.curveTo(commands[i].to, curveResolution);
break;
case ofSubPath::Command::bezierTo:
polyline.bezierTo(commands[i].cp1,commands[i].cp2,commands[i].to, curveResolution);
break;
case ofSubPath::Command::quadBezierTo:
polyline.quadBezierTo(commands[i].cp1,commands[i].cp2,commands[i].to, curveResolution);
break;
case ofSubPath::Command::arc:
polyline.arc(commands[i].to,commands[i].radiusX,commands[i].radiusY,commands[i].angleBegin,commands[i].angleEnd, arcResolution);
break;
case ofSubPath::Command::arcNegative:
polyline.arcNegative(commands[i].to,commands[i].radiusX,commands[i].radiusY,commands[i].angleBegin,commands[i].angleEnd, arcResolution);
break;
}
}
}
//--------------------------------------------------------------
void testApp::draw(){
ofPushMatrix();
ofSetColor(ofColor::yellow);
ofTranslate(20, 20, 0);
polyline.draw();
ofPopMatrix();
}
void testApp::drawCircle(float x, float y, float z, float radius){
vector<ofPoint> & circleCache = circlePolyline.getVertices();
for(int i=0;i<(int)circleCache.size();i++){
circlePoints[i].set(radius*circleCache[i].x+x,radius*circleCache[i].y+y,z);
if(i==0)
{
path.moveTo(circlePoints[i]);
}else
{
path.lineTo(circlePoints[i]);
}
}
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
//.h
#pragma once
#include "ofMain.h"
class testApp : 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 windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofPolyline circlePolyline;
void drawCircle(float x, float y, float z, float radius);
vector<ofPoint> circlePoints;
ofPath path;
//vector<ofPolyline> paths;
vector<ofSubPath::Command> commands;
int commandIndex;
ofPolyline polyline;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment