Here is the problem, I have a polygon that I triangulate with the 'love.math.triangulate' function and I am trying to get the adjacent triangles from one of them, so I wrote a function here :
Code: Select all
local function getAdjacentTris(index, tris)
local adj = {}
local tri2 = tris[index]
for k, tri1 in pairs(tris) do
if k ~= index then
for i = 1, 6, 2 do
local i1 = i+2<6 and i+2 or 1
local i2 = i+3<7 and i+3 or 2
if (tri1[i] == tri2[i] and tri1[i+1] == tri2[i+1])
and (tri1[i1] == tri2[i1] and tri1[i2] == tri2[i2])
then table.insert(adj, {tri1,k}); break end
end
end
if #adj == 3 then break end
end
return adj
end
But when I try it on the table containing the triangles obtained by calling 'love.math.triangulate' it doesn't return any triangles, while these triangles seem to share the same points. I thought it's probably because of the floating point so I rounded them using this function:
Code: Select all
function math.round(num, decimals)
decimals = math.pow(10, decimals or 0)
num = num * decimals
if num >= 0 then num = math.floor(num + 0.5) else num = math.ceil(num - 0.5) end
return num / decimals
end
So I'm lost with all this, how can I get another's adjacent triangles that come from 'love.math.triangulate'? Where am I making a mistake?
Here is the complete script that I used to see if my function works, it displays the polygon then tries to display the triangles adjacent to the first triangle of this polygon, then displays the triangle adjacent to the first in my list at the top left of display:
Code: Select all
local function hexagon(x,y,l)
local i=(l/2)*3^0.5
return {x,y,x+l,y,x+1.5*l,y+i,x+l,y+2*i,x,y+2*i,x-l*0.5,y+i}
end
local function getAdjacentTris(index, tris)
local adj = {}
local tri2 = tris[index]
for k, tri1 in pairs(tris) do
if k ~= index then
for i = 1, 6, 2 do
local i1 = i+2<6 and i+2 or 1 -- 1+2=3; 3+2=5; !!5+2=7
local i2 = i+3<7 and i+3 or 2 -- 1+3=4; 3+3=6; !!5+3=8
if (tri1[i] == tri2[i] and tri1[i+1] == tri2[i+1])
and (tri1[i1] == tri2[i1] and tri1[i2] == tri2[i2])
then table.insert(adj, {tri1,k}); break end
end
end
if #adj == 3 then break end
end
return adj
end
local poly = hexagon(
(love.graphics.getWidth()-100)/2,
(love.graphics.getHeight()-100)/2,
100
)
local polyTris = love.math.triangulate(poly)
local testTris = {
{1,1, 10,1, 5,10},
{1,1, 10,1, 10,10},
{5,10, 10,1, 10,10},
{90,90, 300,90, 150,90}
}
local adjacent_polyTris = getAdjacentTris(1, polyTris)
local adjacent_testTris = getAdjacentTris(1, testTris)
function love.draw()
love.graphics.setColor(1,0,0)
love.graphics.polygon("fill", poly)
love.graphics.setColor(0,1,0)
for k, tri in pairs(adjacent_polyTris) do
love.graphics.polygon("line", tri)
end
love.graphics.push() -- I enlarged the display so that the triangle is more visible
love.graphics.scale(10,10)
for k, tri in pairs(adjacent_testTris) do
love.graphics.polygon("line", tri[1])
end
love.graphics.pop()
end
Update:
To prove that the triangles obtained by 'love.math.triangulate' share at least two same points, I did this:
Code: Select all
local polyTris = love.math.triangulate(poly)
for _, tri in pairs(polyTris) do
print("A: ", "X = "..tri[1], "Y = "..tri[2])
print("B: ", "X = "..tri[3], "Y = "..tri[4])
print("C: ", "X = "..tri[5], "Y = "..tri[6])
print()
end
Code: Select all
A: X = 350 Y = 250
B: X = 450 Y = 250
C: X = 500 Y = 336.6025390625
A: X = 350 Y = 250
B: X = 500 Y = 336.6025390625
C: X = 450 Y = 423.205078125
A: X = 350 Y = 250
B: X = 450 Y = 423.205078125
C: X = 350 Y = 423.205078125
A: X = 350 Y = 250
B: X = 350 Y = 423.205078125
C: X = 300 Y = 336.6025390625
AUTO-SOLVED:
I only checked (A==A && B==B && C==C) so if (A==C) it was not checked therefore not validated, here is the corrected function, it is very surely optimizable but it works:
Code: Select all
local function getAdjacentTris(index, tris)
local adj = {}
local tri2 = tris[index]
for k, tri1 in pairs(tris) do
if k ~= index then
for i = 1, 6, 2 do
local i1 = i<4 and i+2 or 1 -- 1+2=3; 3+2=5; !!5+2=7
local i2 = i<5 and i+3 or 2 -- 1+3=4; 3+3=6; !!5+3=8
local i3 = i1<4 and i1+2 or i<4 and i-2 or i1-2
local i4 = i2<5 and i2+2 or i<5 and i-1 or i2-1
if ( (tri1[i] == tri2[i] and tri1[i+1] == tri2[i+1])
or (tri1[i] == tri2[i1] and tri1[i+1] == tri2[i2])
or (tri1[i] == tri2[i3] and tri1[i+1] == tri2[i4]) )
and ( (tri1[i1] == tri2[i1] and tri1[i2] == tri2[i2])
or (tri1[i1] == tri2[i3] and tri1[i2] == tri2[i4]) )
then
table.insert(adj, {tri1,k}); break
end
end
end
if #adj == 3 then break end
end
return adj
end