Skip to content

Instantly share code, notes, and snippets.

@julianoBRL
Created October 18, 2023 12:22
Show Gist options
  • Save julianoBRL/7299b34cc58f1d1b31bcf66f544b5d4e to your computer and use it in GitHub Desktop.
Save julianoBRL/7299b34cc58f1d1b31bcf66f544b5d4e to your computer and use it in GitHub Desktop.
function distancia(x1, y1, z1, x2, y2, z2)
return math.sqrt((x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2)
end
function objetoMaisProximo(lista, x, y, z)
local objetoMaisProximo = nil
local menorDistancia = math.huge
for _, objeto in pairs(lista) do
local d = distancia(x, y, z, objeto.x, objeto.y, objeto.z)
if d < menorDistancia then
objetoMaisProximo = objeto
menorDistancia = d
end
end
return objetoMaisProximo
end
-- Exemplo de uso
local listaDeObjetos = {
{ nome = "Objeto1", x = 10, y = 20, z = 5, tags = { "tag1", "tag2" } },
{ nome = "Objeto2", x = 15, y = 25, z = 8, tags = { "tag2", "tag3" } },
-- Adicione mais objetos à lista
}
local xAtual, yAtual, zAtual = 12, 22, 6
local objetoProximo = objetoMaisProximo(listaDeObjetos, xAtual, yAtual, zAtual)
print("Objeto mais próximo: " .. objetoProximo.nome)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment