Visitante Postado Janeiro 27, 2013 Share Postado Janeiro 27, 2013 (editado) Eu sinceramente acho injustiça chamar um pacote com duas funções de biblioteca, mas... Isso foi uma ideia do Oneshot. Serve para rotacionar tabelas (por exemplo, se você quiser rotacionar uma área de uma magia). Eu sinceramente não vejo muita utilidade em open tibia, mas ele jura que serve para alguma coisa e me disse para postar, então aqui está: matrix.lua -- Copyright (c) 2013 Skyen Hasus -- -- Permission is hereby granted, free of charge, to any person obtaining a copy -- of this software and associated documentation files (the "Software"), to deal -- in the Software without restriction, including without limitation the rights -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -- copies of the Software, and to permit persons to whom the Software is -- furnished to do so, subject to the following conditions: -- -- The above copyright notice and this permission notice shall be included in -- all copies or substantial portions of the Software. module("matrix", package.seeall) _G.DEGREES_0 = 0 _G.DEGREES_90 = 90 _G.DEGREES_180 = 180 _G.DEGREES_270 = 270 _G.DIRECTION_VERTICAL = 0 _G.DIRECTION_HORIZONTAL = 1 local function rotate_90(matrix) local ret = {} for y in ipairs(matrix) do local w = #matrix[y] for x, v in ipairs(matrix[y]) do if not ret[w-x+1] then ret[w-x+1] = {} end ret[w-x+1][y] = v end end return ret end local function rotate_180(matrix) local ret = {} local h = #matrix for y in ipairs(matrix) do local w = #matrix[y] for x, v in ipairs(matrix[y]) do if not ret[h-y+1] then ret[h-y+1] = {} end ret[h-y+1][w-x+1] = v end end return ret end local function rotate_270(matrix) local ret = {} local h = #matrix for y in ipairs(matrix) do for x, v in ipairs(matrix[y]) do if not ret[x] then ret[x] = {} end ret[x][h-y+1] = v end end return ret end local function mirror_v(matrix) local ret = {} local h = #matrix for y in ipairs(matrix) do for x, v in ipairs(matrix[y]) do if not ret[h-y+1] then ret[h-y+1] = {} end ret[h-y+1][x] = v end end return ret end local function mirror_h(matrix) local ret = {} for y in ipairs(matrix) do local w = #matrix[y] for x, v in ipairs(matrix[y]) do if not ret[y] then ret[y] = {} end ret[y][w-x+1] = v end end return ret end function rotate(matrix, degrees) degrees = degrees % 360 if degrees == DEGREES_0 then return matrix elseif degrees == DEGREES_90 then return rotate_90(matrix) elseif degrees == DEGREES_180 then return rotate_180(matrix) elseif degrees == DEGREES_270 then return rotate_270(matrix) end error("Invalid degree value to function 'rotate'.", 2) return false end function mirror(matrix, direction) if direction == DIRECTION_VERTICAL then return mirror_v(matrix) elseif direction == DIRECTION_HORIZONTAL then return mirror_h(matrix) end error("Invalid direction to function 'mirror'.", 2) return false end Exemplo de uso: require("matrix") local array = { {1, 1, 2}, {0, 1, 0}, {0, 1, 0}, {3, 1, 4}, } local array_vertical = matrix.mirror(array, DIRECTION_VERTICAL) local array_horizontal = matrix.mirror(array, DIRECTION_HORIZONTAL) local array_90 = matrix.rotate(array, DEGREES_90) local array_180 = matrix.rotate(array, DEGREES_180) local array_270 = matrix.rotate(array, DEGREES_270) local array_vertical = matrix.mirror(array, 0) local array_horizontal = matrix.mirror(array, 1) local array_90 = matrix.rotate(array, 90) local array_180 = matrix.rotate(array, 180) local array_270 = matrix.rotate(array, 270) Funções e parâmetros: (Apesar de conter sete funções, apenas duas delas são públicas e você pode usar.) matrix.rotate(matrix, degrees) Rotaciona uma matriz, onde matrix é a matriz a ser rotacionada e degrees é o ângulo da rotação. Apenas aceita ângulos "quadrados" (múltiplos de 90): -180, -90, 0, 90, 180, 270, 360, 450, e por ai vai... Você pode usar DEGREES_0, DEGREES_90, DEGREES_180 e DEGREES_270 também. A função retorna uma nova tabela, com a rotação aplicada. matrix.mirror(matrix, direction) Espelha uma matriz, onde matrix é a matriz a ser espelhada e direction é o sentido do espelhamento. Apenas aceita os valores DIRECTION_VERTICAL (0, vertical) e DIRECTION_HORIZONTAL (1, horizontal). A função retorna uma nova tabela, com o espelhamento aplicado. Você precisa dar o nome do arquivo de matrix.lua, por ser um módulo. Se for usar fora de open tibia, você precisa dar require("matrix") para importar o módulo. Se for usar no open tibia, basta jogar o matrix.lua na pasta lib. Editado Janeiro 27, 2013 por Visitante Link para o comentário https://xtibia.com/forum/topic/205660-pra-quem-quiser-rotacionar-ou-espelhar-matrizes/ Compartilhar em outros sites More sharing options...
sailorv 5 Postado Maio 7, 2013 Share Postado Maio 7, 2013 Sei que posso tomar flod mais nao quero abrir um novo topico com functions que a gente ultilisa uma ou duas vezes na vida ! a 2 semanas atraz eu estava procurando por essas funcoes achei HOJE um pouco tarde talvez nao usei as palavras certas eu tinha procurado com palavras como os nome que eu dei. posto aqui como eu fiz mais no final faz as mesmo coisa que suas ( nada de novo ) matrix = {} function matrix.new ( row , col , void ) local m = {} for i = 1 , row , 1 do table.insert(m, {} ) for j = 1 , col , 1 do -- if void ~= true then table.insert(m[i], 0 ) -- end end end return m end function matrix.reverse ( m ) local tttiii = {} local ii = 1 for i = #m ,1,-1 do table.insert(tttiii, m[i] ) end return tttiii end function matrix.inverse ( m ) local mi = {} -- matrix.new ( #m , #m[1] , true ) for i = 1 , #m , 1 do table.insert( mi, {} ) for j = #m[i] ,1,-1 do table.insert(mi[i], m[i][j] ) end end return mi end function matrix.invert(t) local u = { } for k, v in pairs(t) do u[v] = k end return u end function matrix.transpose ( m ) local mt = matrix.new ( #m[1] , #m ) for i = 1 , #m , 1 do for j = 1 , #m[i] , 1 do mt[j][i] = m [i][j] end end return mt end function matrix.deleteNillValue ( m , rowsize , colsize ) -- Delete Nill Value from a array local m = m or {{0}} local nm = matrix.new ( #m , 0 ) -- clone the size of 1st array local rowsize = rowsize or #m local colsize = colsize or #m[1] local index = 0 for i = 1 , #m , 1 do for j = 1 , #m[i] , 1 do local som = i + index if next( m[(i)][j] ) == nil then -- print ( som .." som " .. i .." i " .. j .." j " ) -- myTable is empty else -- print ( som .." som " .. i .." i " .. j .." j " ) if i == rowsize and j == colsize then table.insert( nm[( som )] ,m[i][j]) table.insert( nm , {} ) index = index +1 else table.insert( nm[(som)] ,m[i][j]) end end end end return nm end -- MiniGameFunction.FieldFlip ( array , "north" ) function MiniGameFunction.FieldFlip ( field , side ) if (type(field) == "table" ) then field = field else return false end if side == "north" then return field end if side == "south" then return matrix.inverse ( matrix.reverse ( field ) ) end if side == "west" then return matrix.inverse ( matrix.transpose ( field ) ) end if side == "east" then return matrix.transpose ( matrix.inverse ( field ) ) end end por que a indentacao usando [ TAB ] e perdida quando eu posto ? Link para o comentário https://xtibia.com/forum/topic/205660-pra-quem-quiser-rotacionar-ou-espelhar-matrizes/#findComment-1517399 Compartilhar em outros sites More sharing options...
Slicer 1070 Postado Maio 7, 2013 Share Postado Maio 7, 2013 /\ pq o CODE do forum eh uma bosta ^^ Link para o comentário https://xtibia.com/forum/topic/205660-pra-quem-quiser-rotacionar-ou-espelhar-matrizes/#findComment-1517423 Compartilhar em outros sites More sharing options...
Eventide 23 Postado Maio 16, 2013 Share Postado Maio 16, 2013 Tem utilidade sim Skyen, lembra daquele exori editado do Apoca que ficava girando várias espadas ao redor do jogador? Ou aquela magia de boomerangue? Se tivéssemos essa função na época dava pra ter feito essas magias em um numero absurdamente reduzido de linhas... Link para o comentário https://xtibia.com/forum/topic/205660-pra-quem-quiser-rotacionar-ou-espelhar-matrizes/#findComment-1521240 Compartilhar em outros sites More sharing options...
Posts Recomendados