;;; File: ;;; polygons.scm ;;; Author: ;;; Samuel A. Rebelsky ;;; Version: ;;; 0.1 of 2 December 2009 ;;; Summary: ;;; A collection of procedures to explore drawing with polygons. ;;; Contents: ;;; (clear! image) ;;; Clear an image ;;; (ngon-vertices n x y r) ;;; Build the vertices for an n-sided polygon of radius r, ;;; centerd at (x,y) ;;; (spingon! image levels sides x y r angle ;;; Procedure: ;;; clear! ;;; Purpose: ;;; Clear an image (define clear! (lambda (image) (image-select-all! image) (image-clear-selection! image) (image-select-nothing! image) (context-update-displays!))) ;;; Procedure: ;;; ngon-vertices ;;; Parameters: ;;; n, a real number ;;; x, a real number ;;; y, a real number ;;; r, a positive real number ;;; theta, a real number ;;; Purpose: ;;; Create a list of the vertices of a regular n-gon centered ;;; at (x,y) and with radius r, with the first vertex at an ;;; angle of theta from the center. ;;; Produces: ;;; verticies, a list of positions (define ngon-vertices (lambda (n x y r theta) (let (; Compute the vertex for a particular angle (vertex (lambda (angle) (position-new (+ x (* r (cos angle))) (+ y (* r (sin angle)))))) ; One unit of rotation for the vertices (unit (/ (* 2 pi) n))) (let kernel ((remaining n) (angle theta)) (if (zero? remaining) null (cons (vertex angle) (kernel (- remaining 1) (+ angle unit)))))))) ;;; Procedure: ;;; polygon! ;;; Parameters: ;;; image, an image ;;; sides, a positive integer ;;; x, a real ;;; y, a real ;;; r, a real ;;; angle, a real ;;; Purpose: ;;; Draw a sides-sided polygon of radius r, centered at (x,y), with the ;;; first point at angle from the horizontal. ;;; Produces: ;;; [Nothing, called for the side effect. (define polygon! (lambda (image sides x y r angle) (image-select-polygon! image REPLACE (ngon-vertices sides x y r angle)) (when (image-has-selection? image) (image-stroke! image)) (image-select-nothing! image))) ;;; Procedure: ;;; spingon! ;;; Parameters: ;;; image, an image ;;; levels, an integer ;;; x, a real ;;; y, a real ;;; angle, a real ;;; scale, a real ;;; theta, a real ;;; Purpose: ;;; Produce a "spingon", a spun polygon that gets smaller and smaller ;;; and ... There are "levels" polygons drawin, each with sides sides. ;;; Subsequent polygons are scaled by scale and rotated by theta. (define spingon! (lambda (image levels sides x y r angle scale theta) (when (> levels 0) (polygon! image sides x y r angle) (spingon! image (- levels 1) sides x y (* r scale) (+ angle theta) scale theta)))) ;;; Procedure: ;;; spin-hex! ;;; Parameters: ;;; image, an image ;;; levels, a non-negative integer ;;; x, a real ;;; y, a real ;;; r, a positive real ;;; Purpose: ;;; Draws a spun hexagon with "levels" hexagons drawn. ;;; Produces: ;;; Nothing; called for the side effect. (define spin-hex! (lambda (image levels x y r) (spingon! image levels 6 x y r (/ pi 6) .9 (/ pi 16)))) ;;; Procedure: ;;; spin-hex-row! ;;; Parameters: ;;; image, an image ;;; copies, an integer ;;; levels, a non-negative integer ;;; x, a real ;;; y, a real ;;; r, a real ;;; Purpose: ;;; Produces a row of copies copies of a spun hexagon. ;;; Produces: ;;; [Nothing, called for the side effect.] (define spin-hex-row! (lambda (image copies levels x y r) (when (> copies 0) (spin-hex! image levels x y r) (spin-hex-row! image (- copies 1) levels (+ x (* 2 r (cos (/ pi 6)))) y r)))) ;;; Build a cols-by-rows sheet of spingons (define spin-hex-sheet! (lambda (image cols rows levels x y r) (when (> rows 0) (spin-hex-row! image cols levels x y r) (spin-hex-row! image cols levels (+ x (* r (cos (/ pi 6)))) (+ y r (* r (sin (/ pi 6)))) r) (spin-hex-sheet! image cols (- rows 2) levels x (+ y r (* 4 r (sin (/ pi 6)))) r))))