1
0
fractals-love2d/gens/triangle.lua
2023-05-11 21:08:04 +03:00

50 lines
1.7 KiB
Lua
Executable File

local originalSize = 1000
local halfSqrt3 = 0.5*3^0.5
local trianglesToDraw = {}
local function insertTriangle(pos, size, triangleType)
pos = pos or Vector2()
size = size or originalSize
local visibleSize = Vector2(love.graphics.getDimensions())+size
local modifiedPosition = pos + size/2
if (modifiedPosition.x > 0 and modifiedPosition.x < visibleSize.x and modifiedPosition.y > 0 and modifiedPosition.y < visibleSize.y) then
table.insert(trianglesToDraw, {
pos = pos, size = size,
triangleType = triangleType or "full"
})
end
end
local function drawTriangle(pos, size, triangleType)
if size <= 6.5 then return end
local h = size * halfSqrt3
local halfSize = size/2
local halfH = h/2
if triangleType == "full" then
love.graphics.line(pos.x, pos.y-halfH, pos.x+halfSize, pos.y+halfH, pos.x-halfSize, pos.y+halfH, pos.x, pos.y-halfH)
elseif triangleType == "top" then
love.graphics.line(pos.x+halfSize, pos.y+halfH, pos.x-halfSize, pos.y+halfH)
elseif triangleType == "left" then
love.graphics.line(pos.x, pos.y-halfH, pos.x+halfSize, pos.y+halfH)
elseif triangleType == "right" then
love.graphics.line(pos.x-halfSize, pos.y+halfH, pos.x, pos.y-halfH)
end
insertTriangle(pos + Vector2( 0 , -h/4), halfSize, "top")
insertTriangle(pos + Vector2( size/4, h/4), halfSize, "right")
insertTriangle(pos + Vector2(-size/4, h/4), halfSize, "left")
end
return function(offset, scale)
trianglesToDraw = {}
insertTriangle(-offset/scale, originalSize/scale)
while #trianglesToDraw > 0 do
local triangle = table.remove(trianglesToDraw)
drawTriangle(triangle.pos, triangle.size, triangle.triangleType)
end
end