-
-
Save PikachuEXE/71c6d0961e6bdc31846f to your computer and use it in GitHub Desktop.
Ruby script for benchmark of ruby-geometry`PointInPolygon#random_ray` and `PointInPolygon#choose_good_ray`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'ruby-geometry', git: 'https://github.com/DanielVartanov/ruby-geometry.git' | |
GEMFILE | |
system 'bundle' | |
end | |
require 'bundler' | |
Bundler.setup(:default) | |
require 'geometry' | |
require 'benchmark' | |
corrdinates_raw = "114.146011,22.284090 114.145050,22.282860 114.146240,22.279619 114.151176,22.277014 114.151833,22.276251 114.151443,22.275000 114.151550,22.273470 114.153198,22.272249 114.156143,22.272970 114.161690,22.271900 114.162521,22.274820 114.162697,22.276211 114.160957,22.275850 114.159927,22.276190 114.158340,22.276270 114.157082,22.276661 114.155312,22.276310 114.153732,22.278000 114.154312,22.278870 114.153992,22.279900 114.152321,22.280890 114.150261,22.282721 114.146011,22.284090" | |
corrdinates = corrdinates_raw.split(/\s+/).map do |raw_coor| | |
# latitude and longitude are reversed since they are from KML | |
raw_coor.split(',').map(&:to_f).reverse | |
end | |
point = Geometry::Point.new(22.2817, 114.14932) | |
vertices = corrdinates.map do |corrdinate_array| | |
Geometry::Point.new_by_array(corrdinate_array) | |
end | |
polygon = Geometry::Polygon.new(vertices) | |
point_in_polygon = Geometry::PointInPolygon.new(point, polygon) | |
Benchmark.bm(25) do |x| | |
x.report("random_ray") { point_in_polygon.send(:random_ray) } | |
x.report("choose_good_ray") { point_in_polygon.send(:choose_good_ray) } | |
count = 0 | |
x.report("choose_good_ray(with_count)") do | |
count = 0 | |
ray = point_in_polygon.send(:random_ray) | |
while !point_in_polygon.send(:good_ray?, ray) | |
ray = point_in_polygon.send(:random_ray) | |
count += 1 | |
end | |
ray | |
end | |
puts "choose_good_ray has looped #{count} times" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment