+(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 angle-in-radians)
+ ;; getting around (sin PI) not being exactly zero by switching to cos at
+ ;; appropiate angles and/or taking the negative value (vice versa for cos)
+ (let* ((quadrant (inexact->exact (round (/ angle-in-radians (/ PI 2)))))
+ (moved-angle (- angle-in-radians (* quadrant (/ PI 2))))
+ (s (sin moved-angle))
+ (c (cos moved-angle))
+ (x (coord-x coordinate))
+ (y (coord-y coordinate)))
+ (case (modulo quadrant 4)
+ ((0) ;; -45 .. 45
+ (cons (- (* c x) (* s y))
+ (+ (* s x) (* c y))))
+ ((1) ;; 45 .. 135
+ (cons (- (* (- s) x) (* c y))
+ (+ (* c x) (* (- s) y))))
+ ((2) ;; 135 .. 225
+ (cons (- (* (- c) x) (* (- s) y))
+ (+ (* (- s) x) (* (- c) y))))
+ ((3) ;; 225 .. 315
+ (cons (- (* s x) (* (- c) y))
+ (+ (* (- c) x) (* s y))))
+ ;; for other angles (modulo quadrant 4) returns one of the above cases
+ )))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 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}."
+ (cond ((< value 0)
+ (cyclic-base-value (+ value cycle) cycle))
+ ((>= value cycle)
+ (cyclic-base-value (- value cycle) cycle))
+ (else 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))))
+