lib/geometry/rectangle.rb in geometry-6 vs lib/geometry/rectangle.rb in geometry-6.1

- old
+ new

@@ -76,10 +76,12 @@ original_new(options[:from], options[:to]) elsif options.has_key?(:height) and options.has_key?(:width) SizedRectangle.new(height: options[:height], width: options[:width]) elsif (2==args.count) and (args.all? {|a| a.is_a?(Array) || a.is_a?(Point) }) original_new(*args) + elsif options.empty? + raise ArgumentError, "#{self} arguments must be named, not: #{args}" else raise ArgumentError, "Bad Rectangle arguments: #{args}, #{options}" end end @@ -169,9 +171,42 @@ def width min, max = @points.minmax {|a,b| a.x <=> b.x} max.x - min.x end # @endgroup + + # Create a new {Rectangle} from the receiver that's inset by the given amount + # @overload inset(x, y) + # @overload inset(top, left, bottom, right) + # @overload inset(x, y) + # @option options [Number] :x Inset from the left and right sides + # @option options [Number] :y Inset from the top and bottom + # @overload inset(top, left, bottom, right) + # @option options [Number] :bottom The inset from the bottom of the {Rectangle} + # @option options [Number] :left The inset from the left side of the {Rectangle} + # @option options [Number] :right The inset from the right side of the {Rectangle} + # @option options [Number] :top The inset from the top of the {Rectangle} + def inset(*args) + options, args = args.partition {|a| a.is_a? Hash} + options = options.reduce({}, :merge) + raise ArumentError, "Can't specify both arguments and options" if !args.empty? && !options.empty? + + if 1 == args.size + distance = args.shift + Rectangle.new from:(min + distance), to:(max - distance) + elsif 2 == args.size + distance = Point[*args] + Rectangle.new from:(min + distance), to:(max - distance) + elsif 4 == args.size + top, left, bottom, right = *args + Rectangle.new from:(min + Point[left, bottom]), to:(max - Point[right, top]) + elsif options[:x] && options[:y] + distance = Point[options[:x], options[:y]] + Rectangle.new from:(min + distance), to:(max - distance) + elsif options[:top] && options[:left] && options[:bottom] && options[:right] + Rectangle.new from:(min + Point[options[:left], options[:bottom]]), to:(max - Point[options[:right], options[:top]]) + end + end end class CenteredRectangle < Rectangle # @return [Point] The {Rectangle}'s center attr_accessor :center