Skip to content

Commit 4811599

Browse files
committed
ascii matrices
1 parent e3102c2 commit 4811599

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ There is swizzling support:
4343
color.rgb = color.bgr
4444

4545

46+
matrices can be printed with echo, because they have tho `$` operator
47+
implemented. My default they use nice unicode characters for best
48+
visual representation. But if you have probmles with them, you can
49+
pass ``-d:noUnicode`` to the compiler and the ``$`` functions will use
50+
a pure ASCII representation.
51+
52+
ASCII unicode
53+
54+
/ 3 7 3 0 \ ⎡3 7 3 0⎤
55+
| 0 2 -1 1 | ⎢0 2 -1 1⎥
56+
| 5 4 3 2 | ⎢5 4 3 2⎥
57+
\ 6 6 4 -1 / ⎣6 6 4 -1⎦
58+
4659
perlin noise:
4760

4861
import glm/vec
@@ -83,13 +96,15 @@ perlin noise:
8396

8497
* Changes regarding based to C++glm and glsl
8598

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

94109
- swizzle support. Unlike c++, Nim allows pretty well to implement
95110
swizzling. So it is implemented with least amount of surprise.

glm/mat.nim

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ type
77
Mat*[M,N: static[int]; T] = object
88
arr*: array[M, Vec[N,T]]
99

10+
when defined(noUnicode):
11+
const matrixDecoration = [" / ", " \\ ", "| ", " \\ ", " / ", " |"]
12+
else:
13+
const matrixDecoration = ["", "", "", "", "", ""]
14+
1015
proc `$`*(m: Mat): string =
1116
var cols: array[m.M, array[m.N, string]]
1217
for i, col in m.arr:
@@ -15,23 +20,25 @@ proc `$`*(m: Mat): string =
1520
result = ""
1621
for row in 0 ..< m.N:
1722
if row == 0:
18-
result &= ""
23+
result &= matrixDecoration[0]
1924
elif row == m.N - 1:
20-
result &= ""
25+
result &= matrixDecoration[1]
2126
else:
22-
result &= ""
27+
result &= matrixDecoration[2]
2328

2429
for col in 0 ..< m.M:
2530
if col != 0:
2631
result &= " "
2732
result &= cols[col][row]
2833

2934
if row == 0:
30-
result &= "\n"
35+
result &= matrixDecoration[3]
3136
elif row == m.N - 1:
32-
result &= "\n"
37+
result &= matrixDecoration[4]
3338
else:
34-
result &= "\n"
39+
result &= matrixDecoration[5]
40+
41+
result &= "\n"
3542

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

0 commit comments

Comments
 (0)