Class: MagicCloud::Rect

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#x0Object

Returns the value of attribute x0.



11
12
13
# File 'lib/magic_cloud/rect.rb', line 11

def x0
  @x0
end

#x1Object

Returns the value of attribute x1.



11
12
13
# File 'lib/magic_cloud/rect.rb', line 11

def x1
  @x1
end

#y0Object

Returns the value of attribute y0.



11
12
13
# File 'lib/magic_cloud/rect.rb', line 11

def y0
  @y0
end

#y1Object

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

Returns:

  • (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

Returns:

  • (Boolean)


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

#heightObject



20
21
22
# File 'lib/magic_cloud/rect.rb', line 20

def height
  @y1 - @y0
end

#inspectObject



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_sObject



84
85
86
# File 'lib/magic_cloud/rect.rb', line 84

def to_s
  "#<Rect[#{x0},#{y0};#{x1},#{y1}]>"
end

#widthObject

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