Class: Geometry::Square
- Inherits:
-
Object
- Object
- Geometry::Square
- Includes:
- ClusterFactory
- Defined in:
- lib/geometry/square.rb
Overview
The Square class cluster is like the Rectangle class cluster, but not longer in one direction.
Constructors
Square.new from:[1,2], to:[2,3] # Using two corners
Square.new origin:[3,4], size:5 # Using an origin point and a size
Square.new center:[5,5], size:5 # Using a center point and a size
Square.new size:5 # Centered on the origin
Direct Known Subclasses
Instance Attribute Summary collapse
- #closed? ⇒ Boolean
- #edges ⇒ Object readonly
-
#origin ⇒ Point
The lower left corner.
-
#points ⇒ Object
Returns the value of attribute points.
Class Method Summary collapse
-
.new(: origin, : size) ⇒ CenteredSquare
Creates a Square with the given origin and size.
Instance Method Summary collapse
- #height ⇒ Object
- #initialize(options = {}) ⇒ Square constructor
-
#max ⇒ Point
The upper right corner of the bounding Rectangle.
-
#min ⇒ Point
The lower left corner of the bounding Rectangle.
-
#minmax ⇒ Array<Point>
The lower left and upper right corners of the bounding Rectangle.
-
#vertices ⇒ Object
Returns the value of attribute points.
- #width ⇒ Object
Methods included from ClusterFactory
Constructor Details
#initialize(options = {}) ⇒ Square
Creates a Geometry::Square given two Points
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/geometry/square.rb', line 57 def initialize(={}) origin = [:from] || [:origin] origin = origin ? Point[origin] : PointZero.new if .has_key? :to point1 = [:to] end point1 = Point[point1] raise(ArgumentError, "Point sizes must match (#{origin.size} != #{point1.size})") unless origin.is_a?(PointZero) || (origin.size == point1.size) # Reorder the points to get lower-left and upper-right minx, maxx = [origin.x, point1.x].minmax miny, maxy = [origin.y, point1.y].minmax @points = [Point[minx, miny], Point[maxx, maxy]] raise(NotSquareError) if height != width end |
Instance Attribute Details
#closed? ⇒ Boolean
79 80 81 |
# File 'lib/geometry/square.rb', line 79 def closed? true end |
#edges ⇒ Object (readonly)
85 86 87 |
# File 'lib/geometry/square.rb', line 85 def edges (points + [points.first]).each_cons(2).map {|v1,v2| Edge.new v1, v2} end |
#origin ⇒ Point
Returns The lower left corner.
21 22 23 |
# File 'lib/geometry/square.rb', line 21 def origin @origin end |
#points ⇒ Object
Returns the value of attribute points.
25 26 27 |
# File 'lib/geometry/square.rb', line 25 def points @points end |
Class Method Details
.new(: origin, : size) ⇒ CenteredSquare
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/geometry/square.rb', line 33 def self.new(*args) , args = args.partition {|a| a.is_a? Hash} = .reduce({}, :merge) if .key?(:size) unless [:size].is_a? Numeric raise NotSquareError, 'Size must be a square' unless [:size].all? {|a| a == [:size].first} [:size] = [:size].first end if .key? :origin SizedSquare.new([:origin], [:size]) else CenteredSquare.new([:center] || PointZero.new, [:size]) end elsif .key?(:from) and .key?(:to) original_new(from: [:from], to: [:to]) end end |
Instance Method Details
#height ⇒ Object
115 116 117 118 |
# File 'lib/geometry/square.rb', line 115 def height min, max = @points.minmax {|a,b| a.y <=> b.y} max.y - min.y end |
#max ⇒ Point
Returns The upper right corner of the bounding Rectangle.
90 91 92 |
# File 'lib/geometry/square.rb', line 90 def max @points.last end |
#min ⇒ Point
Returns The lower left corner of the bounding Rectangle.
95 96 97 |
# File 'lib/geometry/square.rb', line 95 def min @points.first end |
#minmax ⇒ Array<Point>
Returns The lower left and upper right corners of the bounding Rectangle.
100 101 102 |
# File 'lib/geometry/square.rb', line 100 def minmax [self.min, self.max] end |
#vertices ⇒ Object
Returns the value of attribute points.
26 27 28 |
# File 'lib/geometry/square.rb', line 26 def points @points end |
#width ⇒ Object
120 121 122 123 |
# File 'lib/geometry/square.rb', line 120 def width min, max = @points.minmax {|a,b| a.x <=> b.x} max.x - min.x end |