Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Is Point In Polygon?" in mathExtend.lua #3

Closed
Not-A-Normal-Robot opened this issue Nov 19, 2022 · 9 comments
Closed

Add "Is Point In Polygon?" in mathExtend.lua #3

Not-A-Normal-Robot opened this issue Nov 19, 2022 · 9 comments

Comments

@Not-A-Normal-Robot
Copy link
Member

No description provided.

@Not-A-Normal-Robot
Copy link
Member Author

Not-A-Normal-Robot commented Nov 19, 2022

i found this function in the public domain
https://love2d.org/forums/viewtopic.php?p=236582&sid=33f2e78acfbc738ac4b4b9a83488cc6f#p236582

-- By Pedro Gimeno, donated to the public domain
function isPointInPolygon(x, y, poly)
  local x1, y1, x2, y2
  local len = #poly
  x2, y2 = poly[len - 1], poly[len]
  local wn = 0
  for idx = 1, len, 2 do
    x1, y1 = x2, y2
    x2, y2 = poly[idx], poly[idx + 1]

    if y1 > y then
      if (y2 <= y) and (x1 - x) * (y2 - y) < (x2 - x) * (y1 - y) then
        wn = wn + 1
      end
    else
      if (y2 > y) and (x1 - x) * (y2 - y) > (x2 - x) * (y1 - y) then
        wn = wn - 1
      end
    end
  end
  return wn % 2 ~= 0 -- even/odd rule
end

also if you change wn % 2 ~= 0 to wn ~= 0 it'll become the non-zero winding rule

@MrZ626 MrZ626 transferred this issue from 26F-Studio/Zframework Nov 19, 2022
@MrZ626
Copy link
Collaborator

MrZ626 commented Nov 19, 2022

I've seen this algorithm before, great

@MrZ626 MrZ626 closed this as completed in 8e34d96 Dec 1, 2022
@Not-A-Normal-Robot
Copy link
Member Author

what about zframework, which is still used by techmino?

@MrZ626
Copy link
Collaborator

MrZ626 commented Dec 1, 2022

I don't think it has a chance to be used

@Not-A-Normal-Robot
Copy link
Member Author

I used that function for making the star collision detection in 26F-Studio/Techmino#778

@MrZ626
Copy link
Collaborator

MrZ626 commented Dec 1, 2022

😨 okay

@Not-A-Normal-Robot
Copy link
Member Author

also maybe you can have 2 output variables: 1st one is non-zero rule and the 2nd one is the even/odd rule

@MrZ626
Copy link
Collaborator

MrZ626 commented Dec 1, 2022

I think so
time to push -f

@Not-A-Normal-Robot
Copy link
Member Author

looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants