Class: VectorBeWinding::Rect

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Shape

#contains?, #intersects?

Constructor Details

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

Returns a new instance of Rect.



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

def initialize(left, top, right, bottom)
  @left, @right = if left <= right then [left, right] else [right, left] end
  @top, @bottom = if top <= bottom then [top, bottom] else [bottom, top] end
end

Instance Attribute Details

#bottomObject (readonly)

Returns the value of attribute bottom.



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

def bottom
  @bottom
end

#leftObject (readonly)

Returns the value of attribute left.



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

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right.



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

def right
  @right
end

#topObject (readonly)

Returns the value of attribute top.



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

def top
  @top
end

Class Method Details

.range_containingness(a0, a1, b0, b1) ⇒ Object

check [a0, a0] contains [b0, b1]



69
70
71
72
73
74
75
# File 'lib/vector_be_winding/rect.rb', line 69

def self.range_containingness(a0, a1, b0, b1)
  if a0 <= b0 && b1 <= a1
    (b0 - a0) * (a1 - b1)
  else
    -1
  end
end

.range_intersectedness(a0, a1, b0, b1) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/vector_be_winding/rect.rb', line 54

def self.range_intersectedness(a0, a1, b0, b1)
  if a0 < b0
    a1 - b0
  else
    b1 - a0
  end
end

.with_vectors(v0, v1) ⇒ Object



10
11
12
# File 'lib/vector_be_winding/rect.rb', line 10

def self.with_vectors(v0, v1)
  self.new(v0.x, v0.y, v1.x, v1.y)
end

Instance Method Details

#&(rect1) ⇒ Object



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

def &(rect1)
  newLeft = [left, rect1.left].max
  newTop = [top, rect1.top].max
  newRight = [right, rect1.right].min
  newBottom = [bottom, rect1.bottom].min

  if (newLeft <= newRight && newTop <= newBottom) 
    Rect.new(newLeft, newTop, newRight, newBottom)
  else
    nil
  end
end

#==(rect1) ⇒ Object



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

def ==(rect1)
  left == rect1.left && top == rect1.top &&
    right == rect1.right && bottom == rect1.bottom
end

#bounding_rectObject



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

def bounding_rect
  self
end

#containingness(shape1) ⇒ Object



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

def containingness(shape1)
  b = shape1.bounding_rect
  [Rect.range_containingness(left, right, b.left, b.right),
   Rect.range_containingness(top, bottom, b.top, b.bottom)].min
end

#empty?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/vector_be_winding/rect.rb', line 23

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

#intersectedness(shape1) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/vector_be_winding/rect.rb', line 45

def intersectedness(shape1)
  if shape1.class == Rect
    [Rect.range_intersectedness(left, right, shape1.left, shape1.right),
     Rect.range_intersectedness(top, bottom, shape1.top, shape1.bottom)].min
  else
    shape1.intersectedness(self)
  end
end

#|(rect1) ⇒ Object



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

def |(rect1)
  Rect.new([left, rect1.left].min, [top, rect1.top].min,
           [right, rect1.right].max, [bottom, rect1.bottom].max)
end