Class: Quartz::Rect

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyquartz/rect.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, size) ⇒ Rect

Returns a new instance of Rect.



30
31
32
33
# File 'lib/rubyquartz/rect.rb', line 30

def initialize(origin, size)
  @origin = origin.dup
  @size = size.dup
end

Instance Attribute Details

#originObject

Returns the value of attribute origin.



28
29
30
# File 'lib/rubyquartz/rect.rb', line 28

def origin
  @origin
end

#sizeObject

Returns the value of attribute size.



28
29
30
# File 'lib/rubyquartz/rect.rb', line 28

def size
  @size
end

Instance Method Details

#cut(edge, amount) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rubyquartz/rect.rb', line 66

def cut(edge, amount)
  origin = @origin.dup
  size = @size.dup
  case edge
  when :min_x
    origin.x += amount
    size.width -= amount
  when :min_y
    origin.y += amount
    size.height -= amount
  when :max_x
    size.width -= amount
  when :max_y
    size.height -= amount
  else
    raise "Unknown edge #{edge}"
  end
  
  if size.width < 0 or size.height < 0
    size.width = 0
    size.height = 0
  end
  Rect.new(origin, size)
end

#inset(dx, dy) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rubyquartz/rect.rb', line 35

def inset(dx, dy)
  o = Point.new(@origin.x + dx, @origin.y + dy)
  s = Size.new(@size.width - 2*dx, @size.height - 2*dy)
  if s.width < 0 or s.height < 0
    s.width = 0
    s.height = 0
  end
  Rect.new(o, s)
end

#integralObject

Returns a rect with integral coordinates that encloses the receiver



115
116
117
118
119
120
121
# File 'lib/rubyquartz/rect.rb', line 115

def integral
  x0 = min_x.floor
  y0 = min_y.floor
  x1 = max_x.ceil
  y1 = max_y.ceil
  Rect.new(Point.new(x0,y0), Size.new(x1-x0,y1-y0))
end

#max_xObject



48
49
50
# File 'lib/rubyquartz/rect.rb', line 48

def max_x
  @origin.x + @size.width
end

#max_yObject



55
56
57
# File 'lib/rubyquartz/rect.rb', line 55

def max_y
  @origin.y + @size.height
end

#mid_xObject



59
60
61
# File 'lib/rubyquartz/rect.rb', line 59

def mid_x
  @origin.x + @size.width / 2.0
end

#mid_yObject



62
63
64
# File 'lib/rubyquartz/rect.rb', line 62

def mid_y
  @origin.y + @size.height / 2.0
end

#min_xObject



45
46
47
# File 'lib/rubyquartz/rect.rb', line 45

def min_x
  @origin.x
end

#min_yObject



52
53
54
# File 'lib/rubyquartz/rect.rb', line 52

def min_y
  @origin.y
end

#to_sObject



123
124
125
# File 'lib/rubyquartz/rect.rb', line 123

def to_s
  "(#{@origin}, #{@size})"
end

#union!(r) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rubyquartz/rect.rb', line 91

def union!(r)
  _min_x = min_x
  _min_y = min_y
  _max_x = max_x
  _max_y = max_y
  
  other_min_x = r.min_x
  other_min_y = r.min_y
  other_max_x = r.max_x
  other_max_y = r.max_y
  
  _min_x = other_min_x if other_min_x < _min_x
  _min_y = other_min_y if other_min_y < _min_y
  
  _max_x = other_max_x if other_max_x > _max_x
  _max_y = other_max_y if other_max_y > _max_y
  
  @origin.x = _min_x
  @origin.y = _min_y
  @size.width = _max_x - _min_x
  @size.height = _max_y - _min_y
end