Class: Svgcode::SVG::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/svgcode/svg/transform.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str_or_a, b = nil, c = nil, d = nil, e = nil, f = nil) ⇒ Transform

Returns a new instance of Transform.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/svgcode/svg/transform.rb', line 9

def initialize(str_or_a, b = nil, c = nil, d = nil, e = nil, f = nil)
  if str_or_a.is_a?(String)
    raise ArgumentException.new unless str_or_a.start_with?('matrix')
    nums = str_or_a.gsub(/.+\(/, '').gsub(/\)/, '').split(/\s*,\s*/)

    nums.each_with_index do |n, i|
      case i
      when 0
        @a = n.to_f
      when 1
        @b = n.to_f
      when 2
        @c = n.to_f
      when 3
        @d = n.to_f
      when 4
        @e = n.to_f
      when 5
        @f = n.to_f
      end
    end
  else
    @a = str_or_a.to_f
    @b = b.to_f
    @c = c.to_f
    @d = d.to_f
    @e = e.to_f
    @f = f.to_f
  end
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



7
8
9
# File 'lib/svgcode/svg/transform.rb', line 7

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



7
8
9
# File 'lib/svgcode/svg/transform.rb', line 7

def b
  @b
end

#cObject (readonly)

Returns the value of attribute c.



7
8
9
# File 'lib/svgcode/svg/transform.rb', line 7

def c
  @c
end

#dObject (readonly)

Returns the value of attribute d.



7
8
9
# File 'lib/svgcode/svg/transform.rb', line 7

def d
  @d
end

#eObject (readonly)

Returns the value of attribute e.



7
8
9
# File 'lib/svgcode/svg/transform.rb', line 7

def e
  @e
end

#fObject (readonly)

Returns the value of attribute f.



7
8
9
# File 'lib/svgcode/svg/transform.rb', line 7

def f
  @f
end

Instance Method Details

#apply(point) ⇒ Object



44
45
46
47
48
49
# File 'lib/svgcode/svg/transform.rb', line 44

def apply(point)
  point_m = Matrix[[point.x], [point.y], [1]]
  transform_m = to_matrix
  result = transform_m * point_m
  Point.new(result[0, 0], result[1, 0])
end

#to_matrixObject



40
41
42
# File 'lib/svgcode/svg/transform.rb', line 40

def to_matrix
  Matrix[[@a, @c, @e], [@b, @d, @f], [0, 0, 1]]
end