Class: Laser::Cutter::Geometry::Rect

Inherits:
Line
  • Object
show all
Defined in:
lib/laser-cutter/geometry/shape/rect.rb

Instance Attribute Summary collapse

Attributes inherited from Line

#p1, #p2

Attributes inherited from Shape

#name, #position

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Line

#<, #<=>, #>, #center, #clone, #eql?, #hash, #length, #normalized, #overlaps?, #sorted_coords, #xor

Methods inherited from Shape

#move_to, #x, #x=, #y, #y=

Constructor Details

#initialize(*args) ⇒ Rect

Returns a new instance of Rect.



22
23
24
25
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 22

def initialize(*args)
  super(*args)
  relocate!
end

Instance Attribute Details

#sidesObject

Returns the value of attribute sides.



6
7
8
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 6

def sides
  @sides
end

#verticesObject

Returns the value of attribute vertices.



6
7
8
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 6

def vertices
  @vertices
end

Class Method Details

.[](p1, p2) ⇒ Object



8
9
10
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 8

def self.[] p1, p2
  Rect.new(p1, p2)
end

.create(point, w, h, name = nil) ⇒ Object



12
13
14
15
16
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 12

def self.create(point, w, h, name = nil)
  r = Rect.new(point, Point[point.x + w, point.y + h])
  r.name = name
  r
end

.from_line(line) ⇒ Object



18
19
20
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 18

def self.from_line(line)
  Rect.new(line.p1, line.p2)
end

Instance Method Details

#hObject



45
46
47
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 45

def h
  p2.y - p1.y
end

#relocate!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 27

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_aObject



53
54
55
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 53

def to_a
  [[p1.x, p1.y], [p2.x, p2.y]]
end

#to_sObject



49
50
51
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 49

def to_s
  "#{sprintf "%3d", w}(w)x#{sprintf "%3d", h}(h) @ #{position.to_s}"
end

#wObject



41
42
43
# File 'lib/laser-cutter/geometry/shape/rect.rb', line 41

def w
  p2.x - p1.x
end