Class: AaBb

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

Overview

Axis aligned bounding box class (AABB would clash with Toxicgem)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center:, extent:) ⇒ AaBb

Returns a new instance of AaBb.



6
7
8
9
# File 'lib/jruby_art/helpers/aabb.rb', line 6

def initialize(center:, extent:)
  @center = center
  @extent = extent
end

Instance Attribute Details

#centerObject (readonly)

Returns the value of attribute center.



4
5
6
# File 'lib/jruby_art/helpers/aabb.rb', line 4

def center
  @center
end

#extentObject (readonly)

Returns the value of attribute extent.



4
5
6
# File 'lib/jruby_art/helpers/aabb.rb', line 4

def extent
  @extent
end

Class Method Details

.from_min_max(min:, max:) ⇒ Object



11
12
13
# File 'lib/jruby_art/helpers/aabb.rb', line 11

def self.from_min_max(min:, max:)
  new(center: (min + max) * 0.5, extent: max - min)
end

Instance Method Details

#contains?(vec) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/jruby_art/helpers/aabb.rb', line 24

def contains?(vec)
  rad = extent * 0.5
  return false unless (center.x - rad.x..center.x + rad.x).cover? vec.x
  (center.y - rad.y..center.y + rad.y).cover? vec.y
end

#position(vec) ⇒ Object



15
16
17
18
# File 'lib/jruby_art/helpers/aabb.rb', line 15

def position(vec)
  return @center = vec unless block_given?
  @center = vec if yield
end

#scale(d) ⇒ Object



20
21
22
# File 'lib/jruby_art/helpers/aabb.rb', line 20

def scale(d)
  @extent *= d
end