eufon

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

matrix.fnl (1430B)


      1 ;; homogeneous matrix operations for 2d graphics
      2 
      3 (local identity
      4        [
      5         1 0 0
      6         0 1 0
      7         0 0 1
      8         ])
      9 
     10 (fn multiply-matrix [a b]
     11   [
     12    (+ (* (. a 1) (. b 1))   (* (. a 2) (. b 4))   (* (. a 3) (. b 7)))
     13    (+ (* (. a 1) (. b 2))   (* (. a 2) (. b 5))   (* (. a 3) (. b 8)))
     14    (+ (* (. a 1) (. b 3))   (* (. a 2) (. b 6))   (* (. a 3) (. b 9)))
     15 
     16    (+ (* (. a 4) (. b 1))   (* (. a 5) (. b 4))   (* (. a 6) (. b 7)))
     17    (+ (* (. a 4) (. b 2))   (* (. a 5) (. b 5))   (* (. a 6) (. b 8)))
     18    (+ (* (. a 4) (. b 3))   (* (. a 5) (. b 6))   (* (. a 6) (. b 9)))
     19 
     20    (+ (* (. a 7) (. b 1))   (* (. a 8) (. b 4))   (* (. a 9) (. b 7)))
     21    (+ (* (. a 7) (. b 2))   (* (. a 8) (. b 5))   (* (. a 9) (. b 8)))
     22    (+ (* (. a 7) (. b 3))   (* (. a 8) (. b 6))   (* (. a 9) (. b 9)))
     23    ])
     24 
     25 (fn scale [matrix x y]
     26   (let [scale [
     27                x 0 0
     28                0 y 0
     29                0 0 1]]
     30     (multiply-matrix matrix scale)))
     31 
     32 (fn translate [matrix x y]
     33   (let [translation [
     34                      1 0 x
     35                      0 1 y
     36                      0 0 1]]
     37     (multiply-matrix matrix translation)))
     38 
     39 (fn rotate [matrix rad]
     40   (let [sin (math.sin rad)
     41         cos (math.cos rad)
     42         rotation [
     43                   cos (- sin) 0
     44                   sin  cos    0
     45                   0     0     1
     46                   ]]
     47     (multiply-matrix matrix rotation)))
     48 
     49 
     50 {
     51  : identity
     52  : scale
     53  : translate
     54  : rotate
     55  }