I'm pretty new to Löve/programming in general and I have the following problem: I want to program a visualized quick-sort. Now, I already visualized insert-sort and bubble-sort, by doing this:
Code: Select all
function love.update(dt)
if G_StartSort then
if Time:hasDelay(G_Speed) and not G_Stop then
G_SortFuncs:Step() --G_SortFuncs is a function pointer to the sort function.
end
--print("drawing")
G_SortFuncs:Draw()
end
--Timer update
Time:update(dt)
--LoveFrames
loveframes.update(dt)
end
So every time the G_SortFuncs:Step() is called, he does the following:
Code: Select all
function InsertSort:Step()
Time:resetDelay()
if self.j <= #self.Array then
if self.i > 0 and self.Array[self.i] >= self.key then
self.Array[self.i+1] = self.Array[self.i]
self.Array[self.i] = 0 --This is not needed, but makes the animation look better
self.i = self.i - 1
else
self.Array[self.i+1] = self.key
self.j = self.j + 1
self.key = self.Array[self.j]
self.i = self.j - 1
end
else
InsertSort.isSorted = true
end
end
Code: Select all
function InsertSort(Array)
for j = 2, #Array do
key = Array[j]
i = j - 1
while i > 0 and Array[i] >= key do
Array[i+1] = Array[i]
i = i - 1
end
Array[i+1] = key
end
end
Code: Select all
function InsertSort:Draw()
canvas:clear()
local tempposx = self.posx
local tempposy = self.posy
for i = 1, #self.Array do
local height = self.stretch*self.Array[i]--*100/self.maxheight
canvas:renderTo(function()
love.graphics.rectangle("fill", tempposx, tempposy-height, self.width-1, height)
end)
tempposx = tempposx + self.width
end
end
Code: Select all
function love.draw()
love.graphics.draw(canvas)
--LoveFrames
loveframes.draw()
end
Thanks!