人気ブログランキング | 話題のタグを見る

中心を軸に回転 (その2)

ぐるぐる回転しているのを見て閃いた。
Cairo::Matrix.rotateする時に、
ImageSurefaseの(0,0)と重なるポイントが、回転軸になる。
判りづらい行列を書かないでCairo::Matrix.rotateを使える。
変換するのにプロセスが必要と言うことだな。
イメージを移動して軸をきめ回転して移動。
こんな感じだ。

#!usr/bin/env ruby

require 'sdl'
require 'cairo'

class BackGround
def initialize(w, h)
@w, @h = w, h
end

def update(c)
c.set_source_color(Cairo::Color::BLACK)
c.rectangle(0, 0, @w, @h)
c.fill
c.set_source_color(Cairo::Color::GREEN)
c.move_to(0, @h / 2)
c.line_to(@w, @h / 2)
c.stroke
c.move_to(@w / 2, 0)
c.line_to(@w / 2, @h)
c.stroke
end
end

class Panel
attr_writer :matrix
attr_reader :w, :h

def initialize
@w, @h = 100, 100
@image = Cairo::ImageSurface.new(@w, @h)
context = Cairo::Context.new(@image)
context.set_source_color(Cairo::Color::ORANGE)
context.rectangle(0, 0, @w, @h)
context.fill
@matrix = Cairo::Matrix.identity
end

def update(c)
c.matrix = @matrix
c.set_source(@image, 0, 0)
c.paint
end
end


class Phase
def initialize(screen)
@screen = screen
@w, @h = @screen.w, @screen.h
@image = Cairo::ImageSurface.new(@w, @h)
@bg = BackGround.new(@w, @h)
@panel = Panel.new
@deg = 0
end

def update
@deg += 15
@deg = 0 if @deg > 359
rd = @deg * Math::PI / 180
tr_mtrx1 = Cairo::Matrix.translate(-@panel.w / 2, -@panel.h / 2)
ro_mtrx = Cairo::Matrix.rotate(rd)
tr_mtrx2 = Cairo::Matrix.translate(@w / 2, @h / 2)
@panel.matrix = tr_mtrx1 * ro_mtrx * tr_mtrx2
context = Cairo::Context.new(@image)
@bg.update(context)
@panel.update(context)
surface = SDL::Surface.new_from(@image.data, @image.width, @image.height, 32, @image.stride, 0, 0, 0, 0)
@screen.put(surface, 0, 0)
end

def run
loop do
while e=SDL::Event.poll
exit if e.kind_of?(SDL::Event::Quit) || (e.kind_of?(SDL::Event::KeyDown) && e.sym == SDL::Key::ESCAPE)
end
self.update
@screen.flip
SDL.delay(100)
end
end
end

SDL.init(SDL::INIT_VIDEO)
@screen = SDL::Screen.open(300, 300, SDL::Screen.info.bpp, SDL::SWSURFACE)
Phase.new(@screen).run
by gaziya | 2011-05-11 00:00