class EsriShapefile::Ring

Attributes

direction[R]
points[R]

Public Class Methods

new(points) click to toggle source
# File lib/esri_shapefile/models/utils/ring.rb, line 5
def initialize(points)
  @points = points
  @direction = determine_direction(points)
end

Private Instance Methods

==(ring)
Alias for: eql?
determine_direction(points) click to toggle source

Sum over the edges, (x2 − x1)(y2 + y1). If the result is positive the curve is clockwise, if it's negative the curve is counter-clockwise. (The result is twice the enclosed area, with a +/- convention.)

point = (5,0) edge: (6-5)(4+0) = 4 point = (6,4) edge: (4-6)(5+4) = -18 point = (4,5) edge: (1-4)(5+5) = -30 point = (1,5) edge: (1-1)(0+5) = 0 point = (1,0) edge: (5-1)(0+0) = 0

---
-44 counter-clockwise
# File lib/esri_shapefile/models/utils/ring.rb, line 23
def determine_direction(points)
  sum_over_edges = points.
    each_cons(2).
    reduce(0) { |sum, (p1, p2)| sum += ((p2.x - p1.x) * (p2.y + p1.y)) }

  sum_over_edges < 0 ? :count_clockwise : :clockwise
end
eql?(ring) click to toggle source
# File lib/esri_shapefile/models/utils/ring.rb, line 31
def eql?(ring)
  ring.is_a?(Ring) && ring.points == @points && ring.direction == @direction
end
Also aliased as: ==