Class: Sangaku::AABB

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

Instance Method Summary collapse

Constructor Details

#initialize(*points) ⇒ AABB

Returns a new instance of AABB.



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

def initialize(*points)
  points = Point::convert(points)
  xs = points.map { |p| p.x }
  ys = points.map { |p| p.y }
  @line = Line.new([xs.min || 0, ys.min || 0], [xs.max || 0, ys.max || 0])
end

Instance Method Details

#*(scale) ⇒ Object



33
34
35
36
37
38
# File 'lib/sangaku/aabb.rb', line 33

def *(scale)
  mid = @line.mid
  dx = 0.5 * w * scale
  dy = 0.5 * h * scale
  AABB.new([mid.x - dx, mid.y - dy], [mid.x + dx, mid.y + dy])
end

#centre!(point = [0, 0]) ⇒ Object



19
20
21
22
# File 'lib/sangaku/aabb.rb', line 19

def centre!(point = [0, 0])
  point = Point.new(*point) if point.is_a?(Array)
  @line -= (@line.mid - point)
end

#hObject



17
# File 'lib/sangaku/aabb.rb', line 17

def h; @line.h; end

#maxObject



13
# File 'lib/sangaku/aabb.rb', line 13

def max; @line.p2; end

#midObject



14
# File 'lib/sangaku/aabb.rb', line 14

def mid; @line.mid; end

#minObject



12
# File 'lib/sangaku/aabb.rb', line 12

def min; @line.p1; end

#square!Object



24
25
26
27
28
29
30
31
# File 'lib/sangaku/aabb.rb', line 24

def square!
  mid = @line.mid
  ds = 0.5 * [w, h].max
  @line.p1.x = mid.x - ds
  @line.p1.y = mid.y - ds
  @line.p2.x = mid.x + ds
  @line.p2.y = mid.y + ds
end

#to_aObject



47
# File 'lib/sangaku/aabb.rb', line 47

def to_a; @line.to_a; end

#to_grid(count = 5) ⇒ Object



40
41
42
43
44
45
# File 'lib/sangaku/aabb.rb', line 40

def to_grid(count = 5)
  raise if count < 2
  xs = (min.x..max.x).step(w/(count-1).to_f)
  ys = (min.y..max.y).step(h/(count-1).to_f)
  Grid.new(xs.to_a, ys.to_a)
end

#to_sObject Also known as: inspect



48
# File 'lib/sangaku/aabb.rb', line 48

def to_s; @line.to_s; end

#wObject



16
# File 'lib/sangaku/aabb.rb', line 16

def w; @line.w; end