Sha256: d38b586b722de54fd5d38bbfc34f26acab8e29fcdbabb5cc20e9234f6509108e

Contents?: true

Size: 852 Bytes

Versions: 2

Compression:

Stored size: 852 Bytes

Contents

require 'geom3d/eps'
require 'geom3d/vector'

module Geom3d
  class Point
    attr_accessor :x, :y, :z

    def initialize *args
      @x, @y, @z = args.flatten
    end

    def - other
      case other
      when Point
        Vector.new(@x - other.x, @y - other.y, @z - other.z)
      when Vector
        Point.new(@x - other.dx, @y - other.dy, @z - other.dz)
      else
        throw ArgumentError
      end
    end

    def + other
      Point.new(@x + other.dx, @y + other.dy, @z + other.dz)
    end

    def == other
      (@x - other.x) < EPS && (@y - other.y) < EPS && (@z - other.z) < EPS
    end

    def to_s
      "Point3(%.3f,%.3f,%.3f)" % [@x, @y, @z]
    end

    # Allows recursive flatten and multiple assignment to work with this class
    def to_ary
      [@x, @y, @z]
    end

    def flatten
      to_ary.flatten
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
geom3d-0.1.1 lib/geom3d/point.rb
geom3d-0.1.0 lib/geom3d/point.rb