class WindingPolygon::Segment

A container class for segments (or edges) of the polygon to test Allows storage and retrieval from the Balanced Binary Tree

Attributes

above[RW]
below[RW]
edge[R]
left_point[RW]
right_point[RW]

Public Class Methods

new(event) click to toggle source
# File lib/winding-polygon/segment.rb, line 12
def initialize(event)
  @edge = event[:edge]
end

Public Instance Methods

<(other_segment) click to toggle source
# File lib/winding-polygon/segment.rb, line 16
def <(other_segment)
  if @left_point == other_segment.left_point
    return true if @right_point.is_left(other_segment.left_point,other_segment.right_point)<0
  end

  return true if @left_point.is_left(other_segment.left_point,other_segment.right_point)<0

  return false
end
<=>(other_segment) click to toggle source
# File lib/winding-polygon/segment.rb, line 41
def <=> other_segment
  raise Exception.new("Self is edge=#{@edge}, the other_segment is nil") if other_segment.nil?

  return 0  if self == other_segment
  return -1 if self < other_segment
  return  1
end
==(other_segment) click to toggle source
# File lib/winding-polygon/segment.rb, line 36
def == (other_segment)
  return true if @left_point == other_segment.left_point && @right_point == other_segment.right_point
  return false
end
>(other_segment) click to toggle source
# File lib/winding-polygon/segment.rb, line 26
def >(other_segment)
  if @left_point == other_segment.left_point
    return true if @right_point.is_left(other_segment.left_point,other_segment.right_point)>0
  end

  return true if @left_point.is_left(other_segment.left_point,other_segment.right_point)>0

  return false
end
intersection_point_with(other_segment) click to toggle source
# File lib/winding-polygon/segment.rb, line 53
def intersection_point_with(other_segment)
  numerator = (other_segment.left_point.y - @left_point.y) * (other_segment.left_point.x - other_segment.right_point.x) -
      (other_segment.left_point.y - other_segment.right_point.y) * (other_segment.left_point.x - @left_point.x)

  denominator = (@right_point.y - @left_point.y) * (other_segment.left_point.x - other_segment.right_point.x) -
      (other_segment.left_point.y - other_segment.right_point.y) * (@right_point.x - @left_point.x)

  return nil if denominator==0

  t = numerator.to_f / denominator

  x = @left_point.x + t * (@right_point.x - @left_point.x)
  y = @left_point.y + t * (@right_point.y - @left_point.y)

  Point.new(x, y)
end
is_intersected_with(other_segment) click to toggle source
# File lib/winding-polygon/segment.rb, line 70
def is_intersected_with(other_segment)
  # no intersect if either segment doesn't existend
  return false if other_segment.nil?

  #test for existence of an intersect point
  #other_segment left point sign
  lsign = other_segment.left_point.is_left(@left_point, @right_point)
  #other_segment right point sign
  rsign = other_segment.right_point.is_left(@left_point, @right_point)

  # other_segment endpoints have same sign relative to it  => on same side => no intersect is possible
  return false if (lsign * rsign > 0)

  # its left point sign
  lsign = @left_point.is_left(other_segment.left_point, other_segment.right_point)
  #its right point sign
  rsign = @right_point.is_left(other_segment.left_point, other_segment.right_point)

  # its endpoints have same sign relative to other_segment => on same side => no intersect is possible
  return false  if (lsign * rsign > 0)

  return true

end
is_on_the_line(point) click to toggle source
# File lib/winding-polygon/segment.rb, line 95
def is_on_the_line(point)
  return true if @left_point.x<point.x && point.x < @right_point.x && (@left_point.y<point.y && point.y < @right_point.y || @right_point.y<point.y && point.y < @left_point.y) && ((point.x - @left_point.x) / (@right_point.x - @left_point.x) - (point.y - @left_point.y) / (@right_point.y - @left_point.y)).abs<0.00000000001
  false
end
to_s() click to toggle source
# File lib/winding-polygon/segment.rb, line 49
def to_s
  return "edge:#{@edge.to_s}"
end