January 26, 2021, 05:41:41 AM

### Author Topic: [bb] Bezier Curves v1.2 by Entity [ 1+ years ago ]  (Read 588 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Bezier Curves v1.2 by Entity [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
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
1. ;
2. ; Recursive Bezier Curve Demo (v1.2)
3. ; by Jamie "Entity" van den Berge <entity@vapor.com>
4. ;
5.
6. Type Bezier
7.         Field  sx#,  sy#        ; startpoint
8.         Field  ex#,  ey#        ; endpoint
9.         Field csx#, csy#        ; startpoint's controlpoint
10.         Field cex#, cey#        ; endpoint's controlpoint
11. End Type
12.
13. Function DrawBezier( depth, b.Bezier )
14.         Local acsx#, acsy#
15.
16.         If depth > 0
17.                 Local l.Bezier = New Bezier, r.Bezier = New Bezier
18.                 ; we haven't reached desired precision level yet,
19.                 ; so we subdivide into two smaller curves.
20.
21.                 lsx = bsx: lsy = bsy
22.                 rex = bex: rey = bey
23.
24.                 lcsx = (bsx + bcsx) / 2.0: rcex = (bex + bcex) / 2.0
25.                 lcsy = (bsy + bcsy) / 2.0: rcey = (bey + bcey) / 2.0
26.
27.             acsx = (bcsx + bcex) / 2.0  ; X control point average
28.                 acsy = (bcsy + bcey) / 2.0      ; Y control point average
29.
30.                 lcex = ( lcsx + acsx ) / 2.0 : rcsx = ( rcex + acsx ) / 2.0
31.         lcey = ( lcsy + acsy ) / 2.0 : rcsy = ( rcey + acsy ) / 2.0
32.
33.                 lex  = ( lcex + rcsx ) / 2.0: rsx = lex
34.                 ley  = ( lcey + rcsy ) / 2.0: rsy = ley
35.
36.                 depth = depth - 1
37.                 DrawBezier( depth, l ): Delete l        ; subdivide left
38.                 DrawBezier( depth, r ): Delete r        ; subdivide right
39.         Else
40.                 Line bsx, bsy, bex, bey
41.         EndIf
42. End Function
43.
44.
45. ;---------
46. ; EXAMPLE
47. ;---------
48.
49. b.Bezier = New Bezier
50.
51. SeedRnd MilliSecs()
52.
53. While Not KeyHit( 1 )
54.         ; set startpoint to previous end point
55.         bsx = bex: bsy = bey
56.
57.         ; set startpoint's controlpoint to previous end point's inverse control point
58.         bcsx = bsx+(bsx-bcex)
59.         bcsy = bsy+(bsy-bcey)
60.
61.         ; pick a new endpoint
62.         bex  = Rand( 0, GraphicsWidth()-1 ): bey = Rand( 0, GraphicsHeight()-1 )
63.
64.         ; pick a random control point for the new end point.
65.         bcex = Rand( 0, GraphicsWidth()-1 ): bcey = Rand( 0, GraphicsHeight()-1 )
66.
67.         ; and draw the curve
68.         DrawBezier( 5, b )
69.
70.         Delay 400
71. Wend
72. Delete b
73. End