Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 140 lines (117 sloc) 4.662 kB
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
1 use v6;
2
3 class Vector
4 {
5 has @.coordinates;
6
7 multi method new (*@x)
8 {
9 self.bless(*, coordinates => @x);
10 }
11
12 multi method new (@x)
13 {
14 self.bless(*, coordinates => @x);
15 }
16
17 our Str multi method Str()
18 {
19 "(" ~ @.coordinates.join(', ') ~ ")";
20 }
21
22 our Str multi method perl()
23 {
24 self.WHAT.perl ~ ".new(" ~ @.coordinates.map({.perl}).join(', ') ~ ")";
25 }
26
27 method Dim()
28 {
29 @.coordinates.elems;
30 }
31
32 method Length()
33 {
34 sqrt [+] (@.coordinates »*« @.coordinates);
35 }
36
37 method Unitize()
38 {
39 my $length = self.Length;
40 if $length > 1e-10
41 {
42 return Vector.new(@.coordinates >>/>> $length);
43 }
44 else
45 {
46 return Vector.new(@.coordinates);
47 }
48 }
49 }
50
51 # SHOULD: change this back to normal + when Rakudo supports that
52 multi sub infix:<V+>(Vector $a, Vector $b where { $a.Dim == $b.Dim })
53 {
54 Vector.new($a.coordinates »+« $b.coordinates);
55 }
56
57 # SHOULD: change this back to normal - when Rakudo supports that
58 multi sub infix:<V->(Vector $a, Vector $b where { $a.Dim == $b.Dim })
59 {
60 Vector.new($a.coordinates »-« $b.coordinates);
61 }
62
63 # SHOULD: change this back to normal - when Rakudo supports that
64 multi sub prefix:<V->(Vector $a)
65 {
66 Vector.new(0 <<-<< $a.coordinates);
67 }
68
69 # SHOULD: make this scalar * Vector operator - when Rakudo supports
70 multi sub infix:<V*>(Vector $a, $b)
71 {
72 Vector.new($a.coordinates >>*>> $b);
73 }
74
75 multi sub infix:<V*>($a, Vector $b)
76 {
77 Vector.new($a <<*<< $b.coordinates);
78 }
79
80 # SHOULD: Vector / Scalar operator - when Rakudo supports
81 multi sub infix:<V/>(Vector $a, $b)
82 {
83 Vector.new($a.coordinates >>/>> $b);
84 }
85
86 multi sub infix:<⋅>(Vector $a, Vector $b where { $a.Dim == $b.Dim }) # is tighter(&infix:<+>) (NYI)
87 {
88 [+]($a.coordinates »*« $b.coordinates);
89 }
90
91 multi sub infix:<dot>(Vector $a, Vector $b)
92 {
93 $a$b;
94 }
95
96 multi sub infix:<×>(Vector $a where { $a.Dim == 3 }, Vector $b where { $b.Dim == 3 })
97 {
98 Vector.new($a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1],
99 $a.coordinates[2] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[2],
100 $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0]);
101 }
102
103 multi sub infix:<×>(Vector $a where { $a.Dim == 7 }, Vector $b where { $b.Dim == 7 })
104 {
105 Vector.new($a.coordinates[1] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[1]
106 + $a.coordinates[2] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[2]
107 + $a.coordinates[4] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[4],
108 $a.coordinates[2] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[2]
109 + $a.coordinates[3] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[3]
110 + $a.coordinates[5] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[5],
111 $a.coordinates[3] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[3]
112 + $a.coordinates[4] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[4]
113 + $a.coordinates[6] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[6],
114 $a.coordinates[4] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[4]
115 + $a.coordinates[5] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[5]
116 + $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0],
117 $a.coordinates[5] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[5]
118 + $a.coordinates[6] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[6]
119 + $a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1],
120 $a.coordinates[6] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[6]
121 + $a.coordinates[0] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[0]
122 + $a.coordinates[2] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[2],
123 $a.coordinates[0] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[0]
124 + $a.coordinates[1] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[1]
125 + $a.coordinates[3] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[3]);
126 }
127
128 multi sub infix:<cross>(Vector $a, Vector $b)
129 {
130 $a × $b;
131 }
132
133 multi sub circumfix:<⎡ ⎤>(Vector $a)
134 {
135 $a.Length;
136 }
137
138 subset UnitVector of Vector where { (1 - 1e-10) < $^v.Length < (1 + 1e-10) };
139
Something went wrong with that request. Please try again.