Skip to content

Commit 7e092bf

Browse files
docs: Vector README ( Fixes #8 )
1 parent cc0573e commit 7e092bf

File tree

1 file changed

+186
-1
lines changed

1 file changed

+186
-1
lines changed

README.md

Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,187 @@
11
# Vector
2-
Vectors in PowerShell
2+
3+
Numbers are great!
4+
5+
When we measure things with one number, it's technically called a scalar.
6+
7+
When we measure things with more than one number, it's called a [vector](https://en.wikipedia.org/wiki/Vector_%28mathematics_and_physics%29)
8+
9+
We can do lots of things with vectors. We can add or substract them, multiply and divide them.
10+
11+
Vectors are very useful.
12+
13+
This module helps you use Vectors in PowerShell
14+
15+
## Vectors in PowerShell
16+
17+
Vectors are actually built into PowerShell.
18+
19+
Because PowerShell is built atop of the .NET Framework,
20+
and the .NET Framework has had vector support for over a decade,
21+
PowerShell has had vectors for over a decade.
22+
23+
~~~PowerShell
24+
# Create a 2D vector
25+
[Numerics.Vector2]::new(1,2)
26+
# Create a 3D vector
27+
[Numerics.Vector3]::new(1,2,3)
28+
# Create a 4D vector
29+
[Numerics.Vector4]::new(1,2,3,4)
30+
~~~
31+
32+
This module exists to make vectors a bit more useful by providing commands to construct them.
33+
34+
### Installing and Importing
35+
36+
We can install the Vector module from the gallery:
37+
38+
~~~PowerShell
39+
# Install the module from the PowerShell gallery
40+
Install-Module Vector
41+
~~~
42+
43+
Once installed, we can import the Vector module with Import-Module:
44+
45+
~~~PowerShell
46+
Import-Module Vector
47+
~~~
48+
49+
### Getting Vectors
50+
51+
There are a few commands in this module:
52+
53+
* `Get-Vector2`
54+
* `Get-Vector3`
55+
* `Get-Vector4`
56+
57+
Each command constructs a vector of the corresponding size.
58+
59+
We can also drop the `Get` and just refer to them by vector number
60+
61+
~~~PowerShell
62+
Vector2 1 2
63+
Vector3 1 2 3
64+
Vector4 1 2 3 4
65+
~~~
66+
67+
We can be even shorter, and use `V2`, `V3`, and `V4`
68+
69+
~~~PowerShell
70+
v2 1 2
71+
v3 1 2 3
72+
v4 1 2 3 4
73+
~~~
74+
75+
We can turn anything into a series of vectors.
76+
77+
~~~PowerShell
78+
v2 1
79+
v3 1
80+
v4 1
81+
~~~
82+
83+
Strings can become vectors, too! (after all, each byte is already a number)
84+
85+
~~~PowerShell
86+
v2 "hi"
87+
v3 "hi"
88+
v4 "hi"
89+
~~~
90+
91+
### Vector Operators
92+
93+
.NET vectors are _very_ powerful, and overload many operators.
94+
95+
For example, we can add, subtract, multiply, or divide by a scalar.
96+
97+
~~~PowerShell
98+
# Let's start with addition.
99+
# We can add a scalar to a vector.
100+
(v2 1 2) + 1
101+
(v3 1 2 3) + 1
102+
(v4 1 2 3 4) + 1
103+
104+
# Let's try substraction:
105+
(v2 1 2) - 1
106+
(v3 1 2 3) - 1
107+
(v4 1 2 3 4) - 1
108+
109+
# How about multiplication?
110+
(v2 1 2) * 2
111+
(v3 1 2 3) * 2
112+
(v4 1 2 3 4) * 2
113+
114+
# What about division?
115+
(v2 1 2) / 2
116+
(v3 1 2 3) / 2
117+
(v4 1 2 3 4) / 2
118+
~~~
119+
120+
We can also work with other vectors:
121+
122+
~~~PowerShell
123+
# Adding vectors:
124+
(v2 1 2) + (v2 1 2)
125+
(v3 1 2 3) + (v3 1 2 3)
126+
(v4 1 2 3 4) + (v4 1 2 3 4)
127+
128+
# Subtracting vectors:
129+
(v2 1 2) - (v2 1 2)
130+
(v3 1 2 3) - (v3 1 2 3)
131+
(v4 1 2 3 4) - (v4 1 2 3 4)
132+
133+
# Multiplying vectors:
134+
(v2 1 2) * (v2 1 2)
135+
(v3 1 2 3) * (v3 1 2 3)
136+
(v4 1 2 3 4) * (v4 1 2 3 4)
137+
138+
# Dividing vectors:
139+
(v2 1 2) / (v2 1 2)
140+
(v3 1 2 3) / (v3 1 2 3)
141+
(v4 1 2 3 4) / (v4 1 2 3 4)
142+
~~~
143+
144+
### Vector Methods
145+
146+
Vectors have a large number of methods to work with.
147+
148+
Let's start simple, by calculating the length of a given vector.
149+
150+
~~~PowerShell
151+
(v2 1 1).Length()
152+
(v3 1 1 1).Length()
153+
(v4 1 1 1 1).Length()
154+
~~~
155+
156+
Many of the most useful things we can do with a vector are exposed as a static methods:
157+
158+
~~~PowerShell
159+
(v2 1 1) | Get-Member -Static
160+
(v3 1 1 1) | Get-Member -Static
161+
(v4 1 1 1 1) | Get-Member -Static
162+
~~~
163+
164+
165+
We can access static method with `::`
166+
167+
For a small example, let's find the distance between vectors:
168+
169+
~~~PowerShell
170+
$vector1 = v2 1 2
171+
$vector2 = v2 2 1
172+
$vector1::Distance($vector1, $vector2)
173+
~~~
174+
175+
For another simple example, let's find a few point between two points, using [Linear Interpolation `lerp`](https://learn.microsoft.com/en-us/dotnet/api/system.numerics.vector2.lerp?wt.mc_id=MVP_321542)
176+
177+
~~~PowerShell
178+
$vector1 = v2 1 5
179+
$vector2 = v2 1 -5
180+
$vector1::Lerp($vector1, $vector2, 0.25)
181+
$vector1::Lerp($vector1, $vector2, 0.5)
182+
$vector1::Lerp($vector1, $vector2, 0.75)
183+
~~~
184+
185+
All of this would not be possible without the great work of the .NET team to build such incredibly useful data structures.
186+
187+
Hopefully this module helps us all work with vectors!

0 commit comments

Comments
 (0)