Class: Point

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

Constant Summary collapse

@@box_dimensions =
Mddb::Config.simulation['dimensions']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_mongo(value) ⇒ Object



41
42
43
# File 'lib/mddb/point.rb', line 41

def self.from_mongo(value)
  value.nil? ? nil : Point.new(value)
end

.to_mongo(value) ⇒ Object



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

def self.to_mongo(value)
  value = value.respond_to?(:lines) ? value.lines : value
  value.to_a
end

Instance Method Details

#+(vector) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/mddb/point.rb', line 22

def +(vector)
  if vector.is_a? Vector
    result = Point.new
    self.each_index {|i| result.push self[i] + vector[i]}
    return result
  else
    raise "expected a vector, not a #{vector.class}"
  end
end

#distance_to(point) ⇒ Object



32
33
34
# File 'lib/mddb/point.rb', line 32

def distance_to(point)
  self.vector_to(point).length
end

#to_3pObject



54
55
56
57
58
59
60
61
# File 'lib/mddb/point.rb', line 54

def to_3p
  string = ""
  self.each_with_index do |x,i|
    string = string + x.round(6).to_s.rjust(5,' ')
    string = string + ' ' if i < self.size-1
  end
  return string
end

#to_sObject



45
46
47
48
49
50
51
52
# File 'lib/mddb/point.rb', line 45

def to_s
  string = ""
  self.each_with_index do |x,i|
    string = string + x.round(2).to_s.rjust(5,' ')
    string = string + ', ' if i < self.size-1
  end
  return string
end

#vector_to(point) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mddb/point.rb', line 3

def vector_to(point)
  if point.is_a? Point  
    result = Vector.new
    self.each_index {|i| 
      r = point[i] - self[i]
      if r > @@box_dimensions[i]/2
        result.push r - @@box_dimensions[i]
      elsif r < -@@box_dimensions[i]/2
        result.push -(-@@box_dimensions[i] - r)
      else
        result.push r
      end
    }
    return result
  else
    raise "expected a point, not a #{point.class}"
  end
end