Class: Postsvg::Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/postsvg/matrix.rb

Overview

Matrix transformation class for PostScript coordinate transformations Implements affine transformation matrix [a b c d e f]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a: 1, b: 0, c: 0, d: 1, e: 0, f: 0) ⇒ Matrix



9
10
11
12
13
14
15
16
# File 'lib/postsvg/matrix.rb', line 9

def initialize(a: 1, b: 0, c: 0, d: 1, e: 0, f: 0)
  @a = a
  @b = b
  @c = c
  @d = d
  @e = e
  @f = f
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



7
8
9
# File 'lib/postsvg/matrix.rb', line 7

def a
  @a
end

#bObject

Returns the value of attribute b.



7
8
9
# File 'lib/postsvg/matrix.rb', line 7

def b
  @b
end

#cObject

Returns the value of attribute c.



7
8
9
# File 'lib/postsvg/matrix.rb', line 7

def c
  @c
end

#dObject

Returns the value of attribute d.



7
8
9
# File 'lib/postsvg/matrix.rb', line 7

def d
  @d
end

#eObject

Returns the value of attribute e.



7
8
9
# File 'lib/postsvg/matrix.rb', line 7

def e
  @e
end

#fObject

Returns the value of attribute f.



7
8
9
# File 'lib/postsvg/matrix.rb', line 7

def f
  @f
end

Instance Method Details

#apply_point(x, y) ⇒ Object



61
62
63
64
65
66
# File 'lib/postsvg/matrix.rb', line 61

def apply_point(x, y)
  {
    x: (x * @a) + (y * @c) + @e,
    y: (x * @b) + (y * @d) + @f,
  }
end

#decomposeObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/postsvg/matrix.rb', line 68

def decompose
  scale_x = Math.hypot(@a, @b)
  scale_y = ((@a * @d) - (@b * @c)) / scale_x

  rotation = Math.atan2(@b, @a) * (180.0 / Math::PI)

  skew_x = Math.atan2((@a * @c) + (@b * @d), scale_x * scale_x)
  skew_y = Math.atan2((@a * @b) + (@c * @d), scale_y * scale_y)

  {
    translate: { x: @e, y: @f },
    scale: { x: scale_x, y: scale_y },
    rotate: rotation,
    skew: {
      x: skew_x * (180.0 / Math::PI),
      y: skew_y * (180.0 / Math::PI),
    },
  }
end

#identity?Boolean



102
103
104
# File 'lib/postsvg/matrix.rb', line 102

def identity?
  @a == 1 && @b.zero? && @c.zero? && @d == 1 && @e.zero? && @f.zero?
end

#invertObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/postsvg/matrix.rb', line 88

def invert
  det = (@a * @d) - (@b * @c)
  return Matrix.new if det.abs < 1e-10

  inv = Matrix.new
  inv.a = @d / det
  inv.b = -@b / det
  inv.c = -@c / det
  inv.d = @a / det
  inv.e = ((@c * @f) - (@d * @e)) / det
  inv.f = ((@b * @e) - (@a * @f)) / det
  inv
end

#multiply(matrix) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/postsvg/matrix.rb', line 18

def multiply(matrix)
  result = Matrix.new
  result.a = (@a * matrix.a) + (@c * matrix.b)
  result.b = (@b * matrix.a) + (@d * matrix.b)
  result.c = (@a * matrix.c) + (@c * matrix.d)
  result.d = (@b * matrix.c) + (@d * matrix.d)
  result.e = (@a * matrix.e) + (@c * matrix.f) + @e
  result.f = (@b * matrix.e) + (@d * matrix.f) + @f
  result
end

#rotate(degrees) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/postsvg/matrix.rb', line 37

def rotate(degrees)
  radians = degrees * Math::PI / 180.0
  m = Matrix.new
  m.a = Math.cos(radians)
  m.b = Math.sin(radians)
  m.c = -Math.sin(radians)
  m.d = Math.cos(radians)
  multiply(m)
end

#scale(sx, sy) ⇒ Object



33
34
35
# File 'lib/postsvg/matrix.rb', line 33

def scale(sx, sy)
  multiply(Matrix.new(a: sx, d: sy))
end

#skew_x(angle) ⇒ Object



47
48
49
50
# File 'lib/postsvg/matrix.rb', line 47

def skew_x(angle)
  radians = angle * Math::PI / 180.0
  multiply(Matrix.new(c: Math.tan(radians)))
end

#skew_y(angle) ⇒ Object



52
53
54
55
# File 'lib/postsvg/matrix.rb', line 52

def skew_y(angle)
  radians = angle * Math::PI / 180.0
  multiply(Matrix.new(b: Math.tan(radians)))
end

#to_transform_stringObject



57
58
59
# File 'lib/postsvg/matrix.rb', line 57

def to_transform_string
  "matrix(#{@a} #{@b} #{@c} #{@d} #{@e} #{@f})"
end

#translate(tx, ty) ⇒ Object



29
30
31
# File 'lib/postsvg/matrix.rb', line 29

def translate(tx, ty)
  multiply(Matrix.new(e: tx, f: ty))
end