Class: Laser::Cutter::Geometry::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/laser/cutter/geometry/transform.rb

Overview

A rotation followed by a move, from a local frame into the page.

Notches are drawn along the x axis of a local frame, where every side is horizontal and the axis-aligned notching code applies unchanged. A Transform then turns and moves the result onto a side at any angle.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, angle) ⇒ Transform

Returns a new instance of Transform.

Parameters:

  • origin (Point)
  • angle (Numeric) —

    radians, counter-clockwise



30
31
32
33
34
35
36
# File 'lib/laser/cutter/geometry/transform.rb', line 30

def initialize(origin, angle)
  @origin = origin.clone.freeze
  @angle = angle.to_f
  @cos = Math.cos(@angle)
  @sin = Math.sin(@angle)
  freeze
end

Instance Attribute Details

#angle ⇒ Float (readonly)

Returns the turn, in radians, counter-clockwise.

Returns:

  • (Float) —

    the turn, in radians, counter-clockwise



16
17
18
# File 'lib/laser/cutter/geometry/transform.rb', line 16

def angle
  @angle
end

#origin ⇒ Point (readonly)

Returns where the local origin lands.

Returns:

  • (Point) —

    where the local origin lands



13
14
15
# File 'lib/laser/cutter/geometry/transform.rb', line 13

def origin
  @origin
end

Class Method Details

.along(from, to) ⇒ Transform

The frame whose x axis runs from one point towards another, with its origin on the first. Its y axis points to the left of that run.

Parameters:

Returns:



24
25
26
# File 'lib/laser/cutter/geometry/transform.rb', line 24

def self.along(from, to)
  new(from, Math.atan2(to.y - from.y, to.x - from.x))
end

Instance Method Details

#apply(shape) ⇒ Point, Line

Returns a new shape, on the page.

Parameters:

Returns:



40
41
42
43
44
45
46
# File 'lib/laser/cutter/geometry/transform.rb', line 40

def apply(shape)
  case shape
  when Line then Line.new(apply(shape.p1), apply(shape.p2))
  when Point then Point[origin.x + (shape.x * @cos) - (shape.y * @sin), origin.y + (shape.x * @sin) + (shape.y * @cos)]
  else raise ArgumentError, "cannot transform #{shape.class}"
  end
end