Advertisement
Bolodefchoco_LUAXML

[Math] math.inPolygon

Feb 5th, 2016
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.63 KB | None | 0 0
  1. --Creator: Squalleze
  2. --Made in: 05/02/2016
  3. --Last update: 25/05/2016
  4. --[[ Notes:
  5.     Does:
  6.         Descubra se os pontos x,y estão dentro do polígono de ... lados em ... posições
  7.     Args:
  8.         x --> Ponto horizontal
  9.         y --> Ponto vertical
  10.         ... --> Tabela {x,y} dos lados do polígono
  11. ]]--
  12.  
  13. math.inPolygon=function(x,y,...)
  14.     local s={...}
  15.     local i=false
  16.     local p=#s
  17.     if p<3 then error("A polygon needs more than "..p.." points.") end
  18.     for v=1,p do
  19.         if (s[v][2]<y and s[p][2]>=y) or (s[p][2]<y and s[v][2]>=y) then
  20.             if s[v][1]+(y-s[v][2])/(s[p][2]-s[v][2])*(s[p][1]-s[v][1])<x then
  21.                 i=not i
  22.             end
  23.         end
  24.         p=v
  25.     end
  26.     return i
  27. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement