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
rubocop:enable Metrics/AbcSize.
-
#adjust!(other) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #collide?(other) ⇒ Boolean
-
#criss_cross?(other) ⇒ Boolean
rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/AbcSize.
- #height ⇒ Object
-
#initialize(x0, y0, x1, y1) ⇒ Rect
constructor
A new instance of Rect.
- #inspect ⇒ Object
-
#intersect(other) ⇒ Object
rubocop:enable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/AbcSize.
-
#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.
7 8 9 |
# File 'lib/magic_cloud/rect.rb', line 7 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.
11 12 13 |
# File 'lib/magic_cloud/rect.rb', line 11 def x0 @x0 end |
#x1 ⇒ Object
Returns the value of attribute x1.
11 12 13 |
# File 'lib/magic_cloud/rect.rb', line 11 def x1 @x1 end |
#y0 ⇒ Object
Returns the value of attribute y0.
11 12 13 |
# File 'lib/magic_cloud/rect.rb', line 11 def y0 @y0 end |
#y1 ⇒ Object
Returns the value of attribute y1.
11 12 13 |
# File 'lib/magic_cloud/rect.rb', line 11 def y1 @y1 end |
Instance Method Details
#adjust(other) ⇒ Object
rubocop:enable Metrics/AbcSize
48 49 50 |
# File 'lib/magic_cloud/rect.rb', line 48 def adjust(other) dup.tap{|d| d.adjust!(other)} end |
#adjust!(other) ⇒ Object
rubocop:disable Metrics/AbcSize
40 41 42 43 44 45 |
# File 'lib/magic_cloud/rect.rb', line 40 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
24 25 26 27 28 29 |
# File 'lib/magic_cloud/rect.rb', line 24 def collide?(other) @x1 > other.x0 && @x0 < other.x1 && @y1 > other.y0 && @y0 < other.y1 end |
#criss_cross?(other) ⇒ Boolean
rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/AbcSize
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
20 21 22 |
# File 'lib/magic_cloud/rect.rb', line 20 def height @y1 - @y0 end |
#inspect ⇒ Object
80 81 82 |
# File 'lib/magic_cloud/rect.rb', line 80 def inspect "#<Rect[#{x0},#{y0};#{x1},#{y1}]>" end |
#intersect(other) ⇒ Object
rubocop:enable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/AbcSize
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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 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 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
32 33 34 35 36 37 |
# File 'lib/magic_cloud/rect.rb', line 32 def move_to(x, y) @x1 += x - @x0 @y1 += y - @y0 @x0 = x @y0 = y end |
#to_s ⇒ Object
84 85 86 |
# File 'lib/magic_cloud/rect.rb', line 84 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.
16 17 18 |
# File 'lib/magic_cloud/rect.rb', line 16 def width @x1 - @x0 end |