Class: Skia::Matrix

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scale_x: 1.0, skew_x: 0.0, trans_x: 0.0, skew_y: 0.0, scale_y: 1.0, trans_y: 0.0, persp0: 0.0, persp1: 0.0, persp2: 1.0) ⇒ Matrix

Returns a new instance of Matrix.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/skia/matrix.rb', line 7

def initialize(scale_x: 1.0, skew_x: 0.0, trans_x: 0.0,
               skew_y: 0.0, scale_y: 1.0, trans_y: 0.0,
               persp0: 0.0, persp1: 0.0, persp2: 1.0)
  @scale_x = scale_x.to_f
  @skew_x = skew_x.to_f
  @trans_x = trans_x.to_f
  @skew_y = skew_y.to_f
  @scale_y = scale_y.to_f
  @trans_y = trans_y.to_f
  @persp0 = persp0.to_f
  @persp1 = persp1.to_f
  @persp2 = persp2.to_f
end

Instance Attribute Details

#persp0Object

Returns the value of attribute persp0.



5
6
7
# File 'lib/skia/matrix.rb', line 5

def persp0
  @persp0
end

#persp1Object

Returns the value of attribute persp1.



5
6
7
# File 'lib/skia/matrix.rb', line 5

def persp1
  @persp1
end

#persp2Object

Returns the value of attribute persp2.



5
6
7
# File 'lib/skia/matrix.rb', line 5

def persp2
  @persp2
end

#scale_xObject

Returns the value of attribute scale_x.



5
6
7
# File 'lib/skia/matrix.rb', line 5

def scale_x
  @scale_x
end

#scale_yObject

Returns the value of attribute scale_y.



5
6
7
# File 'lib/skia/matrix.rb', line 5

def scale_y
  @scale_y
end

#skew_xObject

Returns the value of attribute skew_x.



5
6
7
# File 'lib/skia/matrix.rb', line 5

def skew_x
  @skew_x
end

#skew_yObject

Returns the value of attribute skew_y.



5
6
7
# File 'lib/skia/matrix.rb', line 5

def skew_y
  @skew_y
end

#trans_xObject

Returns the value of attribute trans_x.



5
6
7
# File 'lib/skia/matrix.rb', line 5

def trans_x
  @trans_x
end

#trans_yObject

Returns the value of attribute trans_y.



5
6
7
# File 'lib/skia/matrix.rb', line 5

def trans_y
  @trans_y
end

Class Method Details

.from_struct(struct) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/skia/matrix.rb', line 56

def self.from_struct(struct)
  new(
    scale_x: struct[:scaleX],
    skew_x: struct[:skewX],
    trans_x: struct[:transX],
    skew_y: struct[:skewY],
    scale_y: struct[:scaleY],
    trans_y: struct[:transY],
    persp0: struct[:persp0],
    persp1: struct[:persp1],
    persp2: struct[:persp2]
  )
end

.from_struct44(struct) ⇒ Object

Create Matrix from 4x4 struct



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/skia/matrix.rb', line 107

def self.from_struct44(struct)
  new(
    scale_x: struct[:m00],
    skew_x: struct[:m01],
    trans_x: struct[:m03],
    skew_y: struct[:m10],
    scale_y: struct[:m11],
    trans_y: struct[:m13],
    persp0: struct[:m30],
    persp1: struct[:m31],
    persp2: struct[:m33]
  )
end

.identityObject



21
22
23
# File 'lib/skia/matrix.rb', line 21

def self.identity
  new
end

.rotate(degrees, px = 0.0, py = 0.0) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/skia/matrix.rb', line 33

def self.rotate(degrees, px = 0.0, py = 0.0)
  radians = degrees * Math::PI / 180.0
  cos = Math.cos(radians)
  sin = Math.sin(radians)

  if px.zero? && py.zero?
    new(scale_x: cos, skew_x: -sin, skew_y: sin, scale_y: cos)
  else
    new(
      scale_x: cos,
      skew_x: -sin,
      trans_x: px - px * cos + py * sin,
      skew_y: sin,
      scale_y: cos,
      trans_y: py - px * sin - py * cos
    )
  end
end

.scale(sx, sy = sx) ⇒ Object



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

