January 24, 2021, 01:23:29 PM

### Author Topic: [bmx] Enumerable bezier curve by Warpy [ 1+ years ago ]  (Read 518 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bmx] Enumerable bezier curve by Warpy [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Enumerable bezier curve
Author : Warpy
Posted : 1+ years ago

Description : I'm just uploading some things from my 'how to do things' directory.

This code creates an object representing a bezier curve defined by four control points, and an optional position increment value.
Iterating over the object using EachIn gets you all the points along the curve. Or, you can use the pos method to get the position of a point a certain fraction along the curve.

Code :
Code: BlitzMax
1. Type bezier
2.         Field x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#
3.         Field inc#
4.
5.         Function Create:bezier(x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#,inc#=-1)
6.                 b:bezier=New bezier
7.                 b.x1=x1
8.                 b.y1=y1
9.                 b.x2=x2
10.                 b.y2=y2
11.                 b.x3=x3
12.                 b.y3=y3
13.                 b.x4=x4
14.                 b.y4=y4
15.
16.                 If inc=-1
17.                         dx#=x4-x1
18.                         dy#=y4-y1
19.                         d#=Sqr(dx*dx+dy*dy)
20.                         inc=1/d
21.                 EndIf
22.                 b.inc=inc
23.
24.                 Return b
25.         End Function
26.
27.         Method objectenumerator:bezierenumerator()
28.                 Return bezierenumerator.Create(Self)
29.         End Method
30.
31.         Method pos(t#,x# Var, y# Var)
32.                 nt#=1-t
33.                 x=nt*nt*nt*x1 + 3*nt*nt*t*x2 + 3*nt*t*t*x3 + t*t*t*x4
34.                 y=nt*nt*nt*y1 + 3*nt*nt*t*y2 + 3*nt*t*t*y3 + t*t*t*y4
35.         End Method
36. End Type
37.
38. Type bezierenumerator
39.         Field b:bezier
40.         Field t#
41.
42.         Function Create:bezierenumerator(b:bezier)
43.                 be:bezierenumerator=New bezierenumerator
44.                 be.b=b
45.                 Return be
46.         End Function
47.
48.         Method HasNext()
49.                 If t<=1 Return 1
50.         End Method
51.
52.         Method NextObject:Object()
53.                 Local point#[2]
54.                 b.pos t,point[0],point[1]
55.                 If t=1
56.                         t:+1
57.                         point=[b.x4,b.y4]
58.                         Return point
59.                 EndIf
60.                 t:+b.inc
61.                 If t>1 t=1
62.                 Return point
63.         End Method
64. End Type
65.
66. 'usage
67. b:bezier = bezier.create( 0,0, 100,100, 200,200, 300,0 )
68.
69. 'you can use EachIn to get all the points along the curve
70. Local point#[2]
71.
72. For point=EachIn b
73.         DrawRect point[0],point[1],1,1
74. Next
75.
76. 'or you can do it directly
77. Local x#,y#
78. Local t#=0
79.
80. For t=0 to 1 step 0.01
81.         b.pos t,x,y
82.         DrawRect x,y,1,1
83. Next

jsp(Posted 1+ years ago)

This looks very useful!Thanks

Chroma(Posted 1+ years ago)

Awesome thanks!