Created
August 30, 2011 15:00
-
-
Save lclarkmichalek/1181092 to your computer and use it in GitHub Desktop.
Spiral 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Constants | |
a = 1 | |
n = 1 | |
function spiral(theta) | |
-- Work out the polar equation | |
local r = a * theta ^ (1/n) | |
-- Convert to cartesian coordinates | |
local x = r * math.sin(theta) | |
local y = r * math.cos(theta) | |
return x, y | |
end | |
function love.draw() | |
-- Reset graphics properties for spiral | |
love.graphics.setPoint(3, "smooth") | |
love.graphics.setColor(255, 255, 255, 255) | |
-- Draw for theta between 0 and 3pi, with sample period of pi/100 | |
for theta=0, 3 * math.pi, math.pi/100 do | |
local x, y = spiral(theta) | |
-- Adjust the points to be in the middle of the screen, and scale by 20 | |
love.graphics.point(width / 2 + x * 20, height / 2 + y * 20) | |
end | |
-- Set properties for particle point | |
love.graphics.setPoint(10, "smooth") | |
love.graphics.setColor(0, 102, 0, 255) | |
-- Draw the particle | |
love.graphics.point(width / 2 + particle.x * 20, height / 2 + particle.y * 20) | |
end | |
function love.update(dt) | |
-- Update the theta value. (dt is the time in seconds since update was last called) | |
particle.theta = particle.theta + particle.speed * dt | |
-- Update the x and y value | |
particle.x, particle.y = spiral(particle.theta) | |
end | |
function love.load() | |
width = love.graphics.getWidth() | |
height = love.graphics.getHeight() | |
love.graphics.setPoint(3, "smooth") | |
love.graphics.setColor(255, 255, 255, 255) | |
love.graphics.setBackgroundColor(0, 0, 0) | |
-- Initialise the particle object | |
particle = { | |
x = width/2, | |
y = height/2, | |
speed = math.pi/10, | |
theta = 0 | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment