class PerfectShape::Rectangle
Constants
- OUT_BOTTOM
bitmask indicating a point lies below
- OUT_LEFT
bitmask indicating a point lies to the left
- OUT_RIGHT
bitmask indicating a point lies to the right
- OUT_TOP
bitmask indicating a point lies above
- RECT_INTERSECTS
Public Instance Methods
contain?(x_or_point, y = nil, outline: false, distance_tolerance: 0)
click to toggle source
Checks if rectangle contains point (two-number Array or x, y args)
@param x The X coordinate of the point to test. @param y The Y coordinate of the point to test.
@return true if the point lies within the bound of the rectangle, false if the point lies outside of the rectangle’s bounds.
# 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()
click to toggle source
# 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?()
click to toggle source
A rectangle is empty if its width or height is 0 (or less)
# File lib/perfect_shape/rectangle.rb, line 102 def empty? width <= 0.0 || height <= 0.0 end
intersect?(rectangle)
click to toggle source
# 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)
click to toggle source
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
# 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