File tree 2 files changed +10
-3
lines changed
2 files changed +10
-3
lines changed Original file line number Diff line number Diff line change
1
+ calculateLength = (x , y , z ) -> Math .sqrt (x * x) + (y * y) + (z * z)
2
+
1
3
class Vector
2
4
constructor : (@x = 0 , @y = 0 , @z = 0 ) ->
3
5
@isNormalized = undefined
@@ -56,11 +58,14 @@ class Vector
56
58
57
59
return @
58
60
59
- length : () ->
60
- return Math .sqrt (@x * @x ) + (@y * @y ) + (@z * @z )
61
+ length : () -> calculateLength @x , @y , @z
61
62
62
63
euclideanDistanceTo : (vector ) ->
63
- return @ subtract (vector).length ()
64
+ return calculateLength (
65
+ @x - vector .x
66
+ @y - vector .y
67
+ @z - vector .z
68
+ )
64
69
65
70
normalize : () ->
66
71
@isNormalized = true
Original file line number Diff line number Diff line change @@ -75,9 +75,11 @@ runTest 'Returns a vector\'s length', (test) ->
75
75
76
76
runTest ' Returns the euclidian distance to a vector' , (test ) ->
77
77
vectorA = new Vector (4 , 2 , 0 )
78
+ vectorAReference = vectorA .clone ()
78
79
vectorB = new Vector (3 , 5 , 7 )
79
80
expectedDistance = 7.681145747868608
80
81
test .same (vectorA .euclideanDistanceTo (vectorB), expectedDistance)
82
+ test .same (vectorA, vectorAReference)
81
83
82
84
83
85
runTest ' Normalizes a vector' , (test ) ->
You can’t perform that action at this time.
0 commit comments