Class: GeoWeb::Point

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

Direct Known Subclasses

Coordinate, Location

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x_or_object, y = nil, z = nil) ⇒ Point

If the object respond only to x and y, the second parameter can be z Point.new(x, y) Point.new(obj) - obj.x and obj.y and obj.z Point.new(obj, z) - obj.x and obj.y TODO - Point.new(hash) - hash and hash and hash TODO - Point.new(hash, z) - hash and hash



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

def initialize(x_or_object, y=nil, z=nil)
  x = x_or_object
  if x_or_object.respond_to? :x and x_or_object.respond_to? :y
    x, y = x_or_object.x, x_or_object.y
    z = x_or_object.respond_to?(:z) && x_or_object.z || y
  end
  @x, @y, @z = x, y, z
end

Instance Attribute Details

#xObject

Returns the value of attribute x.



4
5
6
# File 'lib/geoweb/point.rb', line 4

def x
  @x
end

#yObject

Returns the value of attribute y.



4
5
6
# File 'lib/geoweb/point.rb', line 4

def y
  @y
end

#zObject

Returns the value of attribute z.



4
5
6
# File 'lib/geoweb/point.rb', line 4

def z
  @z
end

Class Method Details

.from_array(coord) ⇒ Object



12
13
14
# File 'lib/geoweb/point.rb', line 12

def from_array(coord)
  self.new(coord[0], coord[1], coord[2])
end

.from_hash(coord) ⇒ Object



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

def from_hash(coord)
  self.new(coord[:x], coord[:y], coord[:z])
end

Instance Method Details

#*(other) ⇒ Object

Point multiplication. Accepts other point, a Numeric or an Array



63
64
65
66
67
68
69
70
71
# File 'lib/geoweb/point.rb', line 63

def *(other)
  if other.respond_to? :x and other.respond_to? :y and other.respond_to? :z
    self.class.new(self.x.to_f * other.x.to_f, self.y.to_f * other.y.to_f, self.z.to_f * other.z.to_f)
  elsif other.respond_to? :to_a
    self * self.class.from_array(other.to_a)
  else
    self.class.new(self.x.to_f * other.to_f, self.y.to_f * other.to_f, self.z.to_f * other.to_f)
  end
end

#+(other) ⇒ Object

Spatial sum (sums x, y and z)



44
45
46
# File 'lib/geoweb/point.rb', line 44

def +(other)
  self.class.new(self.x + other.x, self.y + other.y, self.z.to_i + other.z.to_i)
end

#-(other) ⇒ Object



54
55
56
# File 'lib/geoweb/point.rb', line 54

def -(other)
  self.class.new(self.x - other.x, self.y - other.y, self.z.to_i - other.z.to_i)
end

#/(other) ⇒ Object

Point division. Accepts other point, a Numeric or an Array



74
75
76
77
78
79
80
81
82
83
# File 'lib/geoweb/point.rb', line 74

def /(other)
#      self.class.new(self.x / num, self.y / num, self.z)
  if other.respond_to? :x and other.respond_to? :y and other.respond_to? :z
    self.class.new(self.x.to_f / other.x.to_f, self.y.to_f / other.y.to_f, self.z.to_f / other.z.to_f)
  elsif other.respond_to? :to_a
    self / self.class.from_array(other.to_a)
  else
    self.class.new(self.x.to_f / other.to_f, self.y.to_f / other.to_f, self.z.to_f / other.to_f)
  end
end

#==(other) ⇒ Object



58
59
60
# File 'lib/geoweb/point.rb', line 58

def ==(other)
  self.class.x == other.x and self.y == other.y and self.z.to_i == other.z.to_i
end

#roundObject



39
40
41
# File 'lib/geoweb/point.rb', line 39

def round
  self.clone.round!
end

#round!Object



32
33
34
35
36
37
# File 'lib/geoweb/point.rb', line 32

def round!
  @x = @x.round
  @y = @y.round
  @z = @z && @z.round
  self
end

#to_aObject



89
90
91
# File 'lib/geoweb/point.rb', line 89

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

#to_hashObject



93
94
95
# File 'lib/geoweb/point.rb', line 93

def to_hash
  {:x => @x, :y => @y, :z => @z}
end

#to_sObject



85
86
87
# File 'lib/geoweb/point.rb', line 85

def to_s
  "(#{sprintf('%f', @x)}, #{sprintf('%f', @y)}, #{@z})"
end

#|(other) ⇒ Object

Planar sum (sums x and y)



49
50
51
52
# File 'lib/geoweb/point.rb', line 49

def |(other)
#      new_z = self.z unless self.z != other.z
  self.class.new(self.x + other.x, self.y + other.y, self.z)
end