factorized heightmap code into matrix code, and using records now

This commit is contained in:
2021-12-11 10:30:24 +01:00
parent 3df3a70754
commit a1266ca648
2 changed files with 40 additions and 47 deletions

View File

@@ -12,3 +12,29 @@
(define (char->number c)
(- (char->integer c) 48)) ; 48 is ASCII's number zero
(define-record-type matrix (fields width height data))
(define (matrix-get matrix x y)
(if (or (< x 0)
(< y 0)
(>= x (matrix-width matrix))
(>= y (matrix-height matrix)))
9
(vector-ref (vector-ref (matrix-data matrix) y) x)))
(define (matrix-from-data data)
(make-matrix
(vector-length (vector-ref data 0))
(vector-length data)
data))
; parse 0-9 numerical data from file and return a matrix
(define (load-matrix file)
(let y-loop [(heightmap '())]
(let x-loop [(line '())]
(let [(c (get-char file))]
(cond
[(eof-object? c)
(matrix-from-data (reverse-list->vector heightmap))]
[(char-whitespace? c)
(y-loop (cons (reverse-list->vector line) heightmap))]
[(char-numeric? c)
(x-loop (cons (char->number c) line))])))))