add some matrix functions

main
Daniel Barlow 2022-07-03 14:03:35 +01:00
parent 07fd321f94
commit 69753c2345
2 changed files with 61 additions and 7 deletions

55
matrix.fnl Normal file
View File

@ -0,0 +1,55 @@
;; homogeneous matrix operations for 2d graphics
(local identity
[
1 0 0
0 1 0
0 0 1
])
(fn multiply-matrix [a b]
[
(+ (* (. a 1) (. b 1)) (* (. a 2) (. b 4)) (* (. a 3) (. b 7)))
(+ (* (. a 1) (. b 2)) (* (. a 2) (. b 5)) (* (. a 3) (. b 8)))
(+ (* (. a 1) (. b 3)) (* (. a 2) (. b 6)) (* (. a 3) (. b 9)))
(+ (* (. a 4) (. b 1)) (* (. a 5) (. b 4)) (* (. a 6) (. b 7)))
(+ (* (. a 4) (. b 2)) (* (. a 5) (. b 5)) (* (. a 6) (. b 8)))
(+ (* (. a 4) (. b 3)) (* (. a 5) (. b 6)) (* (. a 6) (. b 9)))
(+ (* (. a 7) (. b 1)) (* (. a 8) (. b 4)) (* (. a 9) (. b 7)))
(+ (* (. a 7) (. b 2)) (* (. a 8) (. b 5)) (* (. a 9) (. b 8)))
(+ (* (. a 7) (. b 3)) (* (. a 8) (. b 6)) (* (. a 9) (. b 9)))
])
(fn scale [matrix x y]
(let [scale [
x 0 0
0 y 0
0 0 1]]
(multiply-matrix matrix scale)))
(fn translate [matrix x y]
(let [translation [
1 0 x
0 1 y
0 0 1]]
(multiply-matrix matrix translation)))
(fn rotate [matrix rad]
(let [sin (math.sin rad)
cos (math.cos rad)
rotation [
cos (- sin) 0
sin cos 0
0 0 1
]]
(multiply-matrix matrix rotation)))
{
: identity
: scale
: translate
: rotate
}

13
rc.fnl
View File

@ -1,6 +1,7 @@
(local { : view } (require :fennel))
(local texture (require :texture))
(local matrix (require :matrix))
(local socket-repl (require :socket-repl))
(let [repl-socket-name
@ -23,25 +24,23 @@
spinner (texture.from-file r "carousel.png")]
(output:on "render"
(fn [{: output : renderer}]
(let [bar-height 40
matrix [1 0 0
0 1 0
0 0 1]]
(let [bar-height 40]
(renderer:draw_rect :#00000077
0 (- 720 bar-height)
690 360 bar-height)
(renderer:draw_texture
kill
matrix
matrix.identity
30 (- 720 bar-height)
0.7)
(renderer:draw_texture
launch matrix
launch
matrix.identity
(- 180 (/ bar-height 2)) (- 720 bar-height)
0.7)
(renderer:draw_texture
spinner
matrix
matrix.identity
(- 360 30 bar-height) (- 720 bar-height)
0.7)))))))