Class: Geokit::Polygon

Inherits:
Object
  • Object
show all
Defined in:
lib/geokit/mappable.rb

Overview

A complex polygon made of multiple points. End point must equal start point to close the poly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points) ⇒ Polygon

Returns a new instance of Polygon.



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/geokit/mappable.rb', line 566

def initialize(points)
  # Pass in an array of Geokit::LatLng
  @poly_x = []
  @poly_y = []

  points.each do |point|
    @poly_x << point.lng
    @poly_y << point.lat
  end
  
  # A Polygon must be 'closed', the last point equal to the first point
  if not @poly_x[0] == @poly_x[-1] or not @poly_y[0] == @poly_y[-1]
    # Append the first point to the array to close the polygon
    @poly_x << @poly_x[0]
    @poly_y << @poly_y[0]
  end
  
end

Instance Attribute Details

#poly_xObject

Returns the value of attribute poly_x.



564
565
566
# File 'lib/geokit/mappable.rb', line 564

def poly_x
  @poly_x
end

#poly_yObject

Returns the value of attribute poly_y.



564
565
566
# File 'lib/geokit/mappable.rb', line 564

def poly_y
  @poly_y
end

Instance Method Details

#contains?(point) ⇒ Boolean

Returns:

  • (Boolean)


585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/geokit/mappable.rb', line 585

def contains?(point)
  j = @poly_x.length - 1
  oddNodes = false
  x = point.lng
  y = point.lat

  for i in (0..j)
    if (@poly_y[i] < y && @poly_y[j] >= y ||
        @poly_y[j] < y && @poly_y[i] >= y)
      if (@poly_x[i] + (y - @poly_y[i]) / (@poly_y[j] - @poly_y[i]) * (@poly_x[j] - @poly_x[i]) < x)
        oddNodes = !oddNodes
      end
    end
 
    j=i
  end

  oddNodes
end