Page 1 of 1

[SNIPPET] Dynamic Spiral Drawing

Posted: Thu Mar 17, 2011 10:31 pm
by Archiboldian C.

Code: Select all

-- taken from the book Mathographics by Robert Dixon 
-- and ported from BBC BASIC to LUA by
-- Archiboldian Cartheniscope
-- A.K.A: Jonathan Weekes http://the-spire.co.uk/
-- it's an excellent book that should be bought and read (or else)



function love.load()
	titles={ "archimedes'", "equiangular", "fermat's" }
	font = love.graphics.newFont("Header0866.ttf", 15)
	debfont = love.graphics.newFont("Header0866.ttf", 10)
	currentsprl=1
	constant=0.1
	angular=3
	depth=600
	A=10
	lcache = { x = 0, y = 0 }
	g = love.graphics
	m = love.mouse
end

function love.update()
	if love.keyboard.isDown("1") then
		currentsprl=1
	end
	if love.keyboard.isDown("2") then
		currentsprl=2
	end
	if love.keyboard.isDown("3") then
		currentsprl=3
	end
end

function love.draw()
	g.setColor(0,255,0)
	g.setFont(font)
	g.print(titles[currentsprl] .. " spiral   -- use keys 1-3 to choose mode",15,6)
	g.setColor(255,255,255)
	g.translate(0,40)
	A=0
	lcache = { x = 400, y = 300 }
	constant=m.getX()/100
	angular=m.getY()/100
	drawSprl(currentsprl,constant,depth)
end

function drawSprl(mode,K,dep)
	-- K is the constant variable

	for S=0,dep do
	
		-- use selected algorithm
		if mode == 1 then
			R=ARC(K,A)
		elseif mode == 2 then
			R=EQU(K,A)
		elseif mode == 3 then
			R=FER(K,A)
		end
		--
		xpos=R*math.cos(A)
		ypos=R*math.sin(A)
		g.line(lcache.x, lcache.y, lcache.x+xpos, lcache.y+ypos)

		lcache.x=lcache.x+xpos
		lcache.y=lcache.y+ypos
		A= A+angular
	
	end
	g.setFont(debfont)
	g.setColor(0,255,0)
	g.print(mode .. " con: " .. K .. "  dep: " .. dep .. "  ang: " .. A .. "  res: " .. R .. "  x: " .. math.floor(lcache.x) .. "  y: " .. math.floor(lcache.y) ,15,6)
	g.setColor(255,255,255)
end

function ARC(con,ang)
	return con*ang
end

function EQU(con,ang)
	return math.pow(con,ang)
end

function FER(con,ang)
	return math.sqrt(ang)*con
end
Went on a bit a tangent (no pun intended) and threw this together.
Enjoy.

Re: [SNIPPET] Dynamic Spiral Drawing

Posted: Fri Mar 18, 2011 7:17 am
by Robin
Trippy. :D

Re: [SNIPPET] Dynamic Spiral Drawing

Posted: Fri Mar 18, 2011 9:54 am
by Archiboldian C.
Robin wrote:Trippy. :D
Thonks.