diff --git a/common.scm b/common.scm index 368c43b..99de2ef 100644 --- a/common.scm +++ b/common.scm @@ -1,3 +1,5 @@ +(import (srfi :43)) ; vector extensions + ; returns a list of numbers parsed from the first line of the file, ; separated by commas (define (read-comma-separated-numbers file) @@ -14,12 +16,16 @@ (define-record-type matrix (fields width height data)) +; builds a matrix record from the given 2D vector +; vectors inside data vector are expected to all be the same length (define (matrix-from-data data) (make-matrix (vector-length (vector-ref data 0)) (vector-length data) data)) +; returns value at x,y coordinates in matrix +; if coordinate is out of bounds, returns default (define (matrix-get matrix x y default) (if (or (< x 0) (< y 0) @@ -28,6 +34,8 @@ default (vector-ref (vector-ref (matrix-data matrix) y) x))) +; set value at x,y in matrix with given one +; if coordinate is out of bounds, nothing happens (define (matrix-set! matrix x y value) (when (and (>= x 0) (>= y 0) @@ -35,6 +43,12 @@ (< y (matrix-height matrix))) (vector-set! (vector-ref (matrix-data matrix) y) x value))) +(define (matrix-print matrix) + (let y-loop [(y 0)] + (printf "~a~%" (vector-ref (matrix-data matrix) y)) + (when (< y (- (matrix-height matrix) 1)) + (y-loop (+ y 1))))) + ; parse 0-9 numerical data from file and return a matrix (define (load-matrix file) (let y-loop [(heightmap '())]