January 24, 2021, 01:23:29 PM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email
?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Home
Forum
Help
Search
Gallery
Login
Register
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Algorithms
»
[bmx] Enumerable bezier curve by Warpy [ 1+ years ago ]
« previous
next »
Print
Pages: [
1
]
Go Down
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
Type
bezier
Field
x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#
Field
inc#
Function
Create:bezier
(
x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#,inc#=-
1
)
b:bezier=
New
bezier
b.x1=x1
b.y1=y1
b.x2=x2
b.y2=y2
b.x3=x3
b.y3=y3
b.x4=x4
b.y4=y4
If
inc=-
1
dx#=x4-x1
dy#=y4-y1
d#=Sqr
(
dx*dx+dy*dy
)
inc=
1
/d
EndIf
b.inc=inc
Return
b
End
Function
Method
objectenumerator:bezierenumerator
(
)
Return
bezierenumerator.Create
(
Self
)
End
Method
Method
pos
(
t#,x#
Var
, y#
Var
)
nt#=
1
-t
x=nt*nt*nt*x1 +
3
*nt*nt*t*x2 +
3
*nt*t*t*x3 + t*t*t*x4
y=nt*nt*nt*y1 +
3
*nt*nt*t*y2 +
3
*nt*t*t*y3 + t*t*t*y4
End
Method
End
Type
Type
bezierenumerator
Field
b:bezier
Field
t#
Function
Create:bezierenumerator
(
b:bezier
)
be:bezierenumerator=
New
bezierenumerator
be.b=b
Return
be
End
Function
Method
HasNext
(
)
If
t<=
1
Return
1
End
Method
Method
NextObject:
Object
(
)
Local
point#
[
2
]
b.pos t,point
[
0
]
,point
[
1
]
If
t=
1
t:+
1
point=
[
b.x4,b.y4
]
Return
point
EndIf
t:+b.inc
If
t>
1
t=
1
Return
point
End
Method
End
Type
'usage
b:bezier = bezier.create
(
0
,
0
,
100
,
100
,
200
,
200
,
300
,
0
)
'you can use EachIn to get all the points along the curve
Local
point#
[
2
]
For
point=
EachIn
b
DrawRect point
[
0
]
,point
[
1
]
,
1
,
1
Next
'or you can do it directly
Local
x#,y#
Local
t#=
0
For
t=
0
to
1
step
0.01
b.pos t,x,y
DrawRect x,y,
1
,
1
Next
Comments :
jsp(Posted 1+ years ago)
This looks very useful!Thanks
Chroma(Posted 1+ years ago)
Awesome thanks!
Logged
Print
Pages: [
1
]
Go Up
« previous
next »
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Algorithms
»
[bmx] Enumerable bezier curve by Warpy [ 1+ years ago ]
SimplePortal 2.3.6 © 2008-2014, SimplePortal