class CooCoo::Transformers::Image::Rotation

Public Class Methods

new(enum, width, height, ox, oy, radians) click to toggle source
Calls superclass method CooCoo::Transformers::Image::Base::new
# File lib/coo-coo/transformer.rb, line 142
def initialize(enum, width, height, ox, oy, radians)
  super(enum, width, height)
  @ox = ox
  @oy = oy
  @radians = radians
end

Public Instance Methods

next() click to toggle source
Calls superclass method CooCoo::Transformers::Proxy#next
# File lib/coo-coo/transformer.rb, line 149
def next
  i = super()
  r = NMatrix.zeroes([1, width * height])
  height.times do |y|
    width.times do |x|
      r[0, map_pixel(x, y)] = sample(i, x, y)
    end
  end
  r
end

Private Instance Methods

map_pixel(x, y) click to toggle source
# File lib/coo-coo/transformer.rb, line 175
def map_pixel(x, y)
  x + y * width
end
rotate_pixel(x, y) click to toggle source
# File lib/coo-coo/transformer.rb, line 179
def rotate_pixel(x, y)
  c = Math.cos(@radians)
  s = Math.sin(@radians)
  x = (x - @ox)
  x = x * c - y * s
  y = y - @oy
  y = x * s + y * c
  [ x, y ]
end
sample(image, x, y) click to toggle source
# File lib/coo-coo/transformer.rb, line 162
def sample(image, x, y)
  rx, ry = *rotate_pixel(x, y)
  rx_min = rx.floor % width
  rx_max = rx.ceil % width
  ry_min = ry.floor % height
  ry_max = ry.ceil % height
  
  (image[0, map_pixel(rx_min, ry_min)] +
   image[0, map_pixel(rx_max, ry_min)] +
   image[0, map_pixel(rx_min, ry_max)] +
   image[0, map_pixel(rx_max, ry_max)]) / 4.0
end