Genuary 2023 Day 15 - Sine Waves

January 15, 2023
pico-8 open-source
Genuary 2023 Day 15 - Sine Waves
-- genuary #15 2023
-- sine waves
-- by carson kompon
gridw=20
gridh=20
gridspacing=4
dotsize=2
angle=0
spinam=2/(60*16)

hgw=gridw\2
hgh=gridh\2

pal({[0]=0,128,8,137,9,139,3,131,1,140,12},1)

camera(-64,-64)
::_::
	cls(1)
	local pts={}
	for i=-hgw,hgw do
		for j=-hgh,hgh do
			px=i*gridspacing
			py=j*gridspacing
			dist=sqrt((px*px)+(py*py))
			dir=atan2(px,py)+angle
			xx=cos(dir)*dist
			yy=sin(dir)*dist/2
			zz=4+sin(t()+(i-j)/16)*4
			add(pts,{
				x=xx,y=yy,z=zz
			})
		end
	end
	local lwr=flr(#pts/2)+1
	local upr=#pts
	local i,j,tmp
	while true do
		if lwr>1 then
			lwr-=1
			tmp=pts[lwr]
		else
			tmp=pts[upr]
			pts[upr]=pts[1]
			upr-=1
			if upr==1 then
				pts[1]=tmp
				break
			end
		end
		
		i=lwr
		j=lwr*2
		while j<=upr do
			if j<upr and pts[j].y<pts[j+1].y then
				j+=1
			end
			if tmp.y<pts[j].y then
				pts[i] = pts[j]
				i=j
				j+=i
			else
				j=upr+1
			end
		end
		pts[i]=tmp
	end
	for i=1,#pts do
		pt=pts[i]
		fillp()
		circfill(pt.x,pt.y,dotsize,0)
		fillp()
		circfill(pt.x,pt.y-pt.z,dotsize,flr(2.5+pt.z))
	end
	angle+=spinam
	flip()
goto _