(module (s9fes polygon) (;export circle-polygon-lines) (import scheme) (import (chicken base)) (import (chicken fixnum)) (import (chicken type)) (import (only (srfi 1) cons* append! reverse!)) (include-relative "s9fes.char-canvas.types") (: circle-polygon-lines (integer integer integer #!optional fixnum fixnum -> (list-of integer))) ;; ;constant but not immediate (define PI (acos -1)) (define DEGREE (/ PI 180)) (define-inline (virt->phys v) (inexact->exact (floor v))) (define-inline (phys-polar-x a r) (virt->phys (* (cos a) r))) (define-inline (phys-polar-y a r) (virt->phys (* (sin a) r))) ;circle-polygon-lines returns a list of coordinates suitable for use w/ ;{{canvas-plot-lines}}. The coordinates describe a closed, convex, polygon. ; ;-> (list-of integer integer ...) - (x0 y0 x1 y1 ...) ; ;@x, @y center ;@radius point distance from center ;@n point count; default 360 ;@angle starting degrees; default; {{0}} - has little influence ; (define (circle-polygon-lines x y radius #!optional (n 360) (angle 0)) (let ((ai (* (/ 360 n) DEGREE)) (ir (exact->inexact radius)) ) (let loop ((i 0) (a (* angle DEGREE)) (ps '())) (if (fx= i n) ;then ensure closed-polygon (let ((ps (reverse! ps))) (append! ps `(,(car ps) ,(cadr ps))) ) ;else more points (loop (fx+ i 1) (+ a ai) ;WTF insensitive to (+ next-y y) vs (+ next-y x) - square test canvas? (cons* (+ (phys-polar-y a ir) y) (+ (phys-polar-x a ir) x) ps)) ) ) ) ) ) ;module (s9fes polygon)