Sha256: 107b8df0e42c2b0e29aabf44e161b4ec8645ea7e46e934f148797691d23bef8b

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

module Svgcode
  module SVG
    class Point
      attr_reader :x, :y

      VALUE_SEP = /\s?,\s?/
      OBJECT_SEP = /\s+/

      def initialize(str_or_x, y = nil)
        if y.nil?
          parts = str_or_x.split(VALUE_SEP)
          @x = parts.first.to_f
          @y = parts.last.to_f
        else
          @x = str_or_x.to_f
          @y = y.to_f
        end
      end

      def negate_y
        Point.new(@x, -@y)
      end

      def negate_y!
        @y = -@y
      end

      def +(point_or_num)
        if point_or_num.is_a?(Point)
          Point.new(@x + point_or_num.x, @y + point_or_num.y)
        else
          Point.new(@x + point_or_num, @y + point_or_num)
        end
      end

      def -(point_or_num)
        if point_or_num.is_a?(Point)
          Point.new(@x - point_or_num.x, @y - point_or_num.y)
        else
          Point.new(@x - point_or_num, @y - point_or_num)
        end
      end

      def *(point_or_num)
        if point_or_num.is_a?(Point)
          Point.new(@x / point_or_num.x, @y / point_or_num.y)
        else
          Point.new(@x / point_or_num, @y / point_or_num)
        end
      end

      def /(point_or_num)
        if point_or_num.is_a?(Point)
          Point.new(@x / point_or_num.x, @y / point_or_num.y)
        else
          Point.new(@x / point_or_num, @y / point_or_num)
        end
      end

      def divide_by!(amount)
        @x /= amount
        @y /= amount
      end

      def flip_y!(max_y)
        @y = max_y - @y
      end

      def ==(other)
        other.is_a?(self.class) && other.x.eql?(@x) && other.y.eql?(@y)
      end

      def to_s
        "#{@x},#{@y}"
      end

      def self.parse(str)
        str.split(OBJECT_SEP).collect { |p| Point.new(p.strip) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
svgcode-0.3.0 lib/svgcode/svg/point.rb
svgcode-0.2.0 lib/svgcode/svg/point.rb