Class: Sangaku::Polygon

Inherits:
Object
  • Object
show all
Defined in:
lib/sangaku/polygon.rb

Instance Method Summary collapse

Constructor Details

#initialize(*points) ⇒ Polygon

Returns a new instance of Polygon.



5
6
7
8
9
10
# File 'lib/sangaku/polygon.rb', line 5

def initialize(*points)
  @points = Point::convert(points)
  @lines = nil
  @closed = false
  @aabb = nil
end

Instance Method Details

#<<(p) ⇒ Object



46
47
48
49
# File 'lib/sangaku/polygon.rb', line 46

def <<(p)
  @lines = nil
  @points << Point.new(*p)
end

#aabbObject



42
43
44
# File 'lib/sangaku/polygon.rb', line 42

def aabb
  @aabb ||= AABB.new(*@points)
end

#clear!Object



23
24
25
26
27
28
# File 'lib/sangaku/polygon.rb', line 23

def clear!
  @points.clear
  @closed = false
  @lines = nil
  @aabb = nil
end

#close!Object



16
17
18
19
20
21
# File 'lib/sangaku/polygon.rb', line 16

def close!
  return if @closed
  @closed = true
  @lines = nil
  @aabb = nil
end

#closed?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/sangaku/polygon.rb', line 12

def closed?
  @closed
end

#lengthObject



30
31
32
# File 'lib/sangaku/polygon.rb', line 30

def length
  @points.length
end

#linesObject



38
39
40
# File 'lib/sangaku/polygon.rb', line 38

def lines
  @lines ||= _points_to_lines
end

#pointsObject



34
35
36
# File 'lib/sangaku/polygon.rb', line 34

def points
  @points
end

#select(x = nil, y = nil) ⇒ Object



51
52
53
# File 'lib/sangaku/polygon.rb', line 51

def select(x = nil, y = nil)
  lines.select { |line| line.contain?(x, y) }
end

#to_aObject



55
56
57
# File 'lib/sangaku/polygon.rb', line 55

def to_a
  @points.map { |p| p.to_a }
end

#to_sObject Also known as: inspect



59
60
61
# File 'lib/sangaku/polygon.rb', line 59

def to_s
  @points.join('->')
end