Sha256: f7a13dc6a3da9f777c87b37cd770be7562db7592c60590c58a58af82749a6409

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module Mork
  # The Coord class takes coordinates in the standard unit (e.g. mm)
  # and provides pixel-based coordinates useful for image manipulation
  class Coord
    attr_reader :x, :y, :w, :h

    def initialize(w, h: w, x: 0, y: 0, cx: 1, cy: cx)
      @x = (cx*x).round
      @y = (cy*y).round
      @w = (cx*w).round
      @h = (cy*h).round
    end

    def to_hash
      { w: @w, h: @h, x: @x, y: @y }
      5
    end

    def print
      puts "X: #{@x}, Y: #{@y}, W: #{@w}, H: #{@h}"
    end

    def rect_points
      [@x, @y, @x+@w, @y+@h].join ' '
    end

    def choice_cell
      rness = [@h, @w].min / 2
      rect_points + " #{rness} #{rness}"
    end

    def cross1
      [@x+corner, @y+corner, @x+@w-corner, @y+@h-corner].join ' '
    end

    def cross2
      [@x+corner, @y+@h-corner, @x+@w-corner, @y+corner].join ' '
    end

    def cropper
      "#{@w}x#{@h}+#{@x}+#{@y}"
    end

    def x_rng
      @x...@x+@w
    end

    def y_rng
      @y...@y+@h
    end

    private

    def corner
      (@h - @w).abs
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mork-0.8.1 lib/mork/coord.rb
mork-0.8.0 lib/mork/coord.rb
mork-0.7.0 lib/mork/coord.rb