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

Inherits:
Line
  • Object
show all
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

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 ⇒ 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.

Returns:

  • (Array<Line>) —

    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.

Returns:

  • (Array<Point>) —

    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

Parameters:

  • p1 (Point) —

    one corner

  • p2 (Point) —

    the opposite corner

Returns:



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

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

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

Parameters:

  • point (Point) —

    the corner with the least x and y

  • w (Float) —

    the width

  • h (Float) —

    the height

  • name (String, Symbol, nil) (defaults to: nil)

Returns:



27
28
29
30
31
# File 'lib/laser/cutter/geometry/rect.rb', line 27

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) ⇒ Rect

Parameters:

  • line (Line) —

    its ends become opposite corners

Returns:



35
36
37
# File 'lib/laser/cutter/geometry/rect.rb', line 35

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

Instance Method Details

#h ⇒ Float

Returns the height.

Returns:

  • (Float) —

    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.

Returns:



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]].

Returns:

  • (Array<Array<Float>>) —

    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.

Returns:

  • (String) —

    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.

Returns:

  • (Float) —

    the width



64
65
66
# File 'lib/laser/cutter/geometry/rect.rb', line 64

def w
  p2.x - p1.x
end