Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add Vector.abs and use it to get rid of the is_approx_vector test (si…

…nce is_approx will work just as well now).
  • Loading branch information...
commit f09c4bff517ae83dc8d7acc865b121bf27de9360 1 parent 7b32a06
SF authored

Showing 2 changed files with 13 additions and 13 deletions. Show diff stats Hide diff stats

  1. +5 0 lib/Vector.pm
  2. +8 13 t/01-basics.t
5 lib/Vector.pm
@@ -47,6 +47,11 @@ class Vector is also
47 47 sqrt(self ⋅ self);
48 48 }
49 49
  50 + multi method abs()
  51 + {
  52 + self.Length;
  53 + }
  54 +
50 55 method Unitize()
51 56 {
52 57 my $length = self.Length;
21 t/01-basics.t
@@ -2,11 +2,6 @@ use v6;
2 2 use Vector;
3 3 use Test;
4 4
5   -sub is_approx_vector(Vector $a, Vector $b, $desc)
6   -{
7   - ok(($a - $b).Length < 0.00001, $desc);
8   -}
9   -
10 5 plan *;
11 6
12 7 my $v1 = Vector.new(1, 2, 3);
@@ -85,16 +80,16 @@ for @vectors -> $v
85 80 {
86 81 my Vector $vn = $v * 4.5;
87 82 is_approx($vn.Length, $v.Length * 4.5, "Scalar by Vector multiply gets proper length");
88   - is_approx_vector($vn.Unitize, $v.Unitize, "Scalar by Vector multiply gets proper direction");
89   - is_approx_vector($vn, 4.5 * $v, "Scalar by Vector multiply is commutative");
  83 + is_approx($vn.Unitize, $v.Unitize, "Scalar by Vector multiply gets proper direction");
  84 + is_approx($vn, 4.5 * $v, "Scalar by Vector multiply is commutative");
90 85 }
91 86
92 87 for @vectors -> $v
93 88 {
94 89 my Vector $vn = $v / 4.5;
95 90 is_approx($vn.Length, $v.Length / 4.5, "Vector by Scalar divide gets proper length");
96   - is_approx_vector($vn.Unitize, $v.Unitize, "Vector by Scalar divide gets proper direction");
97   - is_approx_vector($vn, $v * (1.0 / 4.5), "Vector by Scalar divide is equal to multiplication by reciprocal");
  91 + is_approx($vn.Unitize, $v.Unitize, "Vector by Scalar divide gets proper direction");
  92 + is_approx($vn, $v * (1.0 / 4.5), "Vector by Scalar divide is equal to multiplication by reciprocal");
98 93 }
99 94
100 95 #dot product tests
@@ -132,7 +127,7 @@ for ($v1, $v2, $v3) X ($v1, $v2, $v3) -> $x, $y
132 127 my $cross = $x × $y;
133 128 is_approx($cross ⋅ $x, 0, "(x × y) ⋅ x = 0");
134 129 is_approx($cross ⋅ $y, 0, "(x × y) ⋅ y = 0");
135   - is_approx_vector($cross, -($y × $x), "x × y = -y × x");
  130 + is_approx($cross, -($y × $x), "x × y = -y × x");
136 131 is_approx($cross.Length ** 2, $x.Length ** 2 * $y.Length ** 2 - ($x ⋅ $y) ** 2,
137 132 "|x × y|^2 = |x|^2 * |y|^2 - (x ⋅ y)^2");
138 133 }
@@ -142,7 +137,7 @@ for ($v7, $v8, $v9, $v10) X ($v7, $v8, $v9, $v10) -> $x, $y
142 137 my $cross = $x × $y;
143 138 is_approx($cross ⋅ $x, 0, "(x × y) ⋅ x = 0");
144 139 is_approx($cross ⋅ $y, 0, "(x × y) ⋅ y = 0");
145   - is_approx_vector($cross, -($y × $x), "x × y = -y × x");
  140 + is_approx($cross, -($y × $x), "x × y = -y × x");
146 141 is_approx($cross.Length ** 2, $x.Length ** 2 * $y.Length ** 2 - ($x ⋅ $y) ** 2,
147 142 "|x × y|^2 = |x|^2 * |y|^2 - (x ⋅ y)^2");
148 143 }
@@ -156,7 +151,7 @@ dies_ok( { $v5 cross $v6 }, "You can't do 5D cross products");
156 151 {
157 152 my $a = $v1;
158 153 $a ×= $v2;
159   - is_approx_vector($v1 × $v2, $a, "×= works");
  154 + is_approx($v1 × $v2, $a, "×= works");
160 155 }
161 156
162 157 # UnitVector tests
@@ -169,7 +164,7 @@ dies_ok( { $v5 cross $v6 }, "You can't do 5D cross products");
169 164 my UnitVector $a = UnitVector.new(1, 0, 0);
170 165 my $b = $a;
171 166 $b += $v2;
172   - is_approx_vector($a + $v2, $b, "+= works on UnitVector");
  167 + is_approx($a + $v2, $b, "+= works on UnitVector");
173 168 }
174 169 {
175 170 my UnitVector $a = Vector.new(1, 0, 0);

0 comments on commit f09c4bf

Please sign in to comment.
Something went wrong with that request. Please try again.