I created and released my first cinder block and wanted to put it out there to see if it’s useful to anyone else.
https://github.com/gregkepler/CinderPathFitter
What it does is simplifies and returns a smoothed out, simplified Path2d object based on a vector of Vec2fs that you pass to it. It uses the same algorithm that paperjs uses, but c++-ified. It’s a slightly modified version of An Algorithm for Automatically Fitting Digitized Curves from “Graphic Gems.”
There are 2 samples in there.
- PathSimplification: Drag to draw a line on the screen. Once the line is finished, it analyzes and smooths out the line, letting you know how many lines you saved. It’s based on this paperjs example.
- ContinuousPath: Analyzes and smooths a path as you draw it. Could be good for making a drawing application.
To use it, all you have to do is collect a vector of Vec2f and then call FitCurve on PathFitter and pass along that vector.
PathFitter::FitCurve(mPoints, startIndex, endIndex, 2.0);
- mPoints is the Vec2f vector.
- startIndex is the starting index of the points that you want to smooth out.
- Likewise, endIndex is the index of the point that you want to smooth the points out to.
- The last parameter refers to the error value, meaning how close to the points you want the curves to stick to. A greater number looks a bit more organic, but is less accurate.
- What will be returned is a Path2d object that you can go on and draw immediately.
I took this as far as I could for the time being, but being a c++ and cinder noob, I’m open to any suggestions to make this better. Feel free to let me know where my c++ could be improved too.
My next entry will be a more detailed account of what I’ve learned to be able to drawn smooth lines on screen. So look out for that exciting post. Is that a cliffhanger or what?