Skip to content

Instantly share code, notes, and snippets.

@ExtReMLapin
Last active April 11, 2016 12:46
Show Gist options
  • Save ExtReMLapin/d25f42994de6645142dfc808f7faa45c to your computer and use it in GitHub Desktop.
Save ExtReMLapin/d25f42994de6645142dfc808f7faa45c to your computer and use it in GitHub Desktop.
function love.load()
love.window.setMode(2000, 1000, {resizable=false, vsync=false, minwidth=400, minheight=300})
end
function HSV(h, s, v)
if s <= 0 then return v,v,v end
h, s, v = h/256*6, s/255, v/255
local c = v*s
local x = (1-math.abs((h%2)-1))*c
local m,r,g,b = (v-c), 0,0,0
if h < 1 then r,g,b = c,x,0
elseif h < 2 then r,g,b = x,c,0
elseif h < 3 then r,g,b = 0,c,x
elseif h < 4 then r,g,b = 0,x,c
elseif h < 5 then r,g,b = x,0,c
else r,g,b = c,0,x
end return (r+m)*255,(g+m)*255,(b+m)*255
end
function love.draw()
local w, h = love.graphics.getDimensions( )
local cRe, cIm; --real and imaginary part of the constant c, determinate shape of the Julia Set
local newRe
local newIm
local oldRe
local oldIm; --real and imaginary parts of new and old
local zoom = 1
local moveX = 0
local moveY = 0; --you can change these to zoom and change position
local color= {0,0,0}; --the RGB color value for the pixel
local maxIterations = 300; --after how much iterations the function should stop
--pick some values for the constant c, this determines the shape of the Julia Set
local cRe = -0.7;
local cIm = 0.27015;
--loop through every pixel
local y = 1;
local x;
while ( y < h) do
x = 1
while (x < w) do
--calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values
newRe = 1.5 * (x - w / 2) / (0.5 * zoom * w) + moveX;
newIm = (y - h / 2) / (0.5 * zoom * h) + moveY;
--i will represent the number of iterations
local i = 0;
--start the iteration process
while(i < maxIterations) do
--remember value of previous iteration
oldRe = newRe;
oldIm = newIm;
--the actual iteration, the real and imaginary part are calculated
newRe = oldRe * oldRe - oldIm * oldIm + cRe;
newIm = 2 * oldRe * oldIm + cIm;
--if the point is outside the circle with radius 2: stop
if((newRe * newRe + newIm * newIm) > 4) then break end;
i = i +1
end
--use color model conversion to get rainbow palette, make brightness black if maxIterations reached
if i< maxIterations then
r, g, b = HSV(i % 256, 255, 255)
else
r, g, b = 0, 0 , 0
end
--draw the pixel
love.graphics.setColor( r,g,b,255 )
love.graphics.setPointSize(1)
love.graphics.points(x, y)
x = x+1;
end
y = y + 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment