Issues when Sorting table [Solved]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Issues when Sorting table [Solved]

Post by NoreoAlles »

Hello,
i want to sort a table containing triangles, which all contains 3 3d-vectors each, by the average of all z coponents of the vectors in the triangle.
(Which ever triangle has the lower z gets drawn earlier). I have my attempt here line 133 (and the source):

Code: Select all

function Sort_Triangles(list)
    local i = 1
    table.sort(list,function (a, b)
        i = i+1
        if i+1 > #list then i = 1 end
        print(i)
        return (list[i].vectors[1].z + list[i].vectors[2].z + list[i].vectors[3].z)/3 > (list[i+1].vectors[1].z + list[i+1].vectors[2].z + list[i+1].vectors[3].z)/3 end)
    for i=1, #list do 
       love.graphics.polygon("fill", 
       list[i].vectors[1].x, list[i].vectors.vectors[1].y,
       list[i].vectors.vectors[2].x, list[i].vectors.vectors[2].y, 
       list[i].vectors[3].x,list[i].vectors.vectors[3].y)
        love.graphics.setColor( 0, 0,  0)
    end
end
I really cant understand the table.sort function and i would appriciate any help with writing my algorythm and explanetions of table.sort :awesome:
Attachments
demo.love
(742.37 KiB) Downloaded 45 times
Last edited by NoreoAlles on Mon Jul 01, 2024 5:28 pm, edited 1 time in total.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
MrFariator
Party member
Posts: 559
Joined: Wed Oct 05, 2016 11:53 am

Re: Issues when Sorting table

Post by MrFariator »

table.sort optionally takes a function as its second argument, which it will use to determine if element 'a' should be sorted before element 'b'. If the function returns true, then element 'a' is determined to be in the table before 'b', and vice versa for false. In your code, you're trying to traverse the table inside your custom sorter, and effectively entirely ignoring the 'a' and 'b' elements the function is receiving, which probably causes very incorrect results.

So, you'd need to modify your function to be like this:

Code: Select all

local customSorter = function ( a, b )
  return (a.vectors[1].z + a.vectors[2].z + a.vectors[3].z)/3 > (b.vectors[1].z + b.vectors[2].z + b.vectors[3].z)/3
end

-- usage:
table.sort (list, customSorter)
If you're constantly using this custom sort function, it's better to declare in a way that you can continuously reuse it, like as a local.

In order to flip the sorter to sort lowest triangles first, you'd likely need to flip the ">" to "<", however.
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: Issues when Sorting table

Post by NoreoAlles »

MrFariator wrote: Mon Jul 01, 2024 4:48 pm table.sort optionally takes a function as its second argument, which it will use to determine if element 'a' should be sorted before element 'b'. If the function returns true, then element 'a' is determined to be in the table before 'b', and vice versa for false. In your code, you're trying to traverse the table inside your custom sorter, and effectively entirely ignoring the 'a' and 'b' elements the function is receiving, which probably causes very incorrect results.

So, you'd need to modify your function to be like this:

Code: Select all

local customSorter = function ( a, b )
  return (a.vectors[1].z + a.vectors[2].z + a.vectors[3].z)/3 > (b.vectors[1].z + b.vectors[2].z + b.vectors[3].z)/3
end

-- usage:
table.sort (list, customSorter)
If you're constantly using this custom sort function, it's better to declare in a way that you can continuously reuse it, like as a local.

In order to flip the sorter to sort lowest triangles first, you'd likely need to flip the ">" to "<", however.
First: Thank you, that worked .
Secondly: I thought i had to make my own iterator, but thats whats been giving me errors hahhaha
Its working now, heres a beautifully 3d rendered screenshot:
Attachments
progress.PNG
progress.PNG (10.86 KiB) Viewed 1129 times
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 3 guests