Method: MagicCloud::Rect#intersect

Defined in:
lib/magic_cloud/rect.rb

#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