1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
[//]: # (generated using SlashBack 0.2.0)
# Using Vectors
1. [Initialization](#Initialization)
* [with no arguments](#initialization-with-no-arguments)
* [with single number](#Initialization-with-a-single-number)
* [all components with numbers](#Initializing-all-components-with-numbers)
* [copying a vector](#Copying-a-vector)
* [with larger vectors](#Initializing-vectors-with-larger-vectors)
* [with vectors and numbers](#Constructing-vectors-from-other-vectors-and-numbers)
* [Lists \(and other iterables\)](#lists-and-other-iterables)
* [Buffer protocol \(numpy, bytes\)](#objects-that-support-the-buffer-protocol-numpy-bytes)
2. [Members](#Members)
3. [Methods](#Methods)
* [The copy protocol](#the-copy-protocol)
* [Pickling](#pickling)
* [To list / tuple](#to-list--tuple)
* [To and from bytes](#to-and-from-bytes)
4. [Operators](#Operators)
* [add](#add--operator)
* [sub](#sub--operator)
* [mul](#mul--operator)
* [matmul](#matmul--operator)
* [div](#div--operator)
* [mod](#mod--operator)
* [floordiv](#floordiv--operator)
* [divmod](#divmod)
* [lshift](#lshift--operator)
* [rshift](#rshift--operator)
* [and](#and--operator)
* [or](#or--operator)
* [xor](#xor--operator)
* [pow](#pow--operator)
* [len](#len)
* [getitem and setitem](#getitem-and-setitem--operator)
* [contains](#contains-in-operator)
* [richcompare](#richcompare-eg--operator)
* [iter](#iter)
* [hash](#hash)
## Initialization
There are dozens of ways of constructing a vector\.
For simplicity, if the same initialization process applies to all vector types, it will only be shown for ``` glm.vec2 ```\.
#### Initialization with no arguments
Initializing a vector without any additional arguments will set all of it's components to zero \(of the respective type\)\.
i\.e\. ``` glm.vec2() ``` returns vector ``` (0.0, 0.0) ```\.
A boolean vector would also be initialized with zero \(or ``` False ``` if you will\)\.
#### Initialization with a single number
Initializing a vector with a number will set all of it's components to the given number \(which may be converted if necessary\)\.
i\.e\. ``` glm.vec2(2.43) ``` returns vector ``` (2.43, 2.43) ```\.
#### Initializing all components with numbers
A vector ``` vecN ``` can be initialized with *N* numbers, which will be copied \(or may be converted\) to their components\.
i\.e\. ``` glm.vec2(1, 2) ``` returns vector ``` (1.0, 2.0) ```
``` glm.vec3(4, 5, 6) ``` returns vector ``` (4.0, 5.0, 6.0) ```
``` glm.ivec4(9, 8, 7, 6) ``` returns vector ``` (9, 8, 7, 6) ```
#### Copying a vector
A copy of a vector can be obtained by initializing a vector with an instance of a vector\.
i\.e\. ``` glm.vec2(glm.vec2(3, 2)) ``` returns vector ``` (3.0, 2.0) ```
This is what's known as the copy constructor\.
#### Initializing vectors with larger vectors
You can initialize any vector with a larger vector \(which will discard any values that don't fit into the new vector\)\.
i\.e\. ``` glm.vec1(glm.vec3(1, 2, 3)) ``` returns vector ``` (1.0) ```
likewise ``` glm.vec2(glm.vec4(5, 6, 7, 8)) ``` returns vector ``` (5.0, 6.0) ```
#### Constructing vectors from other vectors and numbers
As long as you don't use any ``` vec1 ```s in your equation, you can construct any vector from a combination of vectors and / or numbers if their sum equals the length of the target vector\.
i\.e\. ``` glm.vec4(glm.vec2(1, 2), 3, 4) ``` returns vector ``` (1.0, 2.0, 3.0, 4.0) ```
likewise ``` glm.vec3(5, glm.vec2(4, 3)) ``` returns vector ``` (5.0, 4.0, 3.0) ```
but ``` glm.vec2(glm.vec1(1), 2) ``` doesn't work\.
``` glm.vec3(glm.vec2(1, 2), glm.vec2(3, 4)) ``` also doesn't work\.
### Lists \(and other iterables\)
Instead of using vectors to initialize vectors, you can also use lists and other iterables\.
e\.g\. ``` glm.vec2([1, 2]) ``` returns vector ``` (1.0, 2.0) ```
or ``` glm.vec3((3, 4), 5) ``` returns vector ``` (3.0, 4.0, 5.0) ```
### Objects that support the buffer protocol \(numpy, bytes\)
A few objects in Python support a functionality called the buffer protocol\.
One such example would be the Python ``` bytes ``` type or ``` numpy.array ```\.
PyGLM also supports this protocol and thus can be converted to or from any other object that supports it, granted it's in a fitting format\.
e\.g\. ``` bytes(glm.u8vec2(1,2)) ``` returns ``` b'\x01\x02' ```
and ``` glm.u8vec2(b'\x01\x02') ``` returns an 8\-bit unsigned integer vector ``` (1, 2) ```
or ``` glm.vec3(numpy.array([4,5,6])) ``` returns vector ``` (4.0, 5.0, 6.0) ```
and ``` numpy.array(glm.vec3(4, 5, 6)) ``` returns ``` array([4., 5., 6.], dtype=float32) ```
*Note: objects that use the buffer protocol *may* request a reference instead of a copy of the object, meaning that if you change the 'copy', you'll also change the original\.*
## Members
A vector has a member for each of it's values\.
``` vec1 ``` has members: ``` (x) ```
``` vec2 ``` has members: ``` (x, y) ```
``` vec3 ``` has members: ``` (x, y, z) ```
``` vec4 ``` has members: ``` (x, y, z, w) ```
Using swizzling, you can also construct vectors from up to four members:
``` Python
v = vec4(1, 2, 3, 4)
v2 = v.xy # returns vec2(1, 2)
v3 = v.zw # returns vec2(3, 4)
v4 = v.xxxw # returns vec4(1, 1, 1, 4)
```
## Methods
Any vector type implements the following methods:
### The copy protocol
Vectors support the copy protocol \(see [here](https://docs.python.org/3/library/copy.html)\)\.
You can use ``` copy.copy(<vector>) ``` or ``` copy.deepcopy(<vector>) ``` to get a copy of a vector\.
### Pickling
Vectors support [pickling](https://docs.python.org/3/library/pickle.html#module-interface) \(as of PyGLM 2\.0\.0\), which is Python's serialization method\.
### To list / tuple
Any vector type has a ``` to_list() ``` and a ``` to_tuple() ``` function, which return's the vector's data represented as a list or tuple respectively\.
### To and from bytes
All vectors have a ``` to_bytes() ``` and a ``` from_bytes() ``` method, which allows for conversion of the vector's data to and from bytes strings\.
## Operators
Vector types support a *lot* of operators\.
### add \(``` + ``` operator\)
Vectors support addition with other vectors and numbers\.
``` Python
sum1 = vec2(1, 2) + vec2(4, 0) # returns vec2(5, 2)
sum2 = vec2(1, 2) + 4 # returns vec2(5, 6)
```
### sub \(``` - ``` operator\)
Vectors support subtraction with other vectors and numbers\.
``` Python
diff1 = vec2(1, 2) - vec2(4, 0) # returns vec2(-3, 2)
diff2 = vec2(1, 2) - 4 # returns vec2(-3, -2)
```
### mul \(``` * ``` operator\)
Vectors support multiplication with other vectors and numbers\.
``` Python
prod1 = vec2(1, 2) * vec2(4, 0) # returns vec2(4, 0)
prod2 = vec2(1, 2) * 4 # returns vec2(4, 8)
```
### matmul \(``` @ ``` operator\)
Has the same effects as the ``` * ``` operator, but with the arguments switched\.
I\.e\. ``` a * b == b @ a ```
### div \(``` / ``` operator\)
Vectors support division with other vectors and numbers\.
``` Python
quot1 = vec2(1, 2) / vec2(4, 0.5) # returns vec2(0.25, 4 )
quot2 = vec2(1, 2) / 4 # returns vec2(0.25, 0.5)
```
### mod \(``` % ``` operator\)
Vectors support modulo operations with other vectors and numbers\.
``` Python
mod1 = vec2(1, 2) % vec2(4, 2) # returns vec2(1, 0)
mod2 = vec2(1, 2) % 4 # returns vec2(1, 2)
```
### floordiv \(``` // ``` operator\)
Vectors support floored division with other vectors and numbers\.
``` Python
fquot1 = vec2(1, 2) // vec2(4, 0.5) # returns vec2(0, 4)
fquot2 = vec2(1, 2) // 4 # returns vec2(0, 0)
```
### divmod
Vectors support combined floor division and modulo operations with other vectors and numbers\.
``` Python
divmod1 = divmod(vec2(1, 2), vec2(4, 2)) # returns (vec2(0, 1), vec2(1, 0))
divmod2 = divmod(vec2(1, 2), 4) # returns (vec2(0, 0), vec2(1, 2))
```
### lshift \(``` << ``` operator\)
Integer vectors support the bitwise left shift operator\.
``` Python
>>> ivec3(1, 2, 3) << 4
ivec3( 16, 32, 48 )
>>> uvec3(1, 2, 3) << uvec3(1, 2, 3)
uvec3( 2, 8, 24 )
```
### rshift \(``` >> ``` operator\)
Integer vectors support the bitwise right shift operator\.
``` Python
>>> ivec3(16, 32, 48) >> 4
ivec3( 1, 2, 3 )
>>> uvec3(2, 8, 24) >> uvec3(1, 2, 3)
uvec3( 1, 2, 3 )
```
### and \(``` & ``` operator\)
Integer vectors support the bitwise and operator\.
``` Python
>>> ivec3(1, 2, 3) & 2
ivec3( 0, 2, 2 )
>>> uvec3(1, 2, 3) & uvec3(3, 2, 1)
uvec3( 1, 2, 1 )
```
### or \(``` | ``` operator\)
Integer vectors support the bitwise or operator\.
``` Python
>>> ivec3(1, 2, 3) | 2
ivec3( 3, 2, 3 )
>>> uvec3(1, 2, 3) | uvec3(6, 5, 4)
uvec3( 7, 7, 7 )
```
### xor \(``` ^ ``` operator\)
Integer vectors support the bitwise xor operator\.
``` Python
>>> ivec3(1, 2, 3) ^ 2
ivec3( 3, 0, 1 )
>>> uvec3(1, 2, 3) ^ uvec3(3, 2, 1)
uvec3( 2, 0, 2 )
```
### pow \(``` ** ``` operator\)
Vectors support pow operations with other vectors and numbers\.
``` Python
pow1 = vec2(1, 2) ** vec2(4, 2) # returns vec2(1, 4)
pow2 = vec2(1, 2) ** 4 # returns vec2(1, 16)
```
### len
The length of a vector can be queried using ``` len() ```\.
``` Python
vec_length = len(vec2()) # returns 2
```
### getitem and setitem \(``` [] ``` operator\)
You can get the values of a vector using indices\.
``` Python
v = vec2(1, 2)
print(v[0]) # prints 1.0
print(v[1]) # prints 2.0
```
Likewise you can set the values\.
``` Python
v = vec2(1, 2)
v[0] = 9
print(v.x) # prints 9.0
```
### contains \(``` in ``` operator\)
You can query wether or not a value is contained by a vector using the ``` in ``` operator\.
``` Python
v = vec2(1, 2)
true = 2 in v
false = 2.01 in v
```
### richcompare \(e\.g\. ``` == ``` operator\)
You can compare vectors using the richcompare operators:
``` Python
vec2(1, 2) == vec2(1, 2) # True
vec2(1, 2) == vec2(2, 2) # False
vec2(1, 2) == vec3(1, 2, 3) # False
vec2(1, 2) != vec2(1, 2) # False
vec2(1, 2) != vec2(2, 2) # True
vec2(1, 2) != vec3(1, 2, 3) # True
vec2(1, 2) < vec2(5, 5) # vec2(1, 1)
vec2(1, 2) < vec2(2, 2) # vec2(1, 0)
vec2(1, 2) < vec2(0, 0) # vec2(0, 0)
vec2(1, 2) <= vec2(5, 5) # vec2(1, 1)
vec2(1, 2) <= vec2(2, 2) # vec2(1, 1)
vec2(1, 2) <= vec2(0, 0) # vec2(0, 0)
vec2(1, 2) > vec2(5, 5) # vec2(0, 0)
vec2(1, 2) > vec2(2, 2) # vec2(0, 0)
vec2(1, 2) > vec2(0, 0) # vec2(1, 1)
vec2(1, 2) >= vec2(5, 5) # vec2(0, 0)
vec2(1, 2) >= vec2(2, 2) # vec2(0, 1)
vec2(1, 2) >= vec2(0, 0) # vec2(1, 1)
```
### iter
You can generate an iterable from vectors using ``` iter() ```\.
``` Python
v = vec2(1, 2)
it = iter(v)
print(next(it)) # prints 1.0
print(next(it)) # prints 2.0
```
### hash
You can generate a hash value for vectors using ``` hash() ```
Example:
``` Python
>>> v = vec2()
>>> hash(v)
-1952026010959490761
>>> v2 = vec2(1, 2)
>>> hash(v2)
8639716006723752019
>>> v3 = v2 * 0
>>> hash(v3)
-1952026010959490761
```
|