0

Through the internet I found the quadratic beizer curve by given three control points. But what should I do if I have a set of points?

x = (1 - t) * (1 - t) * p[0].x + 2 * (1 - t) * t * p[1].x + t * t * p[2].x;

y = (1 - t) * (1 - t) * p[0].y + 2 * (1 - t) * t * p[1].y + t * t * p[2].y;

In my program, I will have a touch event, when user touches it and move the finger around the screen, the program starts to draw curve.

If I am doing Android I can use quadTo but I can't do it that way coz I want to do it in many platform

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
LittleFunny
  • 8,155
  • 15
  • 87
  • 198
  • use at least cubic BEZIERs they are much better for joining ... see [How can i produce multi point linear interpolation?](http://stackoverflow.com/a/30438865/2521214) and the sub-links... – Spektre May 20 '16 at 11:12
  • even easier is to use Catmull-Rom curves, which are [related to cubic Beziers](http://pomax.github.io/bezierinfo/#catmullconv), but can be specified purely using on-curve points, rather than needing the explicit control points that Beziers do. – Mike 'Pomax' Kamermans May 20 '16 at 15:50
  • This question mixes issues of wanting to have smooth touch motions, and a spesific solution that uses beziers - which might not even be a good solution in this case (perhaps better to ask 2 different questions?). – ideasman42 May 20 '16 at 19:13

1 Answers1

0

For this you will need to use a curve fitting algorithm, this is typcally done iteratively using a least square solution.

A common reference for this is graphics-gems FitCurve.c, see:

https://github.com/erich666/GraphicsGems/blob/master/gems/FitCurves.c

While this is a good reference its not so usable as a library function since its using static vars everywhere.


I've extracted open-toonz curve fitting (also based on FitCurve.c) into a stand-alone, single file C library - which can calculate the curve.
https://developer.blender.org/diffusion/B/browse/master/extern/curve_fit_nd/intern/curve_fit_cubic.c
(has various improvements, see header for details)

ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • alternatively, forget the hard solutions and simply joining up the points by creating a Catmull-Rom curve through them (assuming android is smart enough to pass you 'the points you need' instead of all points. If it gives all points, something like reduce the points drawn using [Ramer–Douglas–Peucker](https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm) will fix that problem) – Mike 'Pomax' Kamermans May 20 '16 at 15:48
  • Right, curve fitting bezier curves is quite a heavy weight solution just for curve smoothing. I just posted this answer because the question asks specifically about bezier curves. – ideasman42 May 20 '16 at 19:11