Implementing a scribbler algorithm

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/.

How to do it…

We will implement an application illustrating scribbler. Perform the following steps to do so:

  1. Include the necessary headers:
    #include<vector>
  2. Add properties to your main application class:
    vector <Vec2f> mPath;
    float mMaxDist;
    ColorA mColor;
    bool mDrawPath;
  3. Implement the 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() );
    }
  4. Since the drawing will be made with the mouse, it is necessary to use the mouseDown and mouseUp events. Implement these methods, as follows:
    void MainApp::mouseDown( MouseEvent event )
    {
      mDrawPath = true;
    }
    
    void MainApp::mouseUp( MouseEvent event )
    {
      mDrawPath = false;
    }
  5. Finally, the implementation of drawing methods looks like the following code snippet:
    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));
            }
        }
    }

How it works…

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.

How it works…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.185.87