In this recipe, we are going to implement a scribbler algorithm, which is very simple to implement using Cinder but gives an interesting effect while drawing. You can read more about the concept of connecting neighbor points at http://www.zefrank.com/scribbler/about.html. You can find an example of scribbler at http://www.zefrank.com/scribbler/ or http://mrdoob.com/projects/harmony/.
We will implement an application illustrating scribbler. Perform the following steps to do so:
#include<vector>
vector <Vec2f> mPath; float mMaxDist; ColorA mColor; bool mDrawPath;
setup
method, as follows:void MainApp::setup() { mDrawPath = false; mMaxDist = 50.f; mColor = ColorA(0.3f,0.3f,0.3f, 0.05f); setWindowSize(800, 600); gl::enableAlphaBlending(); gl::clear( Color::white() ); }
mouseDown
and mouseUp
events. Implement these methods, as follows:void MainApp::mouseDown( MouseEvent event ) { mDrawPath = true; } void MainApp::mouseUp( MouseEvent event ) { mDrawPath = false; }
void MainApp::draw(){ if( mDrawPath ) { drawPoint( getMousePos() ); } } void MainApp::drawPoint(Vec2f point) { mPath.push_back( point ); gl::color(mColor); vector<Vec2f>::iterator it; for(it = mPath.begin(); it != mPath.end(); ++it) { if( (*it).distance(point) <mMaxDist ) { gl::drawLine(point, (*it)); } } }
While the left mouse button is down, we are adding a new point to our container and drawing lines connecting it with other points near it. The distance between the newly-added point and the points in its neighborhood we are looking for to draw a connection line has to be less than the value of the mMaxDist
property. Please notice that we are clearing the drawing area only once, at the program startup at the end of the setup
method, so we don't have to redraw all the connections to each frame, which would be very slow.
18.226.185.87