pif_BezierCurve

by Popisfizzy
Generate sample points for drawing Bézier curves.
ID:2069064
 
This project is available on GitHub!

Bézier curves are smooth parametric curves in arbitrary dimensions generated by a collection of n points. Their 'nice' behavior gives them many uses in computer graphics, and on top of that they look quite good too. They can also be naturally transformed by linear maps (i.e., linear transformations or matrix transformations) by simply transforming the n sample points.

Note: At this time, this library provides no means to actually draw the curve or produce an image of the curve, and instead simply outputs a list of points which the programmer must use to draw lines between in order to approximate the curve.

Example

Below is an example of how one could use this library to draw a Bézier curve for nine points with a "smoothness" (see documentation) of 50, where the points are: (0,0), (1,1), (2,2), (3,3), (4,4), (5,3), (6,2), (7,1), (8,0). This is assuming we have a DrawLine() method available that takes two points and draws a line between them.

var
pif_BezierCurve/C = new(
0,0 , 1,1 , 2,2 , 3,3 , 4,4 , 5,3 , 6,2 , 7,1 , 8,0
)
list/Curve

last_x
last_y

C.Smoothness(50)
Curve = C.Curve()

last_x = Curve[1]
last_y = Curve[2]

for(var/i = 1, i < C.Smoothness(), i ++)
var
new_x = Curve[2*i+1]
new_y = Curve[2*i+2]

DrawLine(last_x,last_y , new_x,new_y)

last_x = new_x
last_y = new_y


And below is what this curve looks like, along with the sample points used to generate it.



The MIT License
Copyright (c) 2016-2019 Timothy "popisfizzy" Reilly

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.