Class: Snowy::Matrix

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mat = nil) ⇒ Matrix

Returns a new instance of Matrix.



430
431
432
433
434
435
436
437
438
# 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

Instance Attribute Details

#matrixObject (readonly)

Returns the value of attribute matrix.



419
420
421
# File 'lib/snowy.rb', line 419

def matrix
  @matrix
end

Class Method Details

.[](matrix) ⇒ Object



421
422
423
424
425
426
427
428
# File 'lib/snowy.rb', line 421

def self.[](matrix)
  case matrix
  when self
    matrix
  else
    new matrix
  end
end

Instance Method Details

#initialize_copy(mat) ⇒ Object



440
441
442
443
# 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) ⇒ Object



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# 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) ⇒ Object



479
480
481
# File 'lib/snowy.rb', line 479

def mult(mat)
  mult! self.class[mat].matrix
end

#mult!(mat) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
# 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

#resetObject



445
446
447
# File 'lib/snowy.rb', line 445

def reset
  load [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
end

#rotate(rad) ⇒ Object



523
524
525
526
527
# 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) ⇒ Object



519
520
521
# File 'lib/snowy.rb', line 519

def scale(ax, ay, aw = 1)
  mult!([[ax, 0, 0], [0, ay, 0], [0, 0, aw]])
end

#transform2(x, y, w = 1) ⇒ Object Also known as: transform



497
498
499
500
501
502
# 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

#transform3(x, y, w = 1) ⇒ Object



506
507
508
509
510
511
512
513
# 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) ⇒ Object



515
516
517
# File 'lib/snowy.rb', line 515

def translate(dx, dy, dw = 1)
  mult!([[1, 0, dx], [0, 1, dy], [0, 0, dw]])
end