Class: PerfectShape::Rectangle
- Includes:
- RectangularShape
- Defined in:
- lib/perfect_shape/rectangle.rb
Direct Known Subclasses
Constant Summary collapse
- OUT_LEFT =
bitmask indicating a point lies to the left
1- OUT_TOP =
bitmask indicating a point lies above
2- OUT_RIGHT =
bitmask indicating a point lies to the right
4- OUT_BOTTOM =
bitmask indicating a point lies below
8- RECT_INTERSECTS =
0x80000000
Instance Attribute Summary
Attributes included from RectangularShape
Attributes included from PointLocation
Instance Method Summary collapse
-
#contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0) ⇒ Boolean
Checks if rectangle contains point (two-number Array or x, y args).
- #edges ⇒ Object
-
#empty? ⇒ Boolean
A rectangle is empty if its width or height is 0 (or less).
- #intersect?(rectangle) ⇒ Boolean
-
#out_state(x_or_point, y = nil) ⇒ Object
Returns out state for specified point (x,y): (left, right, top, bottom).
Methods included from RectangularShape
Methods included from PointLocation
Methods inherited from Shape
#==, #bounding_box, #center_point, #center_x, #center_y, #height, #max_x, #max_y, #min_x, #min_y, #width
Instance Method Details
#contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0) ⇒ Boolean
Checks if rectangle contains point (two-number Array or x, y args)
the rectangle, false if the point lies outside of the rectangle’s bounds.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/perfect_shape/rectangle.rb', line 54 def contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0) x, y = Point.normalize_point(x_or_point, y) return unless x && y if outline edges.any? { |edge| edge.contain?(x, y, distance_tolerance: distance_tolerance) } else x.between?(self.x, self.x + width) && y.between?(self.y, self.y + height) end end |
#edges ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/perfect_shape/rectangle.rb', line 65 def edges [ Line.new(points: [[self.x, self.y], [self.x + width, self.y]]), Line.new(points: [[self.x + width, self.y], [self.x + width, self.y + height]]), Line.new(points: [[self.x + width, self.y + height], [self.x, self.y + height]]), Line.new(points: [[self.x, self.y + height], [self.x, self.y]]) ] end |
#empty? ⇒ Boolean
A rectangle is empty if its width or height is 0 (or less)
102 103 104 |
# File 'lib/perfect_shape/rectangle.rb', line 102 def empty? width <= 0.0 || height <= 0.0 end |
#intersect?(rectangle) ⇒ Boolean
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/perfect_shape/rectangle.rb', line 106 def intersect?(rectangle) x = rectangle.x y = rectangle.y w = rectangle.width h = rectangle.height return false if empty? || w <= 0 || h <= 0 x0 = self.x y0 = self.y (x + w) > x0 && (y + h) > y0 && x < (x0 + self.width) && y < (y0 + self.height) end |
#out_state(x_or_point, y = nil) ⇒ Object
Returns out state for specified point (x,y): (left, right, top, bottom)
It can be 0 meaning not outside the rectangle, or if outside the rectangle, then a bit mask combination of OUT_LEFT, OUT_RIGHT, OUT_TOP, or OUT_BOTTOM
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/perfect_shape/rectangle.rb', line 79 def out_state(x_or_point, y = nil) x, y = Point.normalize_point(x_or_point, y) return unless x && y out = 0 if self.width <= 0 out |= OUT_LEFT | OUT_RIGHT elsif x < self.x out |= OUT_LEFT elsif x > self.x + self.width out |= OUT_RIGHT end if self.height <= 0 out |= OUT_TOP | OUT_BOTTOM elsif y < self.y out |= OUT_TOP elsif y > self.y + self.height out |= OUT_BOTTOM end out end |