#! /usr/local/bin/ruby -w
#
#   A RMagick version of Magick++/demo/gravity.cpp
#

require 'RMagick'

# Create the appropriate affine matrix to
# rotate the text by the specified angle

def degrees_to_radians(d)
    d * Math::PI / 180
end

def rotate_by(angle)
    affine  = Magick::AffineMatrix.new(1, 0, 0, 1, 0, 0)
    identity = Magick::AffineMatrix.new(1, 0, 0, 1, 0, 0)

    affine.sx = Math.cos degrees_to_radians(angle)
    affine.rx = Math.sin degrees_to_radians(angle)
    affine.ry = -affine.rx
    affine.sy = affine.sx

    draw_affine = Magick::AffineMatrix.new(1, 0, 0, 1, 0, 0)
    draw_affine.sx = identity.sx * affine.sx + identity.ry * affine.rx
    draw_affine.rx = identity.rx * affine.sx + identity.sy * affine.rx
    draw_affine.ry = identity.sx * affine.ry + identity.ry * affine.sy
    draw_affine.sy = identity.rx * affine.ry + identity.sy * affine.sy
    draw_affine.tx = identity.sx * affine.tx + identity.ry * affine.ty + identity.tx
    draw_affine.ty = 0

    draw_affine
end

x, y = 100, 100

begin

    pic = Magick::ImageList.new

    lines = Magick::Draw.new
    lines.stroke "#600"
    lines.fill_opacity 0
    lines.line 300,100, 300,500
    lines.line 100,300, 500,300
    lines.rectangle 100,100, 500,500

    draw = Magick::Draw.new
    draw.pointsize = 30
    draw.font = "Helvetica"
    draw.fill = "#600"
    draw.undercolor = "red"

    0.step(330, 30) { |angle|
        puts "angle #{angle}"
        pic.new_image(600, 600) { self.background_color = "white" }

        lines.draw pic

        draw.annotate(pic, 0,0,x,y, "NorthWest") {
            self.gravity = Magick::NorthWestGravity
            self.affine = rotate_by(angle)
        }
        draw.annotate(pic, 0,0,0,y, "North") {
            self.gravity = Magick::NorthGravity
            self.affine = rotate_by(angle)
        }
        draw.annotate(pic, 0,0,x,y, "NorthEast") {
            self.gravity = Magick::NorthEastGravity
            self.affine = rotate_by(angle)
        }
        draw.annotate(pic, 0,0,x,0, "East") {
            self.gravity = Magick::EastGravity
            self.affine = rotate_by(angle)
        }
        draw.annotate(pic, 0,0,0,0, "Center") {
            self.gravity = Magick::CenterGravity
            self.affine = rotate_by(angle)
        }
        draw.annotate(pic, 0,0,x,y, "SouthEast") {
            self.gravity = Magick::SouthEastGravity
            self.affine = rotate_by(angle)
        }
        draw.annotate(pic, 0,0,0,y, "South") {
            self.gravity = Magick::SouthGravity
            self.affine = rotate_by(angle)
        }
        draw.annotate(pic, 0,0,x,y, "SouthWest") {
            self.gravity =  Magick::SouthWestGravity
            self.affine = rotate_by(angle)
        }
        draw.annotate(pic, 0,0,x,0, "West") {
            self.gravity = Magick::WestGravity
            self.affine = rotate_by(angle)
        }
    }

    puts "Writing image \"rb_gravity_out.miff\"..."
    pic.delay = 20
    pic.write "./rm_gravity_out.miff"

rescue
    puts "#{$!} exception raised."
    exit 1
end

exit 0