Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…ent and test xy2mn.
  • Loading branch information...
commit e86be860197c9aa193800f68a6e523c005a9e179 1 parent eb84dba
SF authored

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

  1. +10 11 lib/SVGPad.pm
  2. +11 0 t/07-pad.t
21 lib/SVGPad.pm
@@ -2,24 +2,23 @@ use v6;
2 2
3 3 use Vector;
4 4
  5 +subset Vector2 of Vector where { $^v.Dim == 2 };
  6 +
5 7 class SVGPad
6 8 {
7   - has Vector $.xy_min;
8   - has Vector $.xy_max;
9   - has Vector $.mn_min;
10   - has Vector $.mn_max;
  9 + has Vector2 $.xy_min;
  10 + has Vector2 $.xy_max;
  11 + has Vector2 $.mn_min;
  12 + has Vector2 $.mn_max;
11 13
12   - multi method new(Vector $xy_min where { $xy_min.Dim == 2 },
13   - Vector $xy_max where { $xy_max.Dim == 2 },
14   - Vector $mn_min where { $mn_min.Dim == 2 },
15   - Vector $mn_max where { $mn_max.Dim == 2 })
  14 + multi method new(Vector2 $xy_min, Vector2 $xy_max, Vector2 $mn_min, Vector2 $mn_max)
16 15 {
17 16 self.bless(*, xy_min => $xy_min, xy_max => $xy_max, mn_min => $mn_min, mn_max => $mn_max);
18 17 }
19 18
20   - multi method xy2mn(Vector $xy where { $xy.Dim == 2 })
  19 + multi method xy2mn(Vector2 $xy)
21 20 {
22   -
23   -
  21 + my $t = ($xy - $.xy_min).coordinates >>/<< ($.xy_max - $.xy_min).coordinates;
  22 + return $.mn_min + Vector.new(($.mn_max - $.mn_min).coordinates >>*<< $t);
24 23 }
25 24 }
11 t/07-pad.t
@@ -8,10 +8,21 @@ plan *;
8 8
9 9 my $pad = SVGPad.new(Vector.new(-1, 0), Vector.new(1, 3), Vector.new(0, 0), Vector.new(200, 300));
10 10 isa_ok($pad, SVGPad, "Variable is of type SVGPad");
  11 +
11 12 is_approx($pad.xy_min, Vector.new(-1, 0), "xy_min is correct");
12 13 is_approx($pad.xy_max, Vector.new(1, 3), "xy_max is correct");
13 14 is_approx($pad.mn_min, Vector.new(0, 0), "mn_min is correct");
14 15 is_approx($pad.mn_max, Vector.new(200, 300), "mn_max is correct");
15 16
  17 +is_approx($pad.xy2mn(Vector.new(-1, 0)), Vector.new(0, 0), "(-1, 0) goes to (0, 0)");
  18 +is_approx($pad.xy2mn(Vector.new(0, 0)), Vector.new(100, 0), "(0, 0) is (100, 0)");
  19 +is_approx($pad.xy2mn(Vector.new(1, 0)), Vector.new(200, 0), "(1, 0) is (200, 0)");
  20 +is_approx($pad.xy2mn(Vector.new(-1, 1)), Vector.new(0, 100), "(-1, 1) goes to (0, 100)");
  21 +is_approx($pad.xy2mn(Vector.new(0, 1)), Vector.new(100, 100), "(0, 1) is (100, 100)");
  22 +is_approx($pad.xy2mn(Vector.new(1, 1)), Vector.new(200, 100), "(1, 1) is (200, 100)");
  23 +is_approx($pad.xy2mn(Vector.new(-1, 3)), Vector.new(0, 300), "(-1, 3) goes to (0, 300)");
  24 +is_approx($pad.xy2mn(Vector.new(0, 3)), Vector.new(100, 300), "(0, 3) is (100, 300)");
  25 +is_approx($pad.xy2mn(Vector.new(1, 3)), Vector.new(200, 300), "(1, 3) is (200, 300)");
  26 +
16 27
17 28 done_testing;

0 comments on commit e86be86

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