Dark Bit Factory & Gravity
PROGRAMMING => Other languages => Blitz => Topic started by: mike_g on July 10, 2008
-
I'm working on a spline editor in Java atm. I made an effect with lines following a load of randomly generated lines. Looks like scribbling at the moment. Hopefully It should look better when the paths are laid out in a nice pattern. Cheers.
;******************************************************************;
;----------------------- DATA TYPES REQUIRED ----------------------;
;******************************************************************;
Const MAX_SPLINE_SIZE = 100
Const CURVE_DETAIL = 300
Type point
Field x, y
End Type
Type spline
Field size
Field pts.point[MAX_SPLINE_SIZE]
End Type
;Temp array for holding points used in spline constructor
Dim temp.point(MAX_SPLINE_SIZE)
Const RUNNER_SIZE = 12
Type runner
Field x, y
Field s.spline
Field pos[RUNNER_SIZE]
Field loc
End Type
;******************************************************************;
;------------------------- FUNCTIONS USED -------------------------;
;******************************************************************;
Function NewPoint.point(x, y)
Local p.point = New point
p\x = x
p\y = y
Return p
End Function
Function NewSpline.spline()
Local s.spline = New spline
Local i = 0
While(temp.point(i) <> Null)
s\pts.point[i] = temp.points(i)
i = i + 1
If(i = MAX_SPLINE_SIZE) Then Exit
Wend
s\size = i
Return s
End Function
Function DrawSpline(s.spline)
i=0
While(s\pts.point[i+2] <> Null)
BezierCurve(s\pts.point[i], s\pts.point[i+1], s\pts.point[i+2])
i=i+2
Wend
End Function
Function BezierCurve(p1.point, p2.point, p3.point)
For i = 0 To CURVE_DETAIL
ptx1 = p1\x+scroll_x: pty1 = p1\y+scroll_y
ptx2 = p2\x+scroll_x: pty2 = p2\y+scroll_y
ptx3 = p3\x+scroll_x: pty3 = p3\y+scroll_y
x1 = QuadraticBezier(i-1, ptx1, ptx2, ptx3)
y1 = QuadraticBezier(i-1, pty1, pty2, pty3)
x2 = QuadraticBezier(i , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(i , pty1, pty2, pty3)
Line x1, y1, x2, y2
Next
End Function
Function QuadraticBezier(Perc#,p1#,p2#,p3#)
d1#=(((p2-p1)*perc)/CURVE_DETAIL)+p1
d2#=(((p3-p2)*perc)/CURVE_DETAIL)+p2
d3#=(((d2-d1)*perc)/CURVE_DETAIL)+d1
Return d3
End Function
Function CubicBezier(perc#,p1#,p2#,p3#,p4#)
d1#=(((p2-p1)*perc)/CURVE_DETAIL)+p1
d2#=(((p3-p2)*perc)/CURVE_DETAIL)+p2
d3#=(((p4-p3)*perc)/CURVE_DETAIL)+p3
d4#=QuadraticBezier(perc,d1,d2,d3)
Return d4
End Function
;******************************************************************;
;--------------------------- TEST PROGRAM -------------------------;
;******************************************************************;
Const VIDEO_MODE = 2
Graphics 1024,768,32, VIDEO_MODE
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Const PALETTE_SIZE = 512
Dim palette(PALETTE_SIZE)
Local rd#, g#, b#
For i=0 To PALETTE_SIZE
rd#=rd#+0.7: If(rd# > 255)r#=255
g#=g#+1.2: If(g# > 255)g#=255
b#=b#+0.1: If(b# > 255)b#=255
palette(i) = Int(rd) Shl 16 + Int(g) Shl 8 + Int(b)
Next
For a = 1 To 10
For b = 0 To 10
temp.point(b) = NewPoint(Rand(0, 1023), Rand(0, 767))
Next
s.spline = NewSpline()
Next
For s.spline = Each spline
f.runner = CreateRunner(s)
f\loc = Rand(0, 8) Mod 2
Next
Global hold = -1 ;indicates which point is held
Global scroll_x=0, scroll_y=0
While Not KeyHit(1)
;Cls
Runner()
Flip
Wend
;********************************************************************;
;--------------------------- EXTRA FUNCTIONS ------------------------;
;********************************************************************;
Function CreateRunner.runner(s.spline)
Local n.runner = New runner
n\x = s\pts.points[0]\x
n\y = s\pts.points[0]\y
n\s = s
n\loc = 0
For i = 0 To RUNNER_SIZE
n\pos[i]=i
Next
Return n
End Function
Function Runner()
For r.runner = Each runner
DrawRunner(r)
Next
End Function
Function DrawRunner(r.runner)
overlap = False
For i= 0 To RUNNER_SIZE-1
rd = rd+10
gr = gr+8
bl = bl+5
If overlap = False
If r\pos[i] < r\pos[i+1]
ptx1 = r\s\pts[r\loc]\x: pty1 = r\s\pts[r\loc]\y
ptx2 = r\s\pts[r\loc+1]\x: pty2 = r\s\pts[r\loc+1]\y
ptx3 = r\s\pts[r\loc+2]\x: pty3 = r\s\pts[r\loc+2]\y
If i < RUNNER_SIZE-1
x1 = QuadraticBezier(r\pos[i], ptx1, ptx2, ptx3)
y1 = QuadraticBezier(r\pos[i], pty1, pty2, pty3)
x2 = QuadraticBezier(r\pos[i+1] , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(r\pos[i+1] , pty1, pty2, pty3)
;col = DrawLine(x1, y1, x2, y2, col)
Color rd, gr, bl: Line x1, y1, x2, y2
EndIf
Else ; switchover
overlap = True
If r\loc+4 >= r\s\size ;End of spline
r\loc=0
Return
EndIf
ptx1 = r\s\pts[r\loc]\x: pty1 = r\s\pts[r\loc]\y
ptx2 = r\s\pts[r\loc+1]\x: pty2 = r\s\pts[r\loc+1]\y
ptx3 = r\s\pts[r\loc+2]\x: pty3 = r\s\pts[r\loc+2]\y
x1 = QuadraticBezier(r\pos[i], ptx1, ptx2, ptx3)
y1 = QuadraticBezier(r\pos[i], pty1, pty2, pty3)
x2 = r\s\pts[r\loc+2]\x
y2 = r\s\pts[r\loc+2]\y
Color rd, gr, bl: Line x1, y1, x2, y2
;col = DrawLine(x1, y1, x2, y2, col)
EndIf
Else
If r\loc+4 >= r\s\size ;End of spline
r\loc=0
Return
EndIf
ptx1 = r\s\pts[loc+2]\x: pty1 = r\s\pts[loc+2]\y
ptx2 = r\s\pts[loc+3]\x: pty2 = r\s\pts[loc+3]\y
ptx3 = r\s\pts[loc+4]\x: pty3 = r\s\pts[loc+4]\y
If i < RUNNER_SIZE-1
x1 = QuadraticBezier(r\pos[i], ptx1, ptx2, ptx3)
y1 = QuadraticBezier(r\pos[i], pty1, pty2, pty3)
x2 = QuadraticBezier(r\pos[i+1] , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(r\pos[i+1] , pty1, pty2, pty3)
;col = DrawLine(x1, y1, x2, y2, col)
Color rd, gr, bl: Line x1, y1, x2, y2
EndIf
EndIf
Next
For i= 0 To RUNNER_SIZE
r\pos[i] = r\pos[i]+1
If r\pos[i] = CURVE_DETAIL Then r\pos[i]=0
Next
If r\pos[0] = 0
r\loc=r\loc+2
EndIf
End Function
-
Ok the first one sucked. Heres a prettier version using a spline made in the editor:
;******************************************************************;
;----------------------- DATA TYPES REQUIRED ----------------------;
;******************************************************************;
Const MAX_SPLINE_SIZE = 1000
Const CURVE_DETAIL = 50
Type point
Field x, y
End Type
Type spline
Field size
Field pts.point[MAX_SPLINE_SIZE]
End Type
;Temp array for holding points used in spline constructor
Dim temp.point(MAX_SPLINE_SIZE)
Const RUNNER_SIZE = 12
Type runner
Field x, y
Field s.spline
Field pos[RUNNER_SIZE]
Field loc
End Type
;******************************************************************;
;------------------------- FUNCTIONS USED -------------------------;
;******************************************************************;
Function NewPoint.point(x, y)
Local p.point = New point
p\x = x
p\y = y
Return p
End Function
Function NewSpline.spline()
Local s.spline = New spline
Local i = 0
While(temp.point(i) <> Null)
s\pts.point[i] = temp.points(i)
i = i + 1
If(i = MAX_SPLINE_SIZE) Then Exit
Wend
s\size = i
Return s
End Function
Function DrawSpline(s.spline)
i=0
While(s\pts.point[i+2] <> Null)
BezierCurve(s\pts.point[i], s\pts.point[i+1], s\pts.point[i+2])
i=i+2
Wend
End Function
Function BezierCurve(p1.point, p2.point, p3.point)
For i = 0 To CURVE_DETAIL
ptx1 = p1\x+scroll_x: pty1 = p1\y+scroll_y
ptx2 = p2\x+scroll_x: pty2 = p2\y+scroll_y
ptx3 = p3\x+scroll_x: pty3 = p3\y+scroll_y
x1 = QuadraticBezier(i-1, ptx1, ptx2, ptx3)
y1 = QuadraticBezier(i-1, pty1, pty2, pty3)
x2 = QuadraticBezier(i , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(i , pty1, pty2, pty3)
Line x1, y1, x2, y2
Next
End Function
Function QuadraticBezier(Perc#,p1#,p2#,p3#)
d1#=(((p2-p1)*perc)/CURVE_DETAIL)+p1
d2#=(((p3-p2)*perc)/CURVE_DETAIL)+p2
d3#=(((d2-d1)*perc)/CURVE_DETAIL)+d1
Return d3
End Function
Function CubicBezier(perc#,p1#,p2#,p3#,p4#)
d1#=(((p2-p1)*perc)/CURVE_DETAIL)+p1
d2#=(((p3-p2)*perc)/CURVE_DETAIL)+p2
d3#=(((p4-p3)*perc)/CURVE_DETAIL)+p3
d4#=QuadraticBezier(perc,d1,d2,d3)
Return d4
End Function
;******************************************************************;
;--------------------------- TEST PROGRAM -------------------------;
;******************************************************************;
Graphics 800,600,32, 1
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Read size
For i=0 To size
Read x
Read y
temp.point(i) = NewPoint(x, y)
Next
s.spline = NewSpline()
For a = 0 To 9
f.runner = CreateRunner(s)
f\loc = a*4
Next
Global hold = -1 ;indicates which point is held
Global scroll_x=0, scroll_y=0
While Not KeyHit(1)
Runner()
Flip
Wend
;********************************************************************;
;--------------------------- EXTRA FUNCTIONS ------------------------;
;********************************************************************;
Function CreateRunner.runner(s.spline)
Local n.runner = New runner
n\x = s\pts.points[0]\x
n\y = s\pts.points[0]\y
n\s = s
n\loc = 0
For i = 0 To RUNNER_SIZE
n\pos[i]=i
Next
Return n
End Function
Function Runner()
For r.runner = Each runner
DrawRunner(r)
RunnerDot(r.runner)
Next
End Function
Function DrawRunner(r.runner)
overlap = False
For i= 0 To RUNNER_SIZE-1
rd = rd+10
gr = gr+8
bl = bl+5
If overlap = False
If r\pos[i] < r\pos[i+1]
ptx1 = r\s\pts[r\loc]\x: pty1 = r\s\pts[r\loc]\y
ptx2 = r\s\pts[r\loc+1]\x: pty2 = r\s\pts[r\loc+1]\y
ptx3 = r\s\pts[r\loc+2]\x: pty3 = r\s\pts[r\loc+2]\y
If i < RUNNER_SIZE-1
x1 = QuadraticBezier(r\pos[i], ptx1, ptx2, ptx3)
y1 = QuadraticBezier(r\pos[i], pty1, pty2, pty3)
x2 = QuadraticBezier(r\pos[i+1] , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(r\pos[i+1] , pty1, pty2, pty3)
Color rd, gr, bl: Line x1, y1, x2, y2
EndIf
Else ; switchover
overlap = True
If r\loc+4 >= r\s\size ;End of spline
r\loc=0
Return
EndIf
ptx1 = r\s\pts[r\loc]\x: pty1 = r\s\pts[r\loc]\y
ptx2 = r\s\pts[r\loc+1]\x: pty2 = r\s\pts[r\loc+1]\y
ptx3 = r\s\pts[r\loc+2]\x: pty3 = r\s\pts[r\loc+2]\y
x1 = QuadraticBezier(r\pos[i], ptx1, ptx2, ptx3)
y1 = QuadraticBezier(r\pos[i], pty1, pty2, pty3)
x2 = r\s\pts[r\loc+2]\x
y2 = r\s\pts[r\loc+2]\y
Color rd, gr, bl: Line x1, y1, x2, y2
EndIf
Else
If r\loc+4 >= r\s\size ;End of spline
r\loc=0
Return
EndIf
ptx1 = r\s\pts[r\loc+2]\x: pty1 = r\s\pts[r\loc+2]\y
ptx2 = r\s\pts[r\loc+3]\x: pty2 = r\s\pts[r\loc+3]\y
ptx3 = r\s\pts[r\loc+4]\x: pty3 = r\s\pts[r\loc+4]\y
If i < RUNNER_SIZE-1
x1 = QuadraticBezier(r\pos[i], ptx1, ptx2, ptx3)
y1 = QuadraticBezier(r\pos[i], pty1, pty2, pty3)
x2 = QuadraticBezier(r\pos[i+1] , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(r\pos[i+1] , pty1, pty2, pty3)
Color rd, gr, bl: Line x1, y1, x2, y2
EndIf
EndIf
Next
For i= 0 To RUNNER_SIZE
r\pos[i] = r\pos[i]+1
If r\pos[i] = CURVE_DETAIL Then r\pos[i]=0
Next
If r\pos[0] = 0
r\loc=r\loc+2
EndIf
End Function
Function RunnerDot(r.runner)
overlap = False
For i=0 To RUNNER_SIZE-1
If r\pos[i] > r\pos[i+1] Then overlap = True
Next
If overlap = False
ptx1 = r\s\pts[r\loc]\x: pty1 = r\s\pts[r\loc]\y
ptx2 = r\s\pts[r\loc+1]\x: pty2 = r\s\pts[r\loc+1]\y
ptx3 = r\s\pts[r\loc+2]\x: pty3 = r\s\pts[r\loc+2]\y
Else If r\loc+4 < r\s\size
ptx1 = r\s\pts[r\loc+2]\x: pty1 = r\s\pts[r\loc+2]\y
ptx2 = r\s\pts[r\loc+3]\x: pty2 = r\s\pts[r\loc+3]\y
ptx3 = r\s\pts[r\loc+4]\x: pty3 = r\s\pts[r\loc+4]\y
EndIf
x2 = QuadraticBezier(r\pos[RUNNER_SIZE] , ptx1, ptx2, ptx3)
y2 = QuadraticBezier(r\pos[RUNNER_SIZE] , pty1, pty2, pty3)
Color 255, 0, 0
Oval x2-1, y2-1, 3, 3, 1
End Function
Data 52, 100, 180, 10, 120, 60, 90, 40, 120, 100, 180, 60, 120, 80, 90, 70, 120, 100, 180, 130, 120, 120, 90, 140, 120, 100, 180, 160, 120, 140, 90, 190, 120, 100, 180, 20, 140, 30, 180, 37, 209, 70, 210, 30, 190, 60, 180, 80, 180, 100, 190, 120, 180, 150, 180, 180, 190, 130, 210, 170, 210, 180, 180, 189, 151, 100, 180, 110, 260, 90, 290, 20, 420, 50, 480, 130, 330, 190, 350, 218, 359, 160, 410, 110, 450, 220, 370, 244, 352, 170, 320, 142, 308, 50, 480, 20, 430, 50, 340, 59, 311, 20, 270, 60, 270, -21, 226
-
Any chance I could get an exe? cant compile to see it
-
Sure dude, here you go: Link (http://grundez.sitesled.com/Splines.zip)
-
Awesome effect looks like a rose. Love the way that its got highlights runnin through it
-
Thanks man, I still have a lot of work to do on the editor, but if I can get that finished I'd like to make a C library that will basically do a load of different kinds of effects and stuff from the splines. This was done in Blitz because its quick and easy :)
-
Very nice, works fine with B+ too
-
Smart stuff mate.
-
cheers, guys.