Class: Laser::Cutter::Geometry::Rect
- Defined in:
- lib/laser/cutter/geometry/rect.rb
Overview
An axis-aligned rectangle, given by two opposite corners: p1, the lesser, and p2. It keeps its four vertices and sides up to date.
Instance Attribute Summary collapse
-
#sides ⇒ Array<Line>
The four sides, counter-clockwise from p1.
-
#vertices ⇒ Array<Point>
The four corners, counter-clockwise from p1.
Attributes inherited from Line
Attributes inherited from Shape
Class Method Summary collapse
Instance Method Summary collapse
-
#h ⇒ Float
The height.
-
#initialize ⇒ Rect
constructor
Takes whatever Line#initialize takes.
-
#relocate! ⇒ Rect
Moves the rectangle to its position and rebuilds its vertices and sides.
-
#to_a ⇒ Array<Array<Float>>
Both corners, as [[x1, y1], [x2, y2]].
-
#to_s ⇒ String
The size and the position.
-
#w ⇒ Float
The width.
Methods inherited from Line
#<, #<=>, #>, #center, #clone, #eql?, #hash, #length, #normalized, #overlaps?, #sorted_coords, #xor
Methods inherited from Shape
Constructor Details
#initialize ⇒ Rect
Takes whatever Line#initialize takes.
40 41 42 43 |
# File 'lib/laser/cutter/geometry/rect.rb', line 40 def initialize(*) super relocate! end |
Instance Attribute Details
#sides ⇒ Array<Line>
Returns the four sides, counter-clockwise from p1.
13 14 15 |
# File 'lib/laser/cutter/geometry/rect.rb', line 13 def sides @sides end |
#vertices ⇒ Array<Point>
Returns the four corners, counter-clockwise from p1.
13 |
# File 'lib/laser/cutter/geometry/rect.rb', line 13 attr_accessor :sides, :vertices |
Class Method Details
.[](p1, p2) ⇒ Rect
18 19 20 |
# File 'lib/laser/cutter/geometry/rect.rb', line 18 def self.[](p1, p2) Rect.new(p1, p2) end |
Instance Method Details
#h ⇒ Float
Returns the height.
69 70 71 |
# File 'lib/laser/cutter/geometry/rect.rb', line 69 def h p2.y - p1.y end |
#relocate! ⇒ Rect
Moves the rectangle to its position and rebuilds its vertices and sides.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/laser/cutter/geometry/rect.rb', line 49 def relocate! super self.vertices = [] vertices << p1 vertices << p1.plus(w, 0) vertices << p2 vertices << p1.plus(0, h) self.sides = [] vertices.each_with_index do |v, index| sides << Line.new(v, vertices[(index + 1) % vertices.size]) end self end |
#to_a ⇒ Array<Array<Float>>
Returns both corners, as [[x1, y1], [x2, y2]].
79 80 81 |
# File 'lib/laser/cutter/geometry/rect.rb', line 79 def to_a [[p1.x, p1.y], [p2.x, p2.y]] end |
#to_s ⇒ String
Returns the size and the position.
74 75 76 |
# File 'lib/laser/cutter/geometry/rect.rb', line 74 def to_s "#{sprintf '%3d', w}(w)x#{sprintf '%3d', h}(h) @ #{position}" end |
#w ⇒ Float
Returns the width.
64 65 66 |
# File 'lib/laser/cutter/geometry/rect.rb', line 64 def w p2.x - p1.x end |