Main Menu

Spiral

Started by balaporte, February 21, 2011, 05:40:10 PM

Previous topic - Next topic

balaporte

This came out of teaching myself to use sin and cosine for 2D rotation, and draws a rotating spiral on the screen. I had a version that used Kevin's fat line code to make the spiral thicker the farther away it was from the center, but I can't find that now. I'll post it later if I find it.

Use the right and left arrow keys to change the direction of the spiral rotation, and ESC to exit.

PlayBASIC Code: [Select]
sw = 1024
sh = 768
sd = 32
fullscreen = Yes

OpenScreen sw,sh,sd,fullscreen + 1

setfps 60

; VARIABLES

Type spiraltype
cx#
cy#
x#
y#
visible
EndType

spiralcount = 1080

Dim spiral(2,spiralcount) As spiraltype

For a = 1 To spiralcount Step 2
inc b
spiral(1,b).visible = yes
spiral(1,b).cx# = sw/2
spiral(1,b).cy# = sh/2
spiral(1,b).x# = cos(a*3)*a
spiral(1,b).y# = sin(a*3)*a
next a

; 2D scale
for a = 1 to b
spiral(1,a).x# = spiral(1,a).x# * 0.75
spiral(1,a).y# = spiral(1,a).y# * 0.75
next a

rotspeed# = 3

angle# = rotspeed#

; 2D rotate
for a = 1 to b
x# = spiral(1,a).x#
y# = spiral(1,a).y#
spiral(1,a).x# = x#*cos(angle#)-y#*sin(angle#)
spiral(1,a).y# = x#*sin(angle#)+y#*cos(angle#)
next a

; MAIN LOOP

repeat

if rightkey() then angle# = rotspeed# :flushkeys
if leftkey() then angle# = neg(rotspeed#) :flushkeys

for a = 1 to b
x# = spiral(1,a).x#
y# = spiral(1,a).y#
spiral(1,a).x# = x#*cos(angle#)-y#*sin(angle#)
spiral(1,a).y# = x#*sin(angle#)+y#*cos(angle#)
next a

; DISPLAY

cls rgb(255,0,0)

print fps()

ink rgb(0,0,0)

lockbuffer

for a = 1 to b
if a < b
x1# = spiral(1,a).cx# + spiral(1,a).x#
y1# = spiral(1,a).cy# + spiral(1,a).y#
x2# = spiral(1,a+1).cx# + spiral(1,a+1).x#
y2# = spiral(1,a+1).cy# + spiral(1,a+1).y#
line x1#, y1#, x2#, y2#
endif
next a

unlockbuffer

sync

until esckey()




monkeybot


kevin

#2

yes, it's very nice..  hypnotic even  ;)


Here's a bit of tweak

PlayBASIC Code: [Select]
sw = 1024
sh = 768
sd = 32
fullscreen = Yes

OpenScreen sw,sh,sd,fullscreen + 1

;setfps 60

; VARIABLES

Type spiraltype
cx#
cy#
x#
y#
visible
EndType

spiralcount = 1080

Dim spiral(2,spiralcount) As spiraltype

For a = 1 To spiralcount Step 2
inc b
spiral(1,b).visible = yes
spiral(1,b).cx# = sw/2
spiral(1,b).cy# = sh/2
spiral(1,b).x# = cos(a*3)*a
spiral(1,b).y# = sin(a*3)*a
next a

; 2D scale
for a = 1 to b
spiral(1,a).x# = spiral(1,a).x# * 0.75
spiral(1,a).y# = spiral(1,a).y# * 0.75
next a

rotspeed# = 3

angle# = rotspeed#

; 2D rotate
for a = 1 to b
x# = spiral(1,a).x#
y# = spiral(1,a).y#
spiral(1,a).x# = x#*cos(angle#)-y#*sin(angle#)
spiral(1,a).y# = x#*sin(angle#)+y#*cos(angle#)
next a

; MAIN LOOP
ink rgb(0,0,0)


repeat

; Do the Clear here since it's an async fill
; so the gpu fills the backdrop while the rotation code does the work. So they occur together.
;
cls rgb(255,0,0)
if rightkey() then angle# = rotspeed# :flushkeys
if leftkey() then angle# = neg(rotspeed#) :flushkeys


ca#=cos(angle#)
sa#=sin(angle#)

for a = 1 to b
x# = spiral(1,a).x#
y# = spiral(1,a).y#
spiral(1,a).x# = x#*ca#-y#*sa#
spiral(1,a).y# = x#*sa#+y#*ca#
next a

; DISPLAY

print fps()


lockbuffer

a=1
x1# = spiral(1,a).cx# + spiral(1,a).x#
y1# = spiral(1,a).cy# + spiral(1,a).y#
for a = 2 to b
x2# = spiral(1,a).cx# + spiral(1,a).x#
y2# = spiral(1,a).cy# + spiral(1,a).y#
line x1#, y1#, x2#, y2#
x1#=x2#
y1#=y2#
next a

unlockbuffer

sync
until esckey()



balaporte

That makes sense...thanks. I liked the version using your fat line code better, so I'll redo that one and post.

monkeybot

#4
i did it another way.
rather quick and dirty,its well past my bedtime. :)


PlayBASIC Code: [Select]
; PROJECT : Project1
; AUTHOR : monkeybot
; CREATED : 24/02/2011
; ---------------------------------------------------------------------
explicit on
local rad#
openscreen 800,600,32,1
local sw=GetScreenWidth()
local sh=GetScreenHeight()
local x#=400
local y#=300
local c#=0
local w,q,oldx#,oldy#,a
local x2#=100
local y2#=100
;setfps (60)
do
rad#=sw

cls
print str$(fps())
oldx#=x#+(sin(w)*rad#)
oldy#=y#+(cos(w)*rad#)
lockbuffer
for q=a to a+(360*(sh)/68); step 2
w=WrapAngle (q)
if x#>0 and x#<sw and y#>0 and y#<sh
x2#=x#+(sin(w)*rad#)
y2#=y#+(cos(w)*rad#)
line oldx#,oldy#,x2#,y2#
oldx#=x2#
oldy#=y2#
endif
rad#=rad#-0.25
next
unlockbuffer
a+=8
if a>360 then a=0
sync
loop
waitkey