[SNIPPET] Dynamic Spiral Drawing

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Archiboldian C.
Prole
Posts: 10
Joined: Sun Mar 13, 2011 12:52 pm
Location: UK
Contact:

[SNIPPET] Dynamic Spiral Drawing

Post 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.
Attachments
sprlz.love
(8.58 KiB) Downloaded 219 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [SNIPPET] Dynamic Spiral Drawing

Post by Robin »

Trippy. :D
Help us help you: attach a .love.
User avatar
Archiboldian C.
Prole
Posts: 10
Joined: Sun Mar 13, 2011 12:52 pm
Location: UK
Contact:

Re: [SNIPPET] Dynamic Spiral Drawing

Post by Archiboldian C. »

Robin wrote:Trippy. :D
Thonks.
Post Reply

Who is online

Users browsing this forum: No registered users and 49 guests