class Snowy::Matrix

Attributes

matrix[R]

Public Class Methods

[](matrix) click to toggle source
# File lib/snowy.rb, line 421
def self.[](matrix)
  case matrix
  when self
    matrix
  else
    new matrix
  end
end
new(mat = nil) click to toggle source
# File lib/snowy.rb, line 430
def initialize(mat = nil)
  @matrix = [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]

  if mat
    load mat
  else
    reset
  end
end

Public Instance Methods

initialize_copy(mat) click to toggle source
# File lib/snowy.rb, line 440
def initialize_copy(mat)
  @matrix = [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
  load mat
end
load(mat) click to toggle source
# File lib/snowy.rb, line 449
def load(mat)
  case mat
  when Matrix
    matrix[0][0, 3] = mat.matrix[0]
    matrix[1][0, 3] = mat.matrix[1]
    matrix[2][0, 3] = mat.matrix[2]
  when Array
    if mat.size == 3 &&
        mat[0].kind_of?(Array) && mat[0].size == 3 &&
        mat[1].kind_of?(Array) && mat[1].size == 3 &&
        mat[2].kind_of?(Array) && mat[2].size == 3
      matrix[0][0, 3] = mat[0]
      matrix[1][0, 3] = mat[1]
      matrix[2][0, 3] = mat[2]
    else
      if mat.size == 9
        matrix[0][0, 3] = mat[0, 3]
        matrix[1][0, 3] = mat[3, 3]
        matrix[2][0, 3] = mat[6, 3]
      else
        raise ArgumentError, "wrong element number (given #{mat.size} elements, expect 9 elements)"
      end
    end
  else
    raise ArgumentError, "wrong argument type (expect Snowy::Matrix or Array)"
  end

  self
end
mult(mat) click to toggle source
# File lib/snowy.rb, line 479
def mult(mat)
  mult! self.class[mat].matrix
end
mult!(mat) click to toggle source
# File lib/snowy.rb, line 483
def mult!(mat)
  3.times do |i|
    m0 = matrix[i]
    mm = m0.dup
    3.times do |j|
      m0[j] = mm[0] * mat[0][j] +
              mm[1] * mat[1][j] +
              mm[2] * mat[2][j]
    end
  end

  self
end
reset() click to toggle source
# File lib/snowy.rb, line 445
def reset
  load [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
end
rotate(rad) click to toggle source
# File lib/snowy.rb, line 523
def rotate(rad)
  cos = Math.cos(rad)
  sin = Math.sin(rad)
  mult!([[cos, -sin, 0], [sin, cos, 0], [0, 0, 1]])
end
scale(ax, ay, aw = 1) click to toggle source
# File lib/snowy.rb, line 519
def scale(ax, ay, aw = 1)
  mult!([[ax, 0, 0], [0, ay, 0], [0, 0, aw]])
end
transform(x, y, w = 1)
Alias for: transform2
transform2(x, y, w = 1) click to toggle source
# File lib/snowy.rb, line 497
def transform2(x, y, w = 1)
  mx = matrix[0]
  my = matrix[1]
  [x * mx[0] + y * mx[1] + w * mx[2],
   x * my[0] + y * my[1] + w * my[2]]
end
Also aliased as: transform
transform3(x, y, w = 1) click to toggle source
# File lib/snowy.rb, line 506
def transform3(x, y, w = 1)
  mx = matrix[0]
  my = matrix[1]
  mw = matrix[2]
  [x * mx[0] + y * mx[1] + w * mx[2],
   x * my[0] + y * my[1] + w * my[2],
   x * mw[0] + y * mw[1] + w * mw[2]]
end
translate(dx, dy, dw = 1) click to toggle source
# File lib/snowy.rb, line 515
def translate(dx, dy, dw = 1)
  mult!([[1, 0, dx], [0, 1, dy], [0, 0, dw]])
end