123456789101112131415161718192021222324252627282930313233 |
- (define-module (coords-operations)
- #:export
- (index->coords
- coords->index))
- (use-modules
- ((coords-model) #:prefix cm:))
- (define (index->coords index row-count col-count)
- #;(display (simple-format #f "index->coords ~a ~a ~a\n"
- index row-count col-count))
- (cond [(>= index (* row-count col-count))
- (throw 'index-error
- (simple-format #f "expected index < ~a" (* row-count col-count))
- index row-count col-count)]
- [(not (and (exact-integer? index)
- (>= index 0)))
- (throw 'index-error
- "expected index to be exact positive integer or exact integer 0"
- index)]
- [(let* ([row-coord (floor (/ index col-count))]
- [col-coord (- index (* row-coord col-count))])
- (cm:make-coords row-coord col-coord))]))
- (define (coords->index row-coord col-coord
- row-count col-count)
- (cond [(or (< row-coord 0)
- (< col-coord 0)
- (>= row-coord row-count)
- (>= col-coord col-count))
- (throw 'index-error "index out of bounds" row-coord col-coord row-count col-count)]
- [else (+ (* row-coord col-count) col-coord)]))
|