left click to create a new wave - the further you click from the center, the faster the wave will move. Right click to create a stationary wave. press the spacebar to slow the program to a crawl, so you can view still frames of the simulation.
global screen_width
screen_width=screen width()
global max_amplitude
max_amplitude=screen height()/2
global wave_num=0
global totalheight
type wave
position as integer
width as integer
amplitude as integer
speed as integer
endtype
dim wave(0) as wave
`for a=1 to 20
`addwave(rnd(screen_width),100,rnd(100)-50,rnd(4)-2)
`next a
do
cls
if mouseclick()=1 and click=0
click=1
s=(screen width()/2-mousex())/20
addwave(mousex(),100,mousey()-screen height()/2,s)
endif
if mouseclick()=2 and click=0
click=1
s=0
addwave(mousex(),100,mousey()-screen height()/2,s)
endif
if mouseclick()=0 then click=0
movewaves()
display_waves()
sync
if spacekey() then wait 1000
loop
wait 100000
end
function display_waves()
for b=1 to screen_width
totalheight=0
for a=1 to wave_num
height=get_wave_height(a,b)
totalheight=totalheight+height
next a
dot b,screen height()/2+totalheight,rgb(127.5*cos(totalheight)+127.5,127.5*cos(totalheight+120)+127.5,127.5*cos(totalheight+240)+127.5)
next b
endfunction
function error(a as string)
print a
wait 100
cls
endfunction
function get_wave_height(a,x)
center=wave(a).position
centerheight=wave(a).amplitude
width=wave(a).width
if x<center
if x<center-width
y=0
exitfunction y
else
`get a value inbetween zero and one, one being at the center, zero being at the center-width
newx#=abs((center*1.0-x*1.0)/width)
`get the height
y=CosInterpolate(centerheight,0,newx#)
exitfunction y
endif
endif
if x>center
if x>center+width
y=0
exitfunction y
else
`get a value inbetween zero and one, zero being at the center, one being at the center+width
newx#=abs((center*1.0-x*1.0)/(width*1.0))
`get the height
y=CosInterpolate(centerheight,0,newx#)
exitfunction y
endif
endif
if x=center
y=wave(a).amplitude
exitfunction y
endif
endfunction 0
function CosInterpolate(p1#,p2#,x#)
x2# = (1.0-cos(x#*180))/2.0
interpolate#=p1#*(1-x2#)+p2#*x2#
endfunction interpolate#
function addwave(position, width, amplitude, speed)
array insert at bottom wave(0)
inc wave_num
wave(wave_num).position=position
wave(wave_num).width=width
wave(wave_num).amplitude=amplitude
wave(wave_num).speed=speed
endfunction
function movewaves()
for a=1 to wave_num
wave(a).position=wave(a).position+wave(a).speed
next a
endfunction