Permalink
Browse files

Switch SVGPad from Vector to Vector2 (Vector where .Dim == 2). Implem…

…ent and test xy2mn.
  • Loading branch information...
1 parent eb84dba commit e86be860197c9aa193800f68a6e523c005a9e179 SF committed Nov 2, 2009
Showing with 21 additions and 11 deletions.
  1. +10 −11 lib/SVGPad.pm
  2. +11 −0 t/07-pad.t
View
21 lib/SVGPad.pm
@@ -2,24 +2,23 @@ use v6;
use Vector;
+subset Vector2 of Vector where { $^v.Dim == 2 };
+
class SVGPad
{
- has Vector $.xy_min;
- has Vector $.xy_max;
- has Vector $.mn_min;
- has Vector $.mn_max;
+ has Vector2 $.xy_min;
+ has Vector2 $.xy_max;
+ has Vector2 $.mn_min;
+ has Vector2 $.mn_max;
- multi method new(Vector $xy_min where { $xy_min.Dim == 2 },
- Vector $xy_max where { $xy_max.Dim == 2 },
- Vector $mn_min where { $mn_min.Dim == 2 },
- Vector $mn_max where { $mn_max.Dim == 2 })
+ multi method new(Vector2 $xy_min, Vector2 $xy_max, Vector2 $mn_min, Vector2 $mn_max)
{
self.bless(*, xy_min => $xy_min, xy_max => $xy_max, mn_min => $mn_min, mn_max => $mn_max);
}
- multi method xy2mn(Vector $xy where { $xy.Dim == 2 })
+ multi method xy2mn(Vector2 $xy)
{
-
-
+ my $t = ($xy - $.xy_min).coordinates >>/<< ($.xy_max - $.xy_min).coordinates;
+ return $.mn_min + Vector.new(($.mn_max - $.mn_min).coordinates >>*<< $t);
}
}
View
11 t/07-pad.t
@@ -8,10 +8,21 @@ plan *;
my $pad = SVGPad.new(Vector.new(-1, 0), Vector.new(1, 3), Vector.new(0, 0), Vector.new(200, 300));
isa_ok($pad, SVGPad, "Variable is of type SVGPad");
+
is_approx($pad.xy_min, Vector.new(-1, 0), "xy_min is correct");
is_approx($pad.xy_max, Vector.new(1, 3), "xy_max is correct");
is_approx($pad.mn_min, Vector.new(0, 0), "mn_min is correct");
is_approx($pad.mn_max, Vector.new(200, 300), "mn_max is correct");
+is_approx($pad.xy2mn(Vector.new(-1, 0)), Vector.new(0, 0), "(-1, 0) goes to (0, 0)");
+is_approx($pad.xy2mn(Vector.new(0, 0)), Vector.new(100, 0), "(0, 0) is (100, 0)");
+is_approx($pad.xy2mn(Vector.new(1, 0)), Vector.new(200, 0), "(1, 0) is (200, 0)");
+is_approx($pad.xy2mn(Vector.new(-1, 1)), Vector.new(0, 100), "(-1, 1) goes to (0, 100)");
+is_approx($pad.xy2mn(Vector.new(0, 1)), Vector.new(100, 100), "(0, 1) is (100, 100)");
+is_approx($pad.xy2mn(Vector.new(1, 1)), Vector.new(200, 100), "(1, 1) is (200, 100)");
+is_approx($pad.xy2mn(Vector.new(-1, 3)), Vector.new(0, 300), "(-1, 3) goes to (0, 300)");
+is_approx($pad.xy2mn(Vector.new(0, 3)), Vector.new(100, 300), "(0, 3) is (100, 300)");
+is_approx($pad.xy2mn(Vector.new(1, 3)), Vector.new(200, 300), "(1, 3) is (200, 300)");
+
done_testing;

0 comments on commit e86be86

Please sign in to comment.