Design choices are still being made over the Vec3 and Vec4 classes. Mostly due to graphical rotation matrices supporting both Vec3 and Vec4 classes. Currently a large number of function are unimplemented for the Vec4 class.
The vector classes contain parameters x, y, z and w depending on the class, as follows,
Vec2 (x, y)
Vec3 (x, y, z)
Vec4 (x, y, z, w)
- Initalisation
- Standard
- Comparison
- Equal
- [Not Equal](#Not Equal)
- [Extra functionality](#Extra Functionality)
- Zero
- [Distance Between Vectors](#Distance Between Vectors)
- Magnitude
- Normalize
- [Cross Product](#Cross Product)
- [Dot Product](#Dot Product)
- [Angle Between Vectors](#Angle Between Vectors)
- [Midpoint Between Vectors](#Midpoint Between Vectors)
- [Rotate Vector](#Rotate Vector)
- [Set Vector Rotation](#Set Vector Rotation)
- [Scalar Triple Product](#Scalar Triple Product)
- [Vector Triple Product](#Vector Triple Product)
Calling the vector with out parameters will initalise a vector to zero
>>> import pyclid
>>> pyclid.Vec2()
<0, 0>
>>> pyclid.Vec3()
<0, 0, 0>
>>> pyclid.Vec4()
<0, 0, 0, 0>
Or vectors can be initalised with parameters
>>> pyclid.Vec2(1, 2)
<1, 2>
>>> pyclid.Vec3(1, 2, 3)
<1, 2, 3>
>>> pyclid.Vec4(1, 2, 3, 4)
<1, 2, 3, 4>
Requires two Vec2 objects,
Returns a Vec2 object
>>> v1 = pyclid.Vec2(1, 2)
>>> v2 = pyclid.Vec2(3, 4)
>>> v3 = v1 + v2
>>> print v3
<4, 6>
>>> v1 = pyclid.Vec2(6, 2)
>>> v2 = pyclid.Vec2(1, 2)
>>> v3 = v1-v2
>>> print v3
<5, 0>
Must be used with a scalar value
>>> v = pyclid.Vec2(2, 4)
>>> print v*2
<4, 8>
Must be used with a scalar value
>>> v = pyclid.Vec2(2, 4)
>>> print a/2
<1, 2>
Comparison is performed on the same parameters between two vector objects or the same type
>>> v1 = pyclid.Vec2(1, 2)
>>> v2 = pyclid.Vec2(1, 2)
>>> v1 == v2
True
>>> v1 = pyclid.Vec2(1, 2)
>>> v2 = pyclid.Vec2(1, 3)
>>> v1 == v2
False
>>> v1 = pyclid.Vec2(1, 1)
>>> v2 = pyclid.Vec2(1, 2)
>>> v1 != v2
True
Sets the vector parameters to zero
>>> v = pyclid.Vec2(1, 2)
>>> v.zero()
<0, 0>
Find the distance between two vectors using the following equation,
distance = sqrt((u.x**2 - v.x**2) + (u.y**2 - v.y**2))
>>> a = pyclid.Vec2(0, 0)
>>> b = pyclid.Vec2(1, 1)
>>> a.distance_between(b)
1.4142135623730951
Returns a float of the magnitue of the vector. Note, the abs() operator can be used in place of magnitude
>>> v = pyclid.Vec2(3, 4)
>>> v.magnitude()
5.0
>>> abs(v)
5.0
Scales the current vector so that its magnitude is equal to 1.0
>>> v = pyclid.Vec2(3, 4)
>>> v.normalize()
<0.6, 0.8>
Vector cross Product
>>> v = pyclid.Vec2(1, 2)
>>> u = pyclid.Vec2(3, 4)
>>> v.cross(u)
-2
Vector dot product
>>> v = pyclid.Vec2(1, 2)
>>> u = pyclid.Vec2(3, 4)
>>> v.cross(u)
11
Returns the angle between two vectors using <0, 0> as root
>>> v = pyclid.Vec2(0, 1)
>>> u = pyclid.Vec2(1, 0)
>>> v.angle(u)
1.57079632679
(0, 1)
|
|
(0, 0)|_____(1, 0)
Finds the midpoint between two vectors
>>> v = pyclid.Vec2(0, 0)
>>> u = pyclid.Vec2(2, 2)
>>> v.mid_point(u)
<1.0, 1.0>
Rotates a vector about <0, 0> Accepts an angle or a rotation matrix
If a rotation matrix is used it must be of the form pyclid.Mat2()
Matrix
|x1, y1|
|x2, y2|
Vector.x = x1*Vector.x + y1*Vector.y
Vector.y = x2*Vector.x + y2*Vector.y
If a float is supplied the roation is calculated based on
Vector.x = cos(angle)*Vector.x - sin(angle)*Vector.y
Vector.y = sin(angle)*Vector.x + cos(angle)*Vector.y
>>> # Rotate by 90 deg, note floating point error
>>> v = Vec2(1, 0)
>>> v.rotate(math.pi/2.0)
<6.12323399574e-17, 1.0>
>>> v.rotate(math.pi/2.0)
<-1.0, 1.22464679915e-16>
Similar to rotate, however sets the vector to the applied angle (does not apply a rotation ontop of the current rotation)
Currently a bug in this function
Requires three Vec3 vectors, result is the dot product of one of the vectors with the cross product of the other two
a · (b × c)
>>> a = pyclid.Vec3(1, 0, 0)
>>> b = pyclid.Vec3(0, 1, 0)
>>> c = pyclid.Vec3(0, 0, 1)
>>> a.triple_s(b, c)
1
Requires three Vec3 vectors, result is the cross product of one of the vectors with the cross product of the other two vectors
a × (b × c)
>>> a = Vec3(1, 2, 3)
>>> b = Vec3(2, 3, 1)
>>> c = Vec3(1, 1, 2)
>>> a.triple_v(b, c)
<7, 16, -13>