alexthescott.lofi 3d 2

-- lofi 3d 2
-- alexthescott
-- 10/21/9:22pm

-- based on 3d demo
-- by @noahrosamilia

-- circle maths 
-- https://www.cmu.edu/biolphys/deserno/pdf/sphere_equi.pdf

-- new seed every day of the year 
srand(31*stat(81)+stat(82)) 

p1={7,6,135,10,138,11,139,3,131,12,140,1,129,130,128,0}
p2={7,135,10,9,15,143,142,137,14,136,8,2,130,133,128,0}
p3={7,15,143,142,14,8,136,137,9,10,135,138,11,139,12,140}

p={p1,p2,p3}
c=rnd(p)

pal(c,1)

function draw_shape(s)
	for i,l in ipairs(s[2]) do
		draw_line(s[1][l[1]],s[1][l[2]],i)
	end
end

function draw_line(p1,p2,c)
	x0,y0=project(p1)
	x1,y1=project(p2)
	line(x0,y0,x1,y1,c)
end

function draw_point(p,c)
	x,y=project(p)
	pset(x,y,c)
end

function project(p)
	-- calculate x,y and center it
	x=(p[1]-cam[1])*mult/(p[3]-cam[3])+127/2
	y=-(p[2]-cam[2])*mult/(p[3]-cam[3])+127/2
	return x,y
end

function translate_shape(s,t)
	-- copy shape, 0 out points,
	-- keep og lines
	ns={{},s[2]}
	-- add displacement to point
	for p in all(s[1])do
		add(ns[1],{p[1]+t[1],p[2]+t[2],p[3]+t[3]})
	end
	return ns
end

function rotate_shape(s,a,r)
	-- copy shape, 0 out points
	-- keep og lines
	ns={{},s[2]}
	for p in all(s[1])do
		add(ns[1],rotate_point(p,a,r))
	end
	return ns
end

function rotate_point(p,a,r)
	-- figure axis we're rotating
	if a==1 then
		x,y,z=3,2,1
	elseif a==2 then
		x,y,z=1,3,2
	elseif a==3 then
		x,y,z=1,2,3
	end
	
	_x=cos(r)*p[x]-sin(r)*p[y]
	_y=sin(r)*p[x]+cos(r)*p[y]
	np={}
	np[x]=_x
	np[y]=_y
	np[z]=p[z]
	return np
end

function limit_int(v,l)
	if v>0 then
		return min(v,l)
	else
		return max(v,-l)
	end
end

function burn()
	for p=0,384 do
		x=rnd(128)\1
		y=rnd(128)\1
		pc=pget(x,y)
		if pc!=0 then
			pset(x,y,pc-1)
		else
			pset(x,y,0)
		end
	end
end

function fuzz(big)
	if big then
		sz=10
		cnt=512
	else
		sz=3
		cnt=384
	end

	for p=0,cnt do
		x=rnd(128)\1
		y=rnd(128)\1
		pc=pget(x,y)
		if pc!=0 then
			for l=1,rnd(sz)\1 do
			pset(x-l,y,pc)
			pset(x+l,y,pc)
			end
		end		
	end
end

function forward()
	v=c[1]
	del(c,v)
	c[#c+1]=v
	pal(c,1)
end

function backward()
	v=c[#c]
	del(c,v)
	for i=#c+1,1,-1 do
		if i!=1 then
			c[i]=c[i-1]
		else
			c[i]=v
		end
	end
	pal(c,1)
end

function add_point(shape)
	z=rnd(2)-1
	a=rnd()
	x=sqrt(2-z^2)*cos(a)
	y=sqrt(2-z^2)*sin(a)
	p={x,y,z}
	add(shape[1],p)
	if #shape[1]>1 then
		i=#shape[1]
		add(shape[2],{i-1,i})
	end
end

function rm_point(shape)
	del(shape[1],shape[1][#shape[1]])
	del(shape[2],shape[2][#shape[2]])
end

for i=1,rnd()*16 do
	forward()
end


for p=1,24 do
	po={p,p+1}
end

cam={0,0,-2}
mult=64

cir={{},{}}
add_point(cir)
add_point(cir)

remove_points=false

month=stat(81)
day=stat(82)
cls()
_set_fps(60)
::♥::
if t()<2 then
	print("lofi 3d 2",46,59,1)
	print(month.."/"..day,55,65)
else

	if remove_points==false
		and t()%0.5==0 then
			add_point(cir)
	end
	
	if remove_points==false
		and #cir[1]>=64 then
			remove_points=true
	end
	
	if remove_points==true and
		t()%0.25==0 and
		#cir[1]>=1 then
			rm_point(cir)
	elseif #cir[1]<=1 and
		remove_points==true then
			remove_points=false		
	end
	
	cls()
	
	if btn(⬅️) then cam[1] -= 0.1 end
	if btn(➡️) then cam[1] += 0.1 end
	if btn(⬆️) then cam[2] += 0.1 end
	if btn(⬇️) then cam[2] -= 0.1 end
	if btn(❎) then cam[3] -= 0.1 end
	if btn(🅾️) then cam[3] += 0.1 end
	
	if btn(⬆️) and btn(⬇️) or
		btn(➡️) and btn(⬅️) then
			print("cam.x="..cam[1],0,0,2)
			print("cam.y="..cam[2],0,6,2)
			print("cam.z="..cam[3],0,12,2)
	end
	
	cir=rotate_shape(cir,1,0.001)
	cir=rotate_shape(cir,2,0.001)
	draw_shape(cir)
end
flip()
goto ♥