Stream: show and tell

Topic: roc-linear-algebra


view this post on Zulip Hannes (Jul 02 2024 at 14:13):

I started working on a linear algebra package for Roc. It's very limited in scope, only supporting vectors and matrices up to size 4x4, but those are the sizes that are useful for 2D and 3D transforms, so hopefully this will be useful for some gamedev stuff :)

Repo: https://github.com/Hasnep/roc-linear-algebra
Docs: https://hasnep.github.io/roc-linear-algebra/

Example:

import linalg.Matrix4x4
import linalg.Vector4

vecA = (1, 2, 3, 4)
Stdout.line! "Vector a is $(Vector4.display vecA)."

vecB = (5, 6, 7, 8)
Stdout.line! "Vector b is $(Vector4.display vecB)."

vecC = Vector4.add vecA vecB
Stdout.line! "The sum of a and b is $(Vector4.display vecC)."

aDotB = Vector4.dot vecA vecB
Stdout.line! "The dot product of a and b is $(Num.toStr aDotB)."

mat = Matrix4x4.fromDiagonal vecC
det = Matrix4x4.determinant mat
Stdout.line! "The determinant of the matrix is $(Num.toStr det)."

Outputs:

Vector a is [1, 2, 3, 4].
Vector b is [5, 6, 7, 8].
The sum of a and b is [6, 8, 10, 12].
The dot product of a and b is 70.
The determinant of the matrix is 5760.

view this post on Zulip Jared Cone (Sep 21 2024 at 06:05):

Interesting using a tuple for vectors. That makes them a little less verbose to create. For accessing you use .0, .1 notation right? Instead of .x, .y etc

view this post on Zulip Hannes (Sep 22 2024 at 02:37):

Yes, and you can also use destructuring to name the elements:

v = Vector3.foo

# Destructure a value
(x, y, z) = v

# Destructure an argument
f = \(x, y, z) ->
    ...

Although I don't know what you'd call the fourth element of a Vector4 :shrug: I'm sure there's a convention

view this post on Zulip Kilian Vounckx (Sep 22 2024 at 09:38):

It's often called w where I've seen it


Last updated: Jul 06 2025 at 12:14 UTC