Class: Uzi::Rect

Inherits:
Object
  • Object
show all
Defined in:
lib/uzi-rect.rb,
lib/uzi-rect/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Rect

Returns a new instance of Rect.



5
6
7
8
9
10
11
# File 'lib/uzi-rect.rb', line 5

def initialize(*args)
  if args.empty?
    @x, @y, @width, @height = 0, 0, 0, 0
  else
    @x, @y, @width, @height = *args
  end
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/uzi-rect.rb', line 3

def height
  @height
end

#widthObject

Returns the value of attribute width.



3
4
5
# File 'lib/uzi-rect.rb', line 3

def width
  @width
end

#xObject

Returns the value of attribute x.



3
4
5
# File 'lib/uzi-rect.rb', line 3

def x
  @x
end

#yObject

Returns the value of attribute y.



3
4
5
# File 'lib/uzi-rect.rb', line 3

def y
  @y
end

Instance Method Details

#bottomObject



28
# File 'lib/uzi-rect.rb', line 28

def bottom; @y + @height; end

#bottom=(y) ⇒ Object



29
# File 'lib/uzi-rect.rb', line 29

def bottom=(y); @y = y - @height; end

#bottomleftObject



49
50
51
52
53
54
# File 'lib/uzi-rect.rb', line 49

def bottomleft
  [
    left,
    bottom
  ]
end

#bottomleft=(loc) ⇒ Object



56
57
58
59
# File 'lib/uzi-rect.rb', line 56

def bottomleft=(loc)
  self.left = loc[0]
  self.bottom = loc[1]
end

#bottomrightObject



62
63
64
65
66
67
# File 'lib/uzi-rect.rb', line 62

def bottomright
  [
    right,
    bottom
  ]
end

#bottomright=(loc) ⇒ Object



69
70
71
72
# File 'lib/uzi-rect.rb', line 69

def bottomright=(loc)
  self.right = loc[0]
  self.bottom = loc[1]
end

#centerObject

Center point



75
76
77
78
79
80
# File 'lib/uzi-rect.rb', line 75

def center
  [
    @x + (@width / 2.0),
    @y + (@height / 2.0)
  ]
end

#center=(loc) ⇒ Object



82
83
84
85
# File 'lib/uzi-rect.rb', line 82

def center=(loc)
  @x = loc[0] - (@width / 2.0)
  @y = loc[1] - (@height / 2.0)
end

#center_xObject



88
# File 'lib/uzi-rect.rb', line 88

def center_x; center[0]; end

#center_x=(x) ⇒ Object



91
92
93
94
95
96
# File 'lib/uzi-rect.rb', line 91

def center_x=(x)
  c = center
  c[0] = x

  self.center = c
end

#center_yObject



89
# File 'lib/uzi-rect.rb', line 89

def center_y; center[1]; end

#center_y=(y) ⇒ Object



98
99
100
101
102
103
# File 'lib/uzi-rect.rb', line 98

def center_y=(y)
  c = center
  c[1] = y

  self.center = c
end

#contains?(loc) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/uzi-rect.rb', line 105

def contains?(loc)
  x, y = *loc
  x >= left && x <= right && y <= bottom && y >= top
end

#leftObject

Left, right, top and bottom edges



19
# File 'lib/uzi-rect.rb', line 19

def left; @x; end

#left=(x) ⇒ Object



20
# File 'lib/uzi-rect.rb', line 20

def left=(x); @x = x; end

#rightObject



22
# File 'lib/uzi-rect.rb', line 22

def right; @x + @width; end

#right=(x) ⇒ Object



23
# File 'lib/uzi-rect.rb', line 23

def right=(x); @x = x - @width; end

#to_aObject



13
14
15
# File 'lib/uzi-rect.rb', line 13

def to_a
  [ @x, @y, @width, @height ]
end

#topObject



25
# File 'lib/uzi-rect.rb', line 25

def top; @y; end

#top=(y) ⇒ Object



26
# File 'lib/uzi-rect.rb', line 26

def top=(y); @y = y; end

#topleftObject

Corner points



33
# File 'lib/uzi-rect.rb', line 33

def topleft; [ @x, @y ]; end

#topleft=(loc) ⇒ Object



34
# File 'lib/uzi-rect.rb', line 34

def topleft=(loc); @x, @y = loc[0], loc[1]; end

#toprightObject



36
37
38
39
40
41
# File 'lib/uzi-rect.rb', line 36

def topright
  [
    right,
    top
  ]
end

#topright=(loc) ⇒ Object



43
44
45
46
# File 'lib/uzi-rect.rb', line 43

def topright=(loc)
  self.right = loc[0]
  self.top = loc[1]
end