class MagicCloud::Rect

Utility geometrical rectangle, implementing arithmetic interactions with other rectangles (not to be confused with drawable shapes)

Attributes

x0[RW]
x1[RW]
y0[RW]
y1[RW]

Public Class Methods

new(x0, y0, x1, y1) click to toggle source
# File lib/magic_cloud/rect.rb, line 7
def initialize(x0, y0, x1, y1)
  @x0, @y0, @x1, @y1 = x0, y0, x1, y1
end

Public Instance Methods

adjust(other) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/magic_cloud/rect.rb, line 48
def adjust(other)
  dup.tap{|d| d.adjust!(other)}
end
adjust!(other) click to toggle source

rubocop:disable Metrics/AbcSize

# 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) click to toggle source
# 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) click to toggle source

rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/AbcSize

# 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() click to toggle source
# File lib/magic_cloud/rect.rb, line 20
def height
  @y1 - @y0
end
inspect() click to toggle source
# File lib/magic_cloud/rect.rb, line 80
def inspect
  "#<Rect[#{x0},#{y0};#{x1},#{y1}]>"
end
intersect(other) click to toggle source

rubocop:enable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity,Metrics/AbcSize

# 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) click to toggle source

shift to new coords, preserving the size

# 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() click to toggle source
# File lib/magic_cloud/rect.rb, line 84
def to_s
  "#<Rect[#{x0},#{y0};#{x1},#{y1}]>"
end
width() click to toggle source

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.
# File lib/magic_cloud/rect.rb, line 16
def width
  @x1 - @x0
end