File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -234,12 +234,26 @@ def test_array_zip
234234 assert_equal ( zipped , expected )
235235 end
236236
237- def test_cross # the cross product of 2D vectors is a float
237+ def test_cross # where a, b, c are collinear area == 0
238238 a = Vec2D . new ( 0 , 0 )
239239 b = Vec2D . new ( 100 , 100 )
240240 c = Vec2D . new ( 200 , 200 )
241241 # see http://mathworld.wolfram.com/Collinear.html for details
242- assert ( ( a - b ) . cross ( b - c ) . zero? , 'Failed collinearity test using 2D vector product' )
242+ assert ( ( a - b ) . cross ( b - c ) . zero? , 'Failed collinearity test using 2D vector cross product' )
243+ end
244+
245+ def test_cross_area # NB: the sign might be negative
246+ a = Vec2D . new ( 200 , 0 )
247+ b = Vec2D . new ( 0 , 200 )
248+ # Expectation is area is twice that of the triangle created by the vectors
249+ assert_equal ( ( a ) . cross ( b ) . abs , 40_000.0 , 'Failed area test using 2D vector cross product' )
250+ end
251+
252+ def test_cross_non_zero # Could be used calculate area of triangle
253+ a = Vec2D . new ( 40 , 40 )
254+ b = Vec2D . new ( 40 , 140 )
255+ c = Vec2D . new ( 140 , 40 )
256+ assert_equal ( ( a - b ) . cross ( b - c ) . abs , 10_000.0 , 'Failed area test using 2D vector cross product' )
243257 end
244258
245259 def test_equals
You can’t perform that action at this time.
0 commit comments