-
Notifications
You must be signed in to change notification settings - Fork 0
Home
shBLOCK edited this page Dec 2, 2023
·
11 revisions
Easy-to-use and fast vector classes implemented entirely in Cython.
All vector classes: Vec2
Vec3
Vec4
and integer vectors Vec2i
Vec3i
Vec4i
.
Since the 6 vector classes are mostly similar, Vec3 is used in this section, and methods specific to a class are particularized.
-
Vec3(x: float | int, y: float | int, z: float | int)
returnsVec3(x, y, z)
-
Vec3(value: float | int)
returnsVec3(value, value, value)
-
Vec3()
returns Vec3(0.0, 0.0, 0.0) -
Vec3(vec: Vec3)
returns a copy of that vector -
Vec3(vec: Vec3i)
converts Vec3i to Vec3 - Any combination of vectors and numbers that has 3 dimensions in total is also supported, however, this doesn't include vector classes of a different data type.
Vec3(a: Vec2, b: float | int)
Vec3(a: float | int, b: Vec2)
-
x: float
y: float
z: float
-
length: float
the length of the vector -
length_sqr: float
the squared length of the vector -
normalized: Vec3
the normalized vector - "Swizzling": any combinations of
x
y
z
o
l
are properties of the vector.- These properties return a vector of the length of the property's name.
-
x
y
z
represents the components of this vector. -
o
represents zero andl
represents one. - Combinations consisting of only constants (
o
andl
) are not included (eg.vec.lol
). - Examples (Assume that
vec = Vec3(2, 3, 4)
):-
vec.zyx
givesVec3(4.0, 3.0, 2.0)
-
vec.zx
givesVec2(4.0, 2.0)
-
vec.xxxx
givesVec4(2.0, 2.0, 2.0, 2.0)
-
vec.xoly
givesVec4(2.0, 0.0, 1.0, 4.0)
-
In this section, a
and b
represent two Vec3
s.
a + b
a - b
-
a * b
multiplied each component ofa
andb
together -
a / b
divide each component ofa
byb
- For
+
-
*
and/
,b
can also be a number, in whichb
is treated asVec3(b)
-
+a
returns a copy ofa
-
-a
the inverse vector ofa
-
a == b
a != b
check for exact equality -
a.is_close(b, rel_tol=1e-5, abs_tol=1e-14)
check for equality with tolerance (similar tomath.isclose()
) -
bool(a)
returnsTrue
ifa
is NOT a zero vector,False
otherwise -
a @ b
the dot product ofa
andb
-
a ^ b
the cross product ofa
andb
(Only avaliable inVec3
andVec3i
) -
len(a)
returns the component count ofa
(NOT its length!!!) a.distance_to(b)
-
a.distance_sqr_to(b)
returns the squared distance betweena
andb
-
a | b
same asa.distance_to(b)
-
a[i]
a[i] = value
get and set thei
th component ofa
- Vectors are iterable:
iter(a)
returns an internal iterator object
vec = Vec3(1, 2, 3)
for i, value in enumerate(vec):
print(f"Component {i}: {value}")
x, y, z = vec
print("Unpacking:", x, y, z)
print("Unpacking to params:", *vec)
print("Unpacking to list:", [*vec])
vec = Vec3(1, 2, 2)
print("Unpacking to set:", {*vec})
Output:
Component 0: 1.0
Component 1: 2.0
Component 2: 3.0
Unpacking: 1.0 2.0 3.0
Unpacking params: 1.0 2.0 3.0
Unpacking to list: [1.0, 2.0, 3.0]
Unpacking to set: {1.0, 2.0}
Representing a linear transformation (a 2*3 matrix) in 2D.
-
Transform2D()
returns an identity transform Transform2D(xx: float, xy: float, yx: float, yy: float, ox: float, oy: float)
-
Transform2D(x: Vec2, y: Vec2, origin: Vec2)
constructs the transform from the 3 columns -
Transform2D(transform: Transform2D)
returns a copy oftransform
-
Transform2D.translating(translation: Vec2)
returns a transform representing a translation -
Transform2D.scaling(scale: Vec2, origin: Vec2 = None)
returns a transform representing a scaling transformation, and optionally a translation in addition -
Transform2D.rotating(rotation: float, origin: Vec2 = None)
returns a transform representing a rotation, and optionally a translation in addition
-
x: Vec2
y: Vec2
origin: Vec2
the columns of the matrix (origin
can be seen as the translation) -
determinant
returns the determinant of the matrix rotation: float
scale: Vec2
In this section, a
and b
represent two Transform2D
s and v
to represent a Vec2
.
-
a @ b
a(b)
matrix multiplication (b
transformed bya
) -
a @= b
inplace transformsa
withb
, which is NOT equivalent toa = a @ b
! This is to make in place transforming a transform with another more convenient. -
a * v
a(v)
returnv
transformed bya
-
v * a
returnsv
transformed by the INVERSE ofa
-
~a
the inverse ofa
-
a == b
a != b
check for exact equality -
a.is_close(b, rel_tol=1e-5, abs_tol=1e-14)
check for equality with tolerance (similar tomath.isclose()
) -
a[i]
a[i] = v
gets or sets thei
th column of the matrix (0 forx
, 1 fory
, and 2 fororigin
) -
len(a)
the number of columns (always 3) -
a.translated(translation: Vec2)
returns a copy of this transform translated bytranslation
-
a.rotated(rotation: float)
returns a copy of this transform rotated byrotation
-
a.scaled(scale: Vec2)
returns a copy of this transform scaled byscale
-
translate_ip
rotate_ip
andscale_ip
as the in-place counterparts oftranslated
rotated
andscaled
Representing a linear transformation (a 3*4 matrix) in 3D.
-
Transform3D()
returns an identity transform Transform3D(xx: float, xy: float, xz: float, yx: float, yy: float, yz: float, zx: float, zy: float, zz: float, ox: float, oy: float, oz: float)
-
Transform3D(x: Vec3, y: Vec3, z: Vec3, origin: Vec3)
constructs the transform from the 4 columns -
Transform3D(transform: Transform3D)
returns a copy oftransform
-
Transform3D.translating(translation: Vec3)
returns a transform representing a translation -
Transform3D.scaling(scale: Vec3, origin: Vec3 = None)
returns a transform representing a scaling transformation, and optionally a translation in addition -
Transform3D.rotating(axis: Vec3, angle: float, origin: Vec2 = None)
returns a transform representing a rotation aroundaxis
(axis
must be normalized), and optionally a translation in addition
-
x: Vec3
y: Vec3
z: Vec3
origin: Vec3
the columns of the matrix (origin
can be seen as the translation) -
determinant
returns the determinant of the matrix
In this section, a
and b
represent two Transform3D
s and v
to represent a Vec3
.
-
a @ b
a(b)
matrix multiplication (b
transformed bya
) -
a @= b
inplace transformsa
withb
, which is NOT equivalent toa = a @ b
! This is to make in place transforming a transform with another more convenient. -
a * v
a(v)
returnv
transformed bya
-
v * a
returnsv
transformed by the INVERSE ofa
-
~a
the inverse ofa
-
a == b
a != b
check for exact equality -
a.is_close(b, rel_tol=1e-5, abs_tol=1e-14)
check for equality with tolerance (similar tomath.isclose()
) -
a[i]
a[i] = v
gets or sets thei
th column of the matrix (0 forx
, 1 fory
, 2 forz
, and 3 fororigin
) -
len(a)
the number of columns (always 4) -
a.translated(translation: Vec3)
returns a copy of this transform translated bytranslation
-
a.rotated(axis: Vec3, angle: float)
returns a copy of this transform rotated byangle
aroundaxis
(must be normalized) -
a.scaled(scale: Vec3)
returns a copy of this transform scaled byscale
-
translate_ip
rotate_ip
andscale_ip
as the in-place counterparts oftranslated
rotated
andscaled