July 20, 2019, 07:29:22 AM

### Author Topic: Convert lines to bezier curve  (Read 242 times)

• Sr. Member
• Posts: 385
##### Convert lines to bezier curve
« on: April 15, 2019, 09:20:52 AM »
Anyone know how to convert a bunch of lines into a bezier curve?  Basically I want to go from the first image below to the second image.  I know it is possible as I have done it with inkscape, but I want to do it in code.

------------------------------------------------
8 rabbits equals 1 rabbyte.

#### therevills

• Hero Member
• Posts: 552
##### Re: Convert lines to bezier curve
« Reply #1 on: April 15, 2019, 09:38:28 AM »
I've got this little function hidden away in my maths module:

Code: [Select]
`Function Bezier:Float(p1:Float, p2:Float, cp1:Float, cp2:Float, t:Float) Local v:Float = p1 * Pow( (1 - t), 3) + 3 * cp1 * Pow( (1 - t), 2) * t + 3 * cp2 * (1 - t) * Pow(t, 2) + p2 * Pow(t, 3) Return vEndFunction`
and

Code: [Select]
`Function BezierCurve(sx:Float,sy:Float,ex:Float,ey:Float,p1x:Float,p1y:Float,p2x:Float,p2y:Float, t:Float,pointx:Float Var, pointy:Float Var) Local neg:Float =1-t pointx:Float  = sx * (neg) ^ 3 + 3 * p1x * (neg) ^ 2 * t + 3 * p2x * (neg) * t ^ 2 + ex * t ^ 3 pointy:Float  = sy * (neg) ^ 3 + 3 * p1y * (neg) ^ 2 * t + 3 * p2y * (neg) * t ^ 2 + ey * t ^ 3End Function`

#### Steve Elliott

• Hero Member
• Posts: 1839
##### Re: Convert lines to bezier curve
« Reply #2 on: April 15, 2019, 09:55:30 AM »
A long time a go I wrote a Galaxians-type game and I wanted some of the aliens to follow a smooth path, so I wrote a routine that produced lots of points along a bezier curve and read in the data at load time.  The aliens went from point to point.

I've found the game, but not the program to actually produce the bezier curve at the moment...

Looks like therevills has something.
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.1, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
Raspbian Buster, pi4 4Gb RAM,1.5Ghz

#### Derron

• Hero Member
• Posts: 2209
##### Re: Convert lines to bezier curve
« Reply #3 on: April 15, 2019, 09:59:38 AM »
Drawing a bezier curve (or a similar thing like catmull rom splines) is not what the OP wants, he wants to convert data points into a (set of) bezier curve(s).

https://stackoverflow.com/questions/6299019/how-can-i-fit-a-b%C3%A9zier-curve-to-a-set-of-data
https://stackoverflow.com/questions/5525665/smoothing-a-hand-drawn-curve

http://webdocs.cs.ualberta.ca/~graphics/books/GraphicsGems/gems/FitCurves.c

Seems it is not as trivial as drawing the bezier curve once you got the parameters.

bye
Ron