Class: WindingPolygon::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/winding-polygon/segment.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ Segment

Returns a new instance of Segment.



12
13
14
# File 'lib/winding-polygon/segment.rb', line 12

def initialize(event)
  @edge = event[:edge]
end

Instance Attribute Details

#aboveObject

Returns the value of attribute above.



8
9
10
# File 'lib/winding-polygon/segment.rb', line 8

def above
  @above
end

#belowObject

Returns the value of attribute below.



9
10
11
# File 'lib/winding-polygon/segment.rb', line 9

def below
  @below
end

#edgeObject (readonly)

Returns the value of attribute edge.



5
6
7
# File 'lib/winding-polygon/segment.rb', line 5

def edge
  @edge
end

#left_pointObject

Returns the value of attribute left_point.



6
7
8
# File 'lib/winding-polygon/segment.rb', line 6

def left_point
  @left_point
end

#right_pointObject

Returns the value of attribute right_point.



7
8
9
# File 'lib/winding-polygon/segment.rb', line 7

def right_point
  @right_point
end

Instance Method Details

#<(other_segment) ⇒ Object



16
17
18
19
20
21
22
23
24
# 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) ⇒ Object

Raises:

  • (Exception)


41
42
43
44
45
46
47
# 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) ⇒ Object



36
37
38
39
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object



95
96
97
98
# 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_sObject



49
50
51
# File 'lib/winding-polygon/segment.rb', line 49

def to_s
  return "edge:#{@edge.to_s}"
end