+ (cons
+ (min (car i1) (car i2))
+ (max (cdr i1) (cdr i2))))
+
+(define-public (interval-intersection i1 i2)
+ (cons
+ (max (car i1) (car i2))
+ (min (cdr i1) (cdr i2))))
+
+(define-public (interval-sane? i)
+ (not (or (nan? (car i))
+ (inf? (car i))
+ (nan? (cdr i))
+ (inf? (cdr i))
+ (> (car i) (cdr i)))))
+
+(define-public (add-point interval p)
+ (cons (min (interval-start interval) p)
+ (max (interval-end interval) p)))
+
+(define-public (reverse-interval iv)
+ (cons (cdr iv) (car iv)))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; coordinates
+
+(define coord-x car)
+(define coord-y cdr)
+
+(define (coord-operation operator operand coordinate)
+ (if (pair? operand)
+ (cons (operator (coord-x operand) (coord-x coordinate))
+ (operator (coord-y operand) (coord-y coordinate)))
+ (cons (operator operand (coord-x coordinate))
+ (operator operand (coord-y coordinate)))))
+
+(define (coord-apply function coordinate)
+ (if (pair? function)
+ (cons
+ ((coord-x function) (coord-x coordinate))
+ ((coord-y function) (coord-y coordinate)))
+ (cons
+ (function (coord-x coordinate))
+ (function (coord-y coordinate)))))
+
+(define-public (coord-translate coordinate amount)
+ (coord-operation + amount coordinate))
+
+(define-public (coord-scale coordinate amount)
+ (coord-operation * amount coordinate))
+
+(define-public (coord-rotate coordinate degrees-in-radians)
+ (let*
+ ((coordinate
+ (cons
+ (exact->inexact (coord-x coordinate))
+ (exact->inexact (coord-y coordinate))))
+ (radius
+ (sqrt
+ (+ (* (coord-x coordinate) (coord-x coordinate))
+ (* (coord-y coordinate) (coord-y coordinate)))))
+ (angle (angle-0-2pi (atan (coord-y coordinate) (coord-x coordinate)))))
+ (cons
+ (* radius (cos (+ angle degrees-in-radians)))
+ (* radius (sin (+ angle degrees-in-radians))))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; trig
+
+(define-public PI (* 4 (atan 1)))
+
+(define-public TWO-PI (* 2 PI))
+
+(define-public PI-OVER-TWO (/ PI 2))
+
+(define-public THREE-PI-OVER-TWO (* 3 PI-OVER-TWO))
+
+(define-public (cyclic-base-value value cycle)
+ "Take @var{value} and modulo-maps it between 0 and base @var{cycle}."
+ (if (< value 0)
+ (cyclic-base-value (+ value cycle) cycle)
+ (if (>= value cycle)
+ (cyclic-base-value (- value cycle) cycle)
+ value)))
+
+(define-public (angle-0-2pi angle)
+ "Take @var{angle} (in radians) and maps it between 0 and 2pi."
+ (cyclic-base-value angle TWO-PI))
+
+(define-public (angle-0-360 angle)
+ "Take @var{angle} (in degrees) and maps it between 0 and 360 degrees."
+ (cyclic-base-value angle 360.0))
+
+(define-public PI-OVER-180 (/ PI 180))
+
+(define-public (degrees->radians angle-degrees)
+ "Convert the given angle from degrees to radians."
+ (* angle-degrees PI-OVER-180))
+
+(define-public (ellipse-radius x-radius y-radius angle)
+ (/
+ (* x-radius y-radius)
+ (sqrt
+ (+ (* (expt y-radius 2)
+ (* (cos angle) (cos angle)))
+ (* (expt x-radius 2)
+ (* (sin angle) (sin angle)))))))
+
+(define-public (polar->rectangular radius angle-in-degrees)
+ "Return polar coordinates (@var{radius}, @var{angle-in-degrees})
+as rectangular coordinates @ode{(x-length . y-length)}."
+
+ (let ((complex (make-polar
+ radius
+ (degrees->radians angle-in-degrees))))
+ (cons
+ (real-part complex)
+ (imag-part complex))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; string
+
+(define-public (string-endswith s suffix)
+ (equal? suffix (substring s
+ (max 0 (- (string-length s) (string-length suffix)))
+ (string-length s))))
+
+(define-public (string-startswith s prefix)
+ (equal? prefix (substring s 0 (min (string-length s) (string-length prefix)))))
+
+(define-public (string-encode-integer i)
+ (cond
+ ((= i 0) "o")
+ ((< i 0) (string-append "n" (string-encode-integer (- i))))
+ (else (string-append
+ (make-string 1 (integer->char (+ 65 (modulo i 26))))
+ (string-encode-integer (quotient i 26))))))
+
+(define (number->octal-string x)
+ (let* ((n (inexact->exact x))
+ (n64 (quotient n 64))
+ (n8 (quotient (- n (* n64 64)) 8)))
+ (string-append
+ (number->string n64)
+ (number->string n8)
+ (number->string (remainder (- n (+ (* n64 64) (* n8 8))) 8)))))
+
+(define-public (ly:inexact->string x radix)
+ (let ((n (inexact->exact x)))
+ (number->string n radix)))
+
+(define-public (ly:number-pair->string c)
+ (string-append (ly:number->string (car c)) " "
+ (ly:number->string (cdr c))))
+
+(define-public (dir-basename file . rest)
+ "Strip suffixes in @var{rest}, but leave directory component for
+@var{file}."
+ (define (inverse-basename x y) (basename y x))
+ (simple-format #f "~a/~a" (dirname file)
+ (fold inverse-basename file rest)))