Class: GreinerHormann::Vertex

Inherits:
Object
  • Object
show all
Defined in:
lib/greiner_hormann/vertex.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Vertex

Returns a new instance of Vertex.



5
6
7
8
9
10
11
12
13
# File 'lib/greiner_hormann/vertex.rb', line 5

def initialize(x, y)
  self.x = x
  self.y = y

  self.next_node = self.prev_node = self.corresponding = nil
  self.distance = 0.0
  self.is_entry = true
  self.is_intersection = self.visited = false
end

Instance Attribute Details

#correspondingObject

Returns the value of attribute corresponding.



3
4
5
# File 'lib/greiner_hormann/vertex.rb', line 3

def corresponding
  @corresponding
end

#distanceObject

Returns the value of attribute distance.



3
4
5
# File 'lib/greiner_hormann/vertex.rb', line 3

def distance
  @distance
end

#is_entryObject

Returns the value of attribute is_entry.



3
4
5
# File 'lib/greiner_hormann/vertex.rb', line 3

def is_entry
  @is_entry
end

#is_intersectionObject

Returns the value of attribute is_intersection.



3
4
5
# File 'lib/greiner_hormann/vertex.rb', line 3

def is_intersection
  @is_intersection
end

#next_nodeObject

Returns the value of attribute next_node.



3
4
5
# File 'lib/greiner_hormann/vertex.rb', line 3

def next_node
  @next_node
end

#prev_nodeObject

Returns the value of attribute prev_node.



3
4
5
# File 'lib/greiner_hormann/vertex.rb', line 3

def prev_node
  @prev_node
end

#visitedObject

Returns the value of attribute visited.



3
4
5
# File 'lib/greiner_hormann/vertex.rb', line 3

def visited
  @visited
end

#xObject

Returns the value of attribute x.



3
4
5
# File 'lib/greiner_hormann/vertex.rb', line 3

def x
  @x
end

#yObject

Returns the value of attribute y.



3
4
5
# File 'lib/greiner_hormann/vertex.rb', line 3

def y
  @y
end

Class Method Details

.create_intersection(x, y, distance) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/greiner_hormann/vertex.rb', line 15

def self.create_intersection(x, y, distance)
  vert = Vertex.new(x, y)
  vert.distance = distance
  vert.is_intersection = true
  vert.is_entry = false
  vert
end

Instance Method Details

#==(v) ⇒ Object



30
31
32
# File 'lib/greiner_hormann/vertex.rb', line 30

def ==(v)
  x == v.x && y == v.y
end

#equals(v) ⇒ Object



34
35
36
# File 'lib/greiner_hormann/vertex.rb', line 34

def equals(v)
  self == v
end

#is_inside(poly) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/greiner_hormann/vertex.rb', line 38

def is_inside(poly)
  odd_nodes = false
  vert = poly.first
  next_n = vert.next_node

  loop do
    if (vert.y < y && next_n.y >= y || next_n.y < y && vert.y >= y) && (vert.x <= x || next_n.x <= x)
      odd_nodes ^= vert.x + (y - vert.y) / (next_n.y - vert.y) * (next_n.x - vert.x) < x
    end

    vert = vert.next_node
    next_n = vert.next_node || poly.first

    break if vert == poly.first
  end
  odd_nodes
end

#visitObject



23
24
25
26
27
28
# File 'lib/greiner_hormann/vertex.rb', line 23

def visit
  self.visited = true
  if !corresponding.nil? && !corresponding.visited
    corresponding.visit
  end
end