+(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))))