Skip to content

Commit

Permalink
Added arithmetic operator overrides to Point
Browse files Browse the repository at this point in the history
Force them to return Point objects instead of Vectors
  • Loading branch information
bfoz committed May 4, 2012
1 parent 408e047 commit 1ca6672
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/geometry/point.rb
Expand Up @@ -55,5 +55,20 @@ def y
def z
@elements[2]
end

# @group Arithmetic
# Override the arithmetic operators to force them to return {Point}s instead
# of {Vector}s

def +(other)
Point[super]
end

def -(other)
Point[super]
end

# @endgroup

end
end
19 changes: 17 additions & 2 deletions test/geometry/point.rb
@@ -1,9 +1,9 @@
require_relative '../helper'
require_relative '../../lib/geometry/point'

class PointTest < Test::Unit::TestCase
Point = Geometry::Point
Point = Geometry::Point

class PointTest < Test::Unit::TestCase
must "create a Point object using list syntax" do
point = Geometry::Point[2,1]
assert_equal(2, point.size)
Expand Down Expand Up @@ -86,3 +86,18 @@ class PointTest < Test::Unit::TestCase
assert_equal('Point[10, 11]', point.to_s)
end
end

class PointArithmeticTest < Test::Unit::TestCase
def setup
@left = Point[1,2]
@right = Point[3,4]
end

must "return a Point when adding two Points" do
assert_kind_of(Point, @left+@right)
end

must "return a Point when subtracting two Points" do
assert_kind_of(Point, @left-@right)
end
end

0 comments on commit 1ca6672

Please sign in to comment.