Class: MagicCloud::Rect
- Inherits:
-
Object
- Object
- MagicCloud::Rect
- Defined in:
- lib/magic_cloud/rect.rb
Overview
Utility geometrical rectangle, implementing arithmetic interactions with other rectangles (not to be confused with drawable shapes)
Instance Attribute Summary collapse
-
#x0 ⇒ Object
Returns the value of attribute x0.
-
#x1 ⇒ Object
Returns the value of attribute x1.
-
#y0 ⇒ Object
Returns the value of attribute y0.
-
#y1 ⇒ Object
Returns the value of attribute y1.
Instance Method Summary collapse
- #adjust(other) ⇒ Object
- #adjust!(other) ⇒ Object
- #collide?(other) ⇒ Boolean
-
#criss_cross?(other) ⇒ Boolean
rubocop:disable Metrics/CyclomaticComplexity.
- #height ⇒ Object
-
#initialize(x0, y0, x1, y1) ⇒ Rect
constructor
A new instance of Rect.
- #inspect ⇒ Object
-
#intersect(other) ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity.
-
#move_to(x, y) ⇒ Object
shift to new coords, preserving the size.
- #to_s ⇒ Object
-
#width ⇒ Object
NB: we are trying to use instance variables instead of accessors inside this class methods, because they are called so many times that accessor overhead IS significant.
Constructor Details
#initialize(x0, y0, x1, y1) ⇒ Rect
Returns a new instance of Rect.
8 9 10 |
# File 'lib/magic_cloud/rect.rb', line 8 def initialize(x0, y0, x1, y1) @x0, @y0, @x1, @y1 = x0, y0, x1, y1 end |
Instance Attribute Details
#x0 ⇒ Object
Returns the value of attribute x0.
12 13 14 |
# File 'lib/magic_cloud/rect.rb', line 12 def x0 @x0 end |
#x1 ⇒ Object
Returns the value of attribute x1.
12 13 14 |
# File 'lib/magic_cloud/rect.rb', line 12 def x1 @x1 end |
#y0 ⇒ Object
Returns the value of attribute y0.
12 13 14 |
# File 'lib/magic_cloud/rect.rb', line 12 def y0 @y0 end |
#y1 ⇒ Object
Returns the value of attribute y1.
12 13 14 |
# File 'lib/magic_cloud/rect.rb', line 12 def y1 @y1 end |
Instance Method Details
#adjust(other) ⇒ Object
48 49 50 |
# File 'lib/magic_cloud/rect.rb', line 48 def adjust(other) dup.tap { |d| d.adjust!(other) } end |
#adjust!(other) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/magic_cloud/rect.rb', line 41 def adjust!(other) @x0 = other.x0 if other.x0 < @x0 @y0 = other.y0 if other.y0 < @y0 @x1 = other.x1 if other.x1 > @x1 @y1 = other.y1 if other.y1 > @y1 end |
#collide?(other) ⇒ Boolean
26 27 28 29 30 31 |
# File 'lib/magic_cloud/rect.rb', line 26 def collide?(other) @x1 > other.x0 && @x0 < other.x1 && @y1 > other.y0 && @y0 < other.y1 end |
#criss_cross?(other) ⇒ Boolean
rubocop:disable Metrics/CyclomaticComplexity
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/magic_cloud/rect.rb', line 53 def criss_cross?(other) # case 1: this one is horizontal: # overlaps other by x, to right and left, and goes inside it by y @x0 < other.x0 && @x1 > other.x1 && @y0 > other.y0 && @y1 < other.y1 || # case 2: this one is vertical: # overlaps other by y, to top and bottom, and goes inside it by x @y0 < other.y0 && @y1 > other.y1 && @x0 > other.x0 && @x1 < other.x1 end |
#height ⇒ Object
22 23 24 |
# File 'lib/magic_cloud/rect.rb', line 22 def height @y1 - @y0 end |
#inspect ⇒ Object
82 83 84 |
# File 'lib/magic_cloud/rect.rb', line 82 def inspect "#<Rect[#{x0},#{y0};#{x1},#{y1}]>" end |
#intersect(other) ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/magic_cloud/rect.rb', line 65 def intersect(other) # direct comparison is dirtier, yet significantly faster than # something like [@x0, other.x0].max # rubocop:disable Style/MinMaxComparison ix0 = @x0 > other.x0 ? @x0 : other.x0 ix1 = @x1 < other.x1 ? @x1 : other.x1 iy0 = @y0 > other.y0 ? @y0 : other.y0 iy1 = @y1 < other.y1 ? @y1 : other.y1 # rubocop:enable Style/MinMaxComparison if ix0 > ix1 || iy0 > iy1 nil # rectangles are not intersected, in fact else Rect.new(ix0, iy0, ix1, iy1) end end |
#move_to(x, y) ⇒ Object
shift to new coords, preserving the size
34 35 36 37 38 39 |
# File 'lib/magic_cloud/rect.rb', line 34 def move_to(x, y) @x1 += x - @x0 @y1 += y - @y0 @x0 = x @y0 = y end |
#to_s ⇒ Object
86 87 88 |
# File 'lib/magic_cloud/rect.rb', line 86 def to_s "#<Rect[#{x0},#{y0};#{x1},#{y1}]>" end |
#width ⇒ Object
NB: we are trying to use instance variables instead of accessors
inside this class methods, because they are called so many
times that accessor overhead IS significant.
18 19 20 |
# File 'lib/magic_cloud/rect.rb', line 18 def width @x1 - @x0 end |