Class: GuiGeo::Rectangle

Inherits:
Struct
  • Object
show all
Includes:
Tools
Defined in:
lib/gui_geometry/rectangle.rb

Constant Summary

Constants included from GuiGeo

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tools

#clone_value, #max, #min, #minmax

Methods included from GuiGeo

#point, #rect

Constructor Details

#initialize(*args) ⇒ Rectangle

Returns a new instance of Rectangle.



5
6
7
8
9
10
11
12
13
# File 'lib/gui_geometry/rectangle.rb', line 5

def initialize(*args)
  case args.length
  when 0 then super point, point
  when 1 then super point, args[0].clone
  when 2 then super args[0].clone, args[1].clone
  when 4 then super point(*args[0..1]), point(*args[2..3])
  else raise ArgumentError.new
  end
end

Instance Attribute Details

#locObject

Returns the value of attribute loc

Returns:

  • (Object)

    the current value of loc



2
3
4
# File 'lib/gui_geometry/rectangle.rb', line 2

def loc
  @loc
end

#sizeObject

Returns the value of attribute size

Returns:

  • (Object)

    the current value of size



2
3
4
# File 'lib/gui_geometry/rectangle.rb', line 2

def size
  @size
end

Instance Method Details

#+(b) ⇒ Object



15
# File 'lib/gui_geometry/rectangle.rb', line 15

def +(b) b.kind_of?(Point) ? rect(loc+b, size) : rect(loc+b.loc, size+b.size) end

#-(b) ⇒ Object



16
# File 'lib/gui_geometry/rectangle.rb', line 16

def -(b) b.kind_of?(Point) ? rect(loc-b, size) : rect(loc-b.loc, size-b.size) end

#areaObject



52
# File 'lib/gui_geometry/rectangle.rb', line 52

def area; size.area; end

#blObject



90
# File 'lib/gui_geometry/rectangle.rb', line 90

def bl; point(x, y + h); end

#blank?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/gui_geometry/rectangle.rb', line 22

def blank?
  !present?
end

#bottomObject



96
# File 'lib/gui_geometry/rectangle.rb', line 96

def bottom; loc.x + size.y; end

#bound(val) ⇒ Object

val can be a Rectangle or Point returns a Rectangle or Point that is within this Rectangle For Rectangles, the size is only changed if it has to be



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gui_geometry/rectangle.rb', line 33

def bound(val)
  case val
  when Rectangle then
    r = val.clone
    r.size = r.size.min(size)
    r.loc = r.loc.bound(loc, loc + size - val.size)
    r
  when Point then (val-loc).bound(point, size) + loc
  else raise ArgumentError.new("wrong type: (#{val.class}) - Rectangle or Point expected")
  end
end

#brObject



89
# File 'lib/gui_geometry/rectangle.rb', line 89

def br; loc + size; end

#cloneObject



26
27
28
# File 'lib/gui_geometry/rectangle.rb', line 26

def clone
  rect loc.clone, size.clone
end

#contains?(val) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
# File 'lib/gui_geometry/rectangle.rb', line 77

def contains?(val)
  case val
  when nil then false
  when Point then
    val >= loc && val < (loc+size)
  when Rectangle then
    (self & val) == self
  else raise ArgumentError.new("wrong type: (#{val.class}) - Rectangle or Point expected")
  end
end

#disjoint(b) ⇒ Object

return 1-3 rectangles which, together, cover exactly the same area as self and b, but do not overlapp



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gui_geometry/rectangle.rb', line 117

def disjoint(b)
  return self if !b || contains(b)
  return b if b.contains(self)
  return self,b unless overlaps?(b)
  tl_contained = contains?(b.tl) ? 1 : 0
  tr_contained = contains?(b.tr) ? 1 : 0
  bl_contained = contains?(b.bl) ? 1 : 0
  br_contained = contains?(b.br) ? 1 : 0
  sum = tl_contained + tr_contained + bl_contained + br_contained
  case sum
  when 0 # rectangles form a plus "+"
    if b.y < self.y
      r1,r2 = b,self
    else
      r1,r2 = self,b
    end
    tl1 = r1.tl
    br1 = point(r1.right, r2.top)
    tl2 = point(r1.left, r2.bottom)
    br2 = r1.br
    [
      r2,
      rect(tl1,br1-tl1),
      rect(tl2,br2-tl2),
    ]
  when 1
  when 2
  else raise "internal error in disjoint - cases 3 and 4 should be taken care of by the return-tests at the top of the method"
  end




end

#hObject Also known as: height



48
# File 'lib/gui_geometry/rectangle.rb', line 48

def h; size.h; end

#inspectObject



57
58
59
# File 'lib/gui_geometry/rectangle.rb', line 57

def inspect
  "rect"+to_s
end

#intersection(b) ⇒ Object Also known as: |



107
108
109
110
111
112
113
# File 'lib/gui_geometry/rectangle.rb', line 107

def intersection(b)
  return self unless b
  l = loc.max(b.loc)
  s = br.min(b.br) - l
  return rect unless s>point
  rect l, s
end

#leftObject



94
# File 'lib/gui_geometry/rectangle.rb', line 94

def left; loc.y; end

#overlaps?(val) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gui_geometry/rectangle.rb', line 65

def overlaps?(val)
  case val
  when nil then false
  when Point then
    contains?(val)
  when Rectangle then
    val.loc + val.size > loc &&
    loc + size > val.loc
  else raise ArgumentError.new("wrong type: (#{val.class}) - Rectangle or Point expected")
  end
end

#present?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/gui_geometry/rectangle.rb', line 18

def present?
  w > 0 && h > 0
end

#rightObject



95
# File 'lib/gui_geometry/rectangle.rb', line 95

def right; loc.x + size.x; end

#tlObject



88
# File 'lib/gui_geometry/rectangle.rb', line 88

def tl; loc; end

#to_sObject



61
62
63
# File 'lib/gui_geometry/rectangle.rb', line 61

def to_s
  "(#{[loc.x,loc.y,size.x,size.y].join ','})"
end

#topObject



93
# File 'lib/gui_geometry/rectangle.rb', line 93

def top; loc.x; end

#trObject



91
# File 'lib/gui_geometry/rectangle.rb', line 91

def tr; point(x + w, y); end

#union(b) ⇒ Object Also known as: &



99
100
101
102
103
104
# File 'lib/gui_geometry/rectangle.rb', line 99

def union(b)
  return clone unless b
  l = loc.min(b.loc)
  s = br.max(b.br) - l
  rect l, s
end

#wObject Also known as: width



47
# File 'lib/gui_geometry/rectangle.rb', line 47

def w; size.w; end

#xObject



45
# File 'lib/gui_geometry/rectangle.rb', line 45

def x; loc.x; end

#x_rangeObject



54
# File 'lib/gui_geometry/rectangle.rb', line 54

def x_range; x ... (x + w) end

#yObject



46
# File 'lib/gui_geometry/rectangle.rb', line 46

def y; loc.y; end

#y_rangeObject



55
# File 'lib/gui_geometry/rectangle.rb', line 55

def y_range; y ... (y + h) end