def self.scale(sx, sy = sx)
  new(scale_x: sx.to_f, scale_y: sy.to_f)
end

.skew(sx, sy) ⇒ Object



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

def self.skew(sx, sy)
  new(skew_x: sx.to_f, skew_y: sy.to_f)
end

.translate(dx, dy) ⇒ Object



25
26
27
# File 'lib/skia/matrix.rb', line 25

def self.translate(dx, dy)
  new(trans_x: dx.to_f, trans_y: dy.to_f)
end

Instance Method Details

#*(other) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/skia/matrix.rb', line 121

def *(other)
  self.class.new(
    scale_x: @scale_x * other.scale_x + @skew_x * other.skew_y,
    skew_x: @scale_x * other.skew_x + @skew_x * other.scale_y,
    trans_x: @scale_x * other.trans_x + @skew_x * other.trans_y + @trans_x,
    skew_y: @skew_y * other.scale_x + @scale_y * other.skew_y,
    scale_y: @skew_y * other.skew_x + @scale_y * other.scale_y,
    trans_y: @skew_y * other.trans_x + @scale_y * other.trans_y + @trans_y,
    persp0: @persp0 * other.scale_x + @persp1 * other.skew_y + @persp2 * other.persp0,
    persp1: @persp0 * other.skew_x + @persp1 * other.scale_y + @persp2 * other.persp1,
    persp2: @persp0 * other.trans_x + @persp1 * other.trans_y + @persp2 * other.persp2
  )
end

#==(other) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/skia/matrix.rb', line 147

def ==(other)
  return false unless other.is_a?(Matrix)

  @scale_x == other.scale_x && @skew_x == other.skew_x && @trans_x == other.trans_x &&
    @skew_y == other.skew_y && @scale_y == other.scale_y && @trans_y == other.trans_y &&
    @persp0 == other.persp0 && @persp1 == other.persp1 && @persp2 == other.persp2
end

#identity?Boolean

Returns:

  • (Boolean)


141
142
143
144
145
# File 'lib/skia/matrix.rb', line 141

def identity?
  @scale_x == 1.0 && @skew_x == 0.0 && @trans_x == 0.0 &&
    @skew_y == 0.0 && @scale_y == 1.0 && @trans_y == 0.0 &&
    @persp0 == 0.0 && @persp1 == 0.0 && @persp2 == 1.0
end

#to_aObject



155
156
157
158
159
160
161
# File 'lib/skia/matrix.rb', line 155

def to_a
  [
    [@scale_x, @skew_x, @trans_x],
    [@skew_y, @scale_y, @trans_y],
    [@persp0, @persp1, @persp2]
  ]
end

#to_structObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/skia/matrix.rb', line 70

def to_struct
  struct = Native::SKMatrix.new
  struct[:scaleX] = @scale_x
  struct[:skewX] = @skew_x
  struct[:transX] = @trans_x
  struct[:skewY] = @skew_y
  struct[:scaleY] = @scale_y
  struct[:transY] = @trans_y
  struct[:persp0] = @persp0
  struct[:persp1] = @persp1
  struct[:persp2] = @persp2
  struct
end

#to_struct44Object

Convert 3x3 matrix to 4x4 matrix for canvas operations



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/skia/matrix.rb', line 85

def to_struct44
  struct = Native::SKMatrix44.new
  struct[:m00] = @scale_x
  struct[:m01] = @skew_x
  struct[:m02] = 0.0
  struct[:m03] = @trans_x
  struct[:m10] = @skew_y
  struct[:m11] = @scale_y
  struct[:m12] = 0.0
  struct[:m13] = @trans_y
  struct[:m20] = 0.0
  struct[:m21] = 0.0
  struct[:m22] = 1.0
  struct[:m23] = 0.0
  struct[:m30] = @persp0
  struct[:m31] = @persp1
  struct[:m32] = 0.0
  struct[:m33] = @persp2
  struct
end

#transform_point(point) ⇒ Object



135
136
137
138
139
# File 'lib/skia/matrix.rb', line 135

def transform_point(point)
  x = point.x * @scale_x + point.y * @skew_x + @trans_x
  y = point.x * @skew_y + point.y * @scale_y + @trans_y
  Point.new(x, y)
end