Skip to content

Commit

Permalink
better vector functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed May 5, 2011
1 parent 6356861 commit 147b753
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
15 changes: 14 additions & 1 deletion lib/geo_magic/point.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,15 +57,28 @@ def self.create_from *args
new latitude, longitude new latitude, longitude
end end


def move dlat, dlong def move! dlat, dlong
@latitude += dlat @latitude += dlat
@longitude += dlong @longitude += dlong
self
end

def move dlat, dlong
cloned = self.dup
cloned.latitude += dlat
cloned.longitude += dlong
cloned
end end


def move_vector vector def move_vector vector
raise ArgumentError, "Argument must be a GeoMagic::Vector, was #{vector}" if !vector.kind_of? GeoMagic::Vector
move vector.lat_distance, vector.long_distance move vector.lat_distance, vector.long_distance
end end


def move_vector! vector
raise ArgumentError, "Argument must be a GeoMagic::Vector, was #{vector}" if !vector.kind_of? GeoMagic::Vector
move! vector.lat_distance, vector.long_distance
end


def to_point_hash mode= :long def to_point_hash mode= :long
case mode case mode
Expand Down
12 changes: 10 additions & 2 deletions lib/geo_magic/vector.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def initialize p0, p1
@p1 = p1 @p1 = p1
end end


def create_at center, vector def self.create_at center, vector
new center, center.move(vector) new center, center.move_vector(vector)
end end


def length type = nil def length type = nil
Expand All @@ -28,6 +28,14 @@ def length type = nil
d d
end end


def lat_distance
length(:latitude).in_radians
end

def long_distance
length(:longitude).in_radians
end

def vector_distance def vector_distance
GeoMagic::Distance::Vector.new length(:latitude), length(:longitude), :lat_factor => lat_factor GeoMagic::Distance::Vector.new length(:latitude), length(:longitude), :lat_factor => lat_factor
end end
Expand Down
29 changes: 29 additions & 0 deletions spec/geo_magic/vector_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,13 +16,42 @@
end end
end end


describe 'class methods' do
describe '#create_at' do
it "should create a vector from a point and another vector" do
v2 = GeoMagic::Vector.create_at @a, @vector
v2.should be_a(GeoMagic::Vector)
end
end
end

describe '#distance' do describe '#distance' do
it "is has a distance" do it "is has a distance" do
@vector.distance.should be_a(GeoMagic::Distance) @vector.distance.should be_a(GeoMagic::Distance)
@vector.distance.in_meters.should > 500 @vector.distance.in_meters.should > 500
end end
end end


describe '#lat_factor' do
it "is has a lat_factor of 2" do
@vector.lat_factor.should >= 2
end
end

describe '#length' do
describe ':latitude' do
it "is has a latitude length of ..." do
@vector.length(:latitude).in_km.should >= 0.5
end
end

describe ':longitude' do
it "is has a longitude length of ..." do
@vector.length(:longitude).in_km.should >= 0.5
end
end
end

describe '#vector_distance' do describe '#vector_distance' do
it "is has a vector_distance" do it "is has a vector_distance" do
@vector.vector_distance.should be_a(GeoMagic::Distance::Vector) @vector.vector_distance.should be_a(GeoMagic::Distance::Vector)
Expand Down

0 comments on commit 147b753

Please sign in to comment.