customize default return value of matrix-get when peeking outside
This commit is contained in:
23
common.scm
23
common.scm
@@ -13,19 +13,28 @@
|
||||
(- (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))
|
||||
|
||||
(define (matrix-get matrix x y default)
|
||||
(if (or (< x 0)
|
||||
(< y 0)
|
||||
(>= x (matrix-width matrix))
|
||||
(>= y (matrix-height matrix)))
|
||||
default
|
||||
(vector-ref (vector-ref (matrix-data matrix) y) x)))
|
||||
|
||||
(define (matrix-set! matrix x y value)
|
||||
(when (and (>= x 0)
|
||||
(>= y 0)
|
||||
(< x (matrix-width matrix))
|
||||
(< y (matrix-height matrix)))
|
||||
(vector-set! (vector-ref (matrix-data matrix) y) x value)))
|
||||
|
||||
; parse 0-9 numerical data from file and return a matrix
|
||||
(define (load-matrix file)
|
||||
(let y-loop [(heightmap '())]
|
||||
|
||||
Reference in New Issue
Block a user