Class: Skia::Rect

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left = 0.0, top = 0.0, right = 0.0, bottom = 0.0) ⇒ Rect

Returns a new instance of Rect.



7
8
9
10
11
12
# File 'lib/skia/rect.rb', line 7

def initialize(left = 0.0, top = 0.0, right = 0.0, bottom = 0.0)
  @left = left.to_f
  @top = top.to_f
  @right = right.to_f
  @bottom = bottom.to_f
end

Instance Attribute Details

#bottomObject

Returns the value of attribute bottom.



5
6
7
# File 'lib/skia/rect.rb', line 5

def bottom
  @bottom
end

#leftObject

Returns the value of attribute left.



5
6
7
# File 'lib/skia/rect.rb', line 5

def left
  @left
end

#rightObject

Returns the value of attribute right.



5
6
7
# File 'lib/skia/rect.rb', line 5

def right
  @right
end

#topObject

Returns the value of attribute top.



5
6
7
# File 'lib/skia/rect.rb', line 5

def top
  @top
end

Class Method Details

.from_struct(struct) ⇒ Object



22
23
24
# File 'lib/skia/rect.rb', line 22

def self.from_struct(struct)
  new(struct[:left], struct[:top], struct[:right], struct[:bottom])
end

.from_wh(width, height) ⇒ Object



18
19
20
# File 'lib/skia/rect.rb', line 18

def self.from_wh(width, height)
  new(0, 0, width, height)
end

.from_xywh(x, y, width, height) ⇒ Object



14
15
16
# File 'lib/skia/rect.rb', line 14

def self.from_xywh(x, y, width, height)
  new(x, y, x + width, y + height)
end

Instance Method Details

#==(other) ⇒ Object



109
110
111
112
113
114
# File 'lib/skia/rect.rb', line 109

def ==(other)
  return false unless other.is_a?(Rect)

  @left == other.left && @top == other.top &&
    @right == other.right && @bottom == other.bottom
end

#centerObject



51
52
53
# File 'lib/skia/rect.rb', line 51

def center
  Point.new(center_x, center_y)
end

#center_xObject



43
44
45
# File 'lib/skia/rect.rb', line 43

def center_x
  (@left + @right) / 2.0
end

#center_yObject



47
48
49
# File 'lib/skia/rect.rb', line 47

def center_y
  (@top + @bottom) / 2.0
end

#contains?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#contains_rect?(other) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/skia/rect.rb', line 63

def contains_rect?(other)
  @left <= other.left && @top <= other.top &&
    @right >= other.right && @bottom >= other.bottom
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @left >= @right || @top >= @bottom
end

#heightObject



39
40
41
# File 'lib/skia/rect.rb', line 39

def height
  @bottom - @top
end

#inset(dx, dy) ⇒ Object



105
106
107
# File 'lib/skia/rect.rb', line 105

def inset(dx, dy)
  self.class.new(@left + dx, @top + dy, @right - dx, @bottom - dy)
end

#intersect(other) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/skia/rect.rb', line 73

def intersect(other)
  return nil unless intersects?(other)

  self.class.new(
    [@left, other.left].max,
    [@top, other.top].min,
    [@right, other.right].min,
    [@bottom, other.bottom].max
  )
end

#intersects?(other) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/skia/rect.rb', line 68

def intersects?(other)
  @left < other.right && other.left < @right &&
    @top < other.bottom && other.top < @bottom
end

#offset(dx, dy) ⇒ Object



93
94
95
# File 'lib/skia/rect.rb', line 93

def offset(dx, dy)
  self.class.new(@left + dx, @top + dy, @right + dx, @bottom + dy)
end

#offset!(dx, dy) ⇒ Object



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

def offset!(dx, dy)
  @left += dx
  @top += dy
  @right += dx
  @bottom += dy
  self
end

#to_aObject



116
117
118
# File 'lib/skia/rect.rb', line 116

def to_a
  [@left, @top, @right, @bottom]
end

#to_structObject



26
27
28
29
30
31
32
33
# File 'lib/skia/rect.rb', line 26

def to_struct
  struct = Native::SKRect.new
  struct[:left] = @left
  struct[:top] = @top
  struct[:right] = @right
  struct[:bottom] = @bottom
  struct
end

#union(other) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/skia/rect.rb', line 84

def union(other)
  self.class.new(
    [@left, other.left].min,
    [@top, other.top].min,
    [@right, other.right].max,
    [@bottom, other.bottom].max
  )
end

#widthObject



35
36
37
# File 'lib/skia/rect.rb', line 35

def width
  @right - @left
end