MATRIX(3)

๐Ÿ“ฆ version 0_git20260208, in alpine 3.24, arch rolling, nixos 26.05, nixpkgs unstable

okay so bestie, welcome to MATRIX(3) โ€” the Library Functions Manual page for Geometric transformations, and itโ€™s giving math-girl main character energy ๐Ÿ’…โœจ. every single detail is staying, no cap.

๐ŸŒธ NAME ๐ŸŒธ

  • ident, matmul, matmulr, determinant, adjoint, invertmat, xformpoint, xformpointd, xformplane, pushmat, popmat, rot, qrot, scale, move, xform, ixform, persp, look, viewport โ€” Geometric transformations ๐Ÿฆ‹

โœจ SYNOPSIS โœจ

first, the includes, bestie:

#include <draw.h>

#include <geometry.h>

then the whole function lineup, exactly as written:

void ident(Matrix m)

void matmul(Matrix a, Matrix b)

void matmulr(Matrix a, Matrix b)

double determinant(Matrix m)

void adjoint(Matrix m, Matrix madj)

double invertmat(Matrix m, Matrix inv)

Point3 xformpoint(Point3 p, Space *to, Space *from)

Point3 xformpointd(Point3 p, Space *to, Space *from)

Point3 xformplane(Point3 p, Space *to, Space *from)

Space *pushmat(Space *t)

Space *popmat(Space *t)

void rot(Space *t, double theta, int axis)

void qrot(Space *t, Quaternion q)

void scale(Space *t, double x, double y, double z)

void move(Space *t, double x, double y, double z)

void xform(Space *t, Matrix m)

void ixform(Space *t, Matrix m, Matrix inv)

int persp(Space *t, double fov, double n, double f)

void look(Space *t, Point3 eye, Point3 look, Point3 up)

void viewport(Space *t, Rectangle r, double aspect)

๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay so these routines manipulate 3-space affine and projective transformations, represented as 4ร—4 matrices, like this:

typedef double Matrix[4][4];

the matrix girlies first:

  • ident โ€” stores an identity matrix in its argument. โœจ
  • matmul โ€” stores aร—b in a.
  • matmulr โ€” stores bร—a in b.
  • determinant โ€” returns the determinant of matrix m.
  • adjoint โ€” stores the adjoint (matrix of cofactors) of m in madj.
  • invertmat โ€” stores the inverse of matrix m in minv, returning mโ€™s determinant. should m be singular (determinant zero), invertmat stores its adjoint in minv. ๐Ÿซถ

the rest of the routines described here manipulate Spaces and transform Point3s. a Point3 is a point in three-space, represented by its homogeneous coordinates:

typedef struct Point3 Point3;
struct Point3{
      double x, y, z, w;
};

the homogeneous coordinates (x, y, z, w) represent the Euclidean point (x/w, y/w, z/w) if wโ‰ 0, and a โ€œpoint at infinityโ€ if w=0.

a Space is just a data structure describing a coordinate system:

typedef struct Space Space;
struct Space{
      Matrix t;
      Matrix tinv;
      Space *next;
};

it contains a pair of transformation matrices and a pointer to the Spaceโ€™s parent. the matrices transform points to and from the โ€œroot coordinate system,โ€ which is represented by a null Space pointer.

pushmat creates a new Space. its argument is a pointer to the parent space. its result is a newly allocated copy of the parent, but with its next pointer pointing at the parent. popmat discards the Space that is its argument, returning a pointer to the stack. nominally, these two functions define a stack of transformations, but pushmat can be called multiple times on the same Space multiple times, creating a transformation tree. ๐ŸŒฑ

xformpoint and xformpointd both transform points from the Space pointed to by from to the space pointed to by to. either pointer may be null, indicating the root coordinate system. the difference between the two functions is that xformpointd divides x, y, z, and w by w, if wโ‰ 0, making (x, y, z) the Euclidean coordinates of the point. ๐Ÿ’ซ

xformplane transforms planes or normal vectors. a plane is specified by the coefficients (a, b, c, d) of its implicit equation ax+by+cz+d=0. since this representation is dual to the homogeneous representation of points, libgeometry represents planes by Point3 structures, with (a, b, c, d) stored in (x, y, z, w). ๐Ÿฆ‹

the remaining functions transform the coordinate system represented by a Space. their Space * argument must be non-null โ€” you canโ€™t modify the root Space. rot rotates by angle theta (in radians) about the given axis, which must be one of XAXIS, YAXIS or ZAXIS. qrot transforms by a rotation about an arbitrary axis, specified by Quaternion q. ๐Ÿ”ฅ

scale scales the coordinate system by the given scale factors in the directions of the three axes. move translates by the given displacement in the three axial directions.

xform transforms the coordinate system by the given Matrix. if the matrixโ€™s inverse is known a priori, calling ixform will save the work of recomputing it. ๐Ÿ‘‘

persp does a perspective transformation. the transformation maps the frustum with apex at the origin, central axis down the positive y axis, and apex angle fov and clipping planes y=n and y=f into the double-unit cube. the plane y=n maps to y'=-1, y=f maps to y'=1.

look does a view-pointing transformation. the eye point is moved to the origin. the line through the eye and look points is aligned with the y axis, and the plane containing the eye, look and up points is rotated into the x-y plane. โœจ

viewport maps the unit-cube window into the given screen viewport. the viewport rectangle r has r.min at the top left-hand corner, and r.max just outside the lower right-hand corner. argument aspect is the aspect ratio (dx/dy) of the viewportโ€™s pixels (not of the whole viewport). the whole window is transformed to fit centered inside the viewport with equal slop on either top and bottom or left and right, depending on the viewportโ€™s aspect ratio. the window is viewed down the y axis, with x to the left and z up. the viewport has x increasing to the right and y increasing down. the windowโ€™s y coordinates are mapped, unchanged, into the viewportโ€™s z coordinates. ๐Ÿ’…

๐ŸŽ€ SOURCE ๐ŸŽ€

/src/libgeometry/matrix.c โ€” thatโ€™s the source file, bestie. slay.

๐ŸŒˆ SEE ALSO ๐ŸŒˆ

  • arith3(3) โ€” go read this one next, iykyk ๐Ÿ’œ

๐Ÿ’– xoxo, your Unix bestie โ€” source/version: /src/libgeometry/matrix.c; footer tag: MATRIX(3); date: not in the original, bestie. periodt. ๐Ÿซถ