Class: PbfReverseGeocoder::PointInPolygon
- Inherits:
-
Object
- Object
- PbfReverseGeocoder::PointInPolygon
- Defined in:
- lib/pbf_reverse_geocoder/point_in_polygon.rb
Class Method Summary collapse
-
.contains?(point, polygon) ⇒ Boolean
点がポリゴン内にあるか判定 点からX軸正方向に伸ばした半直線が、ポリゴンの辺と何回交差するかを数える 奇数回 = 内側、偶数回 = 外側.
Class Method Details
.contains?(point, polygon) ⇒ Boolean
点がポリゴン内にあるか判定点からX軸正方向に伸ばした半直線が、ポリゴンの辺と何回交差するかを数える奇数回 = 内側、偶数回 = 外側
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pbf_reverse_geocoder/point_in_polygon.rb', line 16 def self.contains?(point, polygon) return false if polygon.nil? || polygon.empty? px, py = point inside = false # ポリゴンの各辺について交差判定 j = polygon.length - 1 polygon.length.times do |i| xi, yi = polygon[i] xj, yj = polygon[j] # Y座標の範囲チェック:辺が点のY座標をまたいでいるか if (yi > py) != (yj > py) # X座標の交差判定:半直線が辺と交差する点のX座標を計算 x_intersect = ((xj - xi) * (py - yi) / (yj - yi)) + xi # 点のX座標より右側で交差していたら、inside を反転 inside = !inside if px < x_intersect end j = i end inside end |