-
Notifications
You must be signed in to change notification settings - Fork 1
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: floaty: floatz: float -
length: floatthe length of the vector -
length_sqr: floatthe squared length of the vector -
normalized: Vec3the normalized vector - "Swizzling": any combinations of
xyzolare properties of the vector.- These properties return a vector of the length of the property's name.
-
xyzrepresents the components of this vector. -
orepresents zero andlrepresents one. - Combinations consisting of only constants (
oandl) are not included (eg.vec.lol). - Examples (Assume that
vec = Vec3(2, 3, 4)):-
vec.zyxgivesVec3(4.0, 3.0, 2.0) -
vec.zxgivesVec2(4.0, 2.0) -
vec.xxxxgivesVec4(2.0, 2.0, 2.0, 2.0) -
vec.xolygivesVec4(2.0, 0.0, 1.0, 4.0)
-
In this section, a and b represent two Vec3s.
a + ba - b-
a * bmultiplied each component ofaandbtogether -
a / bdivide each component ofabyb - For
+-*and/,bcan also be a number, in whichbis treated asVec3(b) -
+areturns a copy ofa -
-athe inverse vector ofa -
a == ba != bcheck 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)returnsTrueifais NOT a zero vector,Falseotherwise -
a @ bthe dot product ofaandb -
a ^ bthe cross product ofaandb(Only avaliable inVec3andVec3i) -
len(a)returns the component count ofa(NOT its length!!!) a.distance_to(b)-
a.distance_sqr_to(b)returns the squared distance betweenaandb -
a | bsame asa.distance_to(b) -
a[i]a[i] = valueget and set theith 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: Vec2y: Vec2origin: Vec2the columns of the matrix (origincan be seen as the translation) -
determinantreturns the determinant of the matrix rotation: floatscale: Vec2
In this section, a and b represent two Transform2Ds and v to represent a Vec2.
-
a @ ba(b)matrix multiplication (btransformed bya) -
a @= binplace transformsawithb, which is NOT equivalent toa = a @ b! This is to make in place transforming a transform with another more convenient. -
a * va(v)returnvtransformed bya -
v * areturnsvtransformed by the INVERSE ofa -
~athe inverse ofa -
a == ba != bcheck 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] = vgets or sets theith 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_iprotate_ipandscale_ipas the in-place counterparts oftranslatedrotatedandscaled
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(axismust be normalized), and optionally a translation in addition
-
x: Vec3y: Vec3z: Vec3origin: Vec3the columns of the matrix (origincan be seen as the translation) -
determinantreturns the determinant of the matrix
In this section, a and b represent two Transform3Ds and v to represent a Vec3.
-
a @ ba(b)matrix multiplication (btransformed bya) -
a @= binplace transformsawithb, which is NOT equivalent toa = a @ b! This is to make in place transforming a transform with another more convenient. -
a * va(v)returnvtransformed bya -
v * areturnsvtransformed by the INVERSE ofa -
~athe inverse ofa -
a == ba != bcheck 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] = vgets or sets theith 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 byanglearoundaxis(must be normalized) -
a.scaled(scale: Vec3)returns a copy of this transform scaled byscale -
translate_iprotate_ipandscale_ipas the in-place counterparts oftranslatedrotatedandscaled