Class: VectorBeWinding::Rect
- Defined in:
- lib/vector_be_winding/rect.rb
Instance Attribute Summary collapse
-
#bottom ⇒ Object
readonly
Returns the value of attribute bottom.
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
-
#top ⇒ Object
readonly
Returns the value of attribute top.
Class Method Summary collapse
-
.range_containingness(a0, a1, b0, b1) ⇒ Object
check [a0, a0] contains [b0, b1].
- .range_intersectedness(a0, a1, b0, b1) ⇒ Object
- .with_vectors(v0, v1) ⇒ Object
Instance Method Summary collapse
- #&(rect1) ⇒ Object
- #==(rect1) ⇒ Object
- #bounding_rect ⇒ Object
- #containingness(shape1) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(left, top, right, bottom) ⇒ Rect
constructor
A new instance of Rect.
- #intersectedness(shape1) ⇒ Object
- #|(rect1) ⇒ Object
Methods inherited from Shape
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
#bottom ⇒ Object (readonly)
Returns the value of attribute bottom.
3 4 5 |
# File 'lib/vector_be_winding/rect.rb', line 3 def bottom @bottom end |
#left ⇒ Object (readonly)
Returns the value of attribute left.
3 4 5 |
# File 'lib/vector_be_winding/rect.rb', line 3 def left @left end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
3 4 5 |
# File 'lib/vector_be_winding/rect.rb', line 3 def right @right end |
#top ⇒ Object (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_rect ⇒ Object
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
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 |