Skip to content

Instantly share code, notes, and snippets.

@ofZach
Created February 5, 2014 17:53
Show Gist options
  • Save ofZach/8829491 to your computer and use it in GitHub Desktop.
Save ofZach/8829491 to your computer and use it in GitHub Desktop.
smooth line drawing input for openframeworks
#include "testApp.h"
// this can go in your h file;
ofPoint pts[5];
uint ctr;
ofPath path;
ofPolyline lineOrig;
//--------------------------------------------------------------
void testApp::setup(){
ofBackground(0);
ctr = 1;
}
//--------------------------------------------------------------
void testApp::update(){
}
//--------------------------------------------------------------
void testApp::draw(){
path.setFilled(false);
if (path.getOutline().size() > 0){
path.getOutline()[0].draw();
}
ofTranslate(300,0);
lineOrig.draw();
}
//--------------------------------------------------------------
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){
lineOrig.addVertex( ofPoint (x,y));
ofPoint p = ofPoint(x,y);
ctr++;
pts[ctr] = p;
if (ctr == 4)
{
pts[3] = ofPoint((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
if (path.getCommands().size() == 0) path.moveTo(pts[0]);
path.bezierTo(pts[1],pts[2], pts[3]);
pts[0] = pts[3];
pts[1] = pts[4];
ctr = 1;
}
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
ctr = 0;
pts[0] = ofPoint(x,y);
path.clear();
lineOrig.addVertex( ofPoint (x,y));
}
//--------------------------------------------------------------
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){
}
@ofZach
Copy link
Author

ofZach commented Feb 5, 2014

smoother drawing code for OF

comparison: http://i.imgur.com/8XbIqAe.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment