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

#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

#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: |


102
103
104
105
106
107
108
# File 'lib/gui_geometry/rectangle.rb', line 102

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

#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

#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

#trObject


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

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

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


94
95
96
97
98
99
# File 'lib/gui_geometry/rectangle.rb', line 94

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 - 1) 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 - 1) end