Add Licensing and Contributing
[clnl] / src / main / clnl / nvm / topology.lisp
1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3.  See distributed LICENSE.txt.
2 (in-package #:clnl-nvm)
3
4 (defun wrap (pos min max)
5  (cond
6   ((>= pos max) (+ min (mod (- pos max) (- max min))))
7   ((< pos min)
8    (let
9     ((res (- max (mod (- min pos) (- max min)))))
10     (if (< res max) res min))) ; If d is infinitesimal, may return max, which would be bad :(
11   (t pos)))
12
13 (defgeneric wrap-x (topology x))
14 (defgeneric wrap-y (topology y))
15
16 ; Torus implementations
17 (defmethod wrap-x ((topology (eql :torus)) x)
18  (wrap x (- (min-pxcor) 0.5d0) (+ (max-pxcor) 0.5d0)))
19
20 (defmethod wrap-y ((topology (eql :torus)) y)
21  (wrap y (- (min-pycor) 0.5d0) (+ (max-pycor) 0.5d0)))