Ooops
January 26, 2021, 05:58:04 AM

Author Topic: [bb] Arbitrary-Control Bezier Curves by ImaginaryHuman [ 1+ years ago ]  (Read 407 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Arbitrary-Control Bezier Curves
Author : ImaginaryHuman
Posted : 1+ years ago

Description : This deceptively simple code allows for the calculation of a bezier curve with any number of control points, from 3 upwards.

The function calculates one point on a curve. You will need to call it multiple times with a varying `Position`, in the floating point range of 0.0 to 1.0, and then use the points for drawing - e.g. draw straight lines between each point.

Note, one of the only drawbacks besides speed is that the calculation destroys the contents of the arrays of control points, so if you are to calculate many points on a single curve you may want to make a copy of your control-point arrays before calling the function each time.

I have tested it with a 50 control-point bezier curve and made myself a nice spiral :-D With so many control points, the calculation gets much heavier, and thus slower, so it's dubious as to the usefulness of using so many points for a single curve when cubic beziers are usually good enough. However, 6-10 point beziers are quite interesting.

This generic function is about 1.4 times slower than a hand-coded approach to a specific number of control points, mainly due to the loop overhead.

Pass it an array of X coordinates and an array of Y coordinates, which define the control points. The point on the curve is returned in the variables PointX,PointY.

Replace all of the references to `Float` with `Double` for more accurate calculations.


Code :
Code: BlitzBasic
  1. Function CurvePointAny(X:Float[],Y:Float[],NumberOfControls:Int=3,Position:Float,PointX:Float Var,PointY:Float Var)
  2.         'Calculate a point on a bezier curve with any number of control points using single-precision floating point math
  3.         'Requires at least 3 control points with X and Y coordinates and a current position on the curve in the range 0..1
  4.         'Can handle any number of control points >=3, but the more you add the more calculation is required
  5.         'Coordinates are returned in PointX and PointY variables
  6.         'Contents of X and Y arrays will be trashed by the calculations, so create copies if you need to preserve values
  7.  
  8.         'Calculate bezier curve with any number of control points
  9.         Local OuterLoop:Int=NumberOfControls-1                  'Total number of outer-loop iterations
  10.         Local InnerLoop:Int
  11.         While OuterLoop>0
  12.                 InnerLoop=0                                                                     'Start inner loop at 0
  13.                 While InnerLoop<OuterLoop
  14.                         X[InnerLoop]:+((X[InnerLoop+1]-X[InnerLoop])*Position)  'Calculate X point on the curve
  15.                         Y[InnerLoop]:+((Y[InnerLoop+1]-Y[InnerLoop])*Position)  'Calculate Y point on the curve
  16.                         InnerLoop:+1                                                            'Next inner loop
  17.                 Wend
  18.                 OuterLoop:-1                                                                    'Next outer loop
  19.         Wend   
  20.  
  21.         'Copy final position into return variables
  22.         PointX=X[0]
  23.         PointY=Y[0]
  24. End Function


Comments :


ImaginaryHuman(Posted 1+ years ago)

 Someone please move this to the BlitzMax section, sorry ;-D


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal