[bb] Bezier Curves v1.2 by Entity [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : Bezier Curves v1.2
Author : Entity
Posted : 1+ years ago

Description : Well.. erm.. this draws a bezier curve :)
v1.2: fixed small memory leak


Code :
Code (blitzbasic) Select
;
; Recursive Bezier Curve Demo (v1.2)
; by Jamie "Entity" van den Berge <entity@vapor.com>
;
 
Type Bezier
Field  sx#,  sy# ; startpoint
Field  ex#,  ey# ; endpoint
Field csx#, csy# ; startpoint's controlpoint
Field cex#, cey# ; endpoint's controlpoint
End Type

Function DrawBezier( depth, b.Bezier )
Local acsx#, acsy#

If depth > 0
Local l.Bezier = New Bezier, r.Bezier = New Bezier
; we haven't reached desired precision level yet,
; so we subdivide into two smaller curves.

lsx = bsx: lsy = bsy
rex = bex: rey = bey

lcsx = (bsx + bcsx) / 2.0: rcex = (bex + bcex) / 2.0
lcsy = (bsy + bcsy) / 2.0: rcey = (bey + bcey) / 2.0

   acsx = (bcsx + bcex) / 2.0 ; X control point average
acsy = (bcsy + bcey) / 2.0 ; Y control point average

lcex = ( lcsx + acsx ) / 2.0 : rcsx = ( rcex + acsx ) / 2.0
        lcey = ( lcsy + acsy ) / 2.0 : rcsy = ( rcey + acsy ) / 2.0

lex  = ( lcex + rcsx ) / 2.0: rsx = lex
ley  = ( lcey + rcsy ) / 2.0: rsy = ley

depth = depth - 1
DrawBezier( depth, l ): Delete l ; subdivide left
DrawBezier( depth, r ): Delete r ; subdivide right
Else
Line bsx, bsy, bex, bey
EndIf
End Function


;---------
; EXAMPLE
;---------

b.Bezier = New Bezier

SeedRnd MilliSecs()

While Not KeyHit( 1 )
; set startpoint to previous end point
bsx = bex: bsy = bey

; set startpoint's controlpoint to previous end point's inverse control point
bcsx = bsx+(bsx-bcex)
bcsy = bsy+(bsy-bcey)

; pick a new endpoint
bex  = Rand( 0, GraphicsWidth()-1 ): bey = Rand( 0, GraphicsHeight()-1 )

; pick a random control point for the new end point.
bcex = Rand( 0, GraphicsWidth()-1 ): bcey = Rand( 0, GraphicsHeight()-1 )

; and draw the curve
DrawBezier( 5, b )

Delay 400
Wend
Delete b
End


Comments : none...