Class: Triangular::Point

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

Direct Known Subclasses

Vector

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, z) ⇒ Point

Returns a new instance of Point.



7
8
9
10
11
# File 'lib/triangular/point.rb', line 7

def initialize(x, y, z)
  @x = x
  @y = y
  @z = z
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/triangular/point.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/triangular/point.rb', line 5

def y
  @y
end

#zObject

Returns the value of attribute z.



5
6
7
# File 'lib/triangular/point.rb', line 5

def z
  @z
end

Class Method Details

.parse(string) ⇒ Object



29
30
31
32
33
# File 'lib/triangular/point.rb', line 29

def self.parse(string)
  match_data = string.strip.match(pattern)

  new(match_data[:x].to_f, match_data[:y].to_f, match_data[:z].to_f)
end

.patternObject



35
36
37
# File 'lib/triangular/point.rb', line 35

def self.pattern
  /(?<x>-?\d+(.\d+(e-?\d+)?)?)\s(?<y>-?\d+(.\d+(e-?\d+)?)?)\s(?<z>-?\d+(.\d+(e-?\d+)?)?)/
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
26
27
# File 'lib/triangular/point.rb', line 23

def ==(other)
  return false unless other.is_a?(Point)

  x == other.x && y == other.y && z == other.z
end

#to_sObject



13
14
15
# File 'lib/triangular/point.rb', line 13

def to_s
  "#{@x.to_f} #{@y.to_f} #{@z.to_f}"
end

#translate!(x, y, z) ⇒ Object



17
18
19
20
21
# File 'lib/triangular/point.rb', line 17

def translate!(x, y, z)
  @x += x
  @y += y
  @z += z
end