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

Polygon.contains? bug #7

Open
xsxVeNxsx opened this issue Dec 9, 2013 · 6 comments
Open

Polygon.contains? bug #7

xsxVeNxsx opened this issue Dec 9, 2013 · 6 comments

Comments

@xsxVeNxsx
Copy link

arr = [Geometry::Point.new(1.6, 1.0), Geometry::Point.new(1.6, 2.0), 
     Geometry::Point.new(1.2, 2.0), Geometry::Point.new(1.2, 1.0)]
point = Geometry::Point.new(1.5, 1.5)
result = Geometry::Polygon.new(arr).contains?(point)

Result = true. It's ok. But:

arr = [Geometry::Point.new(1.6, 1.0), Geometry::Point.new(1.6, 2.0),
     Geometry::Point.new(1.2, 2.0), Geometry::Point.new(1.2, 1.0)]
point = Geometry::Point.new(1.5, 1.5)
result = Geometry::Polygon.new(arr).contains?(point)
(0..100).each{result &&= Geometry::Polygon.new(arr).contains?(point)}

result = false

@DanielVartanov
Copy link
Owner

Thanks for reporting a very interesting bug. I'll investigate what's happening there.

xsxVeNxsx added a commit to horror/d2d-shooter_game that referenced this issue Dec 11, 2013
ruby-geometry is issued uncorrect results with some data. Details on DanielVartanov/ruby-geometry#7
That's why a new implementation has been developed, without using third-party gems.
@seangransee
Copy link

Yes, it appears that Point.contains? is nondeterministic. Here's another example:

point = Geometry::Point.new(509, 553)

polygon = Geometry::Polygon.new([
  Geometry::Point.new(297, 581),
  Geometry::Point.new(464, 414),
  Geometry::Point.new(545, 495),
  Geometry::Point.new(383, 662)
])

results = Array.new(10000).map do
  polygon.contains?(point)
end

puts "contains? returned true #{results.count(true)} times"
puts "contains? returned false #{results.count(false)} times"

Running this prints the following output for me:

contains? returned true 515 times
contains? returned false 9485 times

I suspect this has something to do with the randomness introduced in point_in_polygon.rb line 54.

In my application, I've monkey-patched the Point.contains? method with this algorithm: PNPOLY - Point Inclusion in Polygon Test.

@DanielVartanov
Copy link
Owner

Hey @seangransee, can you please form a PR with your patch? This problem is indeed very serious.

@seangransee
Copy link

Sure! I'll try to take care of it sometime this week.

@DanielVartanov
Copy link
Owner

Thanks in advance!

@seangransee
Copy link

PR created in #17. It's not perfect yet, but it's a starting point.

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

3 participants