Class: Geom3d::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/geom3d/point.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Point

Returns a new instance of Point.



8
9
10
# File 'lib/geom3d/point.rb', line 8

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

Instance Attribute Details

#xObject

Returns the value of attribute x.



6
7
8
# File 'lib/geom3d/point.rb', line 6

def x
  @x
end

#yObject

Returns the value of attribute y.



6
7
8
# File 'lib/geom3d/point.rb', line 6

def y
  @y
end

#zObject

Returns the value of attribute z.



6
7
8
# File 'lib/geom3d/point.rb', line 6

def z
  @z
end

Instance Method Details

#+(other) ⇒ Object



23
24
25
# File 'lib/geom3d/point.rb', line 23

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

#-(other) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/geom3d/point.rb', line 12

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

#==(other) ⇒ Object



27
28
29
# File 'lib/geom3d/point.rb', line 27

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

#flattenObject



40
41
42
# File 'lib/geom3d/point.rb', line 40

def flatten
  to_ary.flatten
end

#to_aryObject

Allows recursive flatten and multiple assignment to work with this class



36
37
38
# File 'lib/geom3d/point.rb', line 36

def to_ary
  [@x, @y, @z]
end

#to_sObject



31
32
33
# File 'lib/geom3d/point.rb', line 31

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