Skip to content

Commit

Permalink
ascii matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
krux02 committed Jul 12, 2018
1 parent e3102c2 commit 4811599
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ There is swizzling support:
color.rgb = color.bgr


matrices can be printed with echo, because they have tho `$` operator
implemented. My default they use nice unicode characters for best
visual representation. But if you have probmles with them, you can
pass ``-d:noUnicode`` to the compiler and the ``$`` functions will use
a pure ASCII representation.

ASCII unicode

/ 3 7 3 0 \ ⎡3 7 3 0⎤
| 0 2 -1 1 | ⎢0 2 -1 1⎥
| 5 4 3 2 | ⎢5 4 3 2⎥
\ 6 6 4 -1 / ⎣6 6 4 -1⎦

perlin noise:

import glm/vec
Expand Down Expand Up @@ -83,13 +96,15 @@ perlin noise:

* Changes regarding based to C++glm and glsl

- the `mod` function is called `modulo` instead. `mod` is already an
operator in Nim and has it's own meaning that is very different to
the meaning of the `mod` function in glsl. The name `fmod` is also
not good, because `fmod` in c++ has also a different meaning.
Therefore `mod` is simply named `modulo` in nim-glm. The other
mod functions all have a behavior towards zero, modulo does not
have this.
- the `mod` function is called `floorMod` instead. `mod` is already
an operator in Nim and has it's own meaning that is very different
to the meaning of the `mod` function in glsl. The name `fmod` is
also not good, because `fmod` in c++ has also a different meaning.
The function `floorMod` from the ``math`` package has the same
meaning as the `mod` function in glsl. Therefore `mod` is simply
named `floorMod` to be at least consistent with the Nim standard
library. The `mod` operator always rounds towards zero, I
recommend to never use this operator.

- swizzle support. Unlike c++, Nim allows pretty well to implement
swizzling. So it is implemented with least amount of surprise.
Expand Down
19 changes: 13 additions & 6 deletions glm/mat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ type
Mat*[M,N: static[int]; T] = object
arr*: array[M, Vec[N,T]]

when defined(noUnicode):
const matrixDecoration = [" / ", " \\ ", "| ", " \\ ", " / ", " |"]
else:
const matrixDecoration = ["", "", "", "", "", ""]

proc `$`*(m: Mat): string =
var cols: array[m.M, array[m.N, string]]
for i, col in m.arr:
Expand All @@ -15,23 +20,25 @@ proc `$`*(m: Mat): string =
result = ""
for row in 0 ..< m.N:
if row == 0:
result &= ""
result &= matrixDecoration[0]
elif row == m.N - 1:
result &= ""
result &= matrixDecoration[1]
else:
result &= ""
result &= matrixDecoration[2]

for col in 0 ..< m.M:
if col != 0:
result &= " "
result &= cols[col][row]

if row == 0:
result &= "\n"
result &= matrixDecoration[3]
elif row == m.N - 1:
result &= "\n"
result &= matrixDecoration[4]
else:
result &= "\n"
result &= matrixDecoration[5]

result &= "\n"

proc `[]=`*[M,N,T](v:var Mat[M,N,T]; ix:int; c:Vec[N,T]): void {.inline.} =
v.arr[ix] = c
Expand Down

0 comments on commit 4811599

Please sign in to comment.