#lang racket
(require gigls/unsafe)

;;; File:
;;;   pollockj-wolterzo-noycechr.image-series.rkt
;;; Authors:
;;;   Wesley Pollock
;;;   Zoe Wolter
;;;   Christain Noyce
;;; Summary:
;;;   Final project for CSC151.02 with Sam Rebelsky Fall 2013

; +--------------------+------------------------------------------------------
; | Polygon Procedures |
; +--------------------+

;;; Edited from code written for Assignment 5: Producing Playful Polygons (Toby Baratta and Wes Pollock)
;;; Procedure
;;;   turtle-polygon!
;;; Parameters
;;;   turtle, a turtle
;;;   side-length, a positive real number
;;;   sides, a positive integer
;;; Purpose
;;;   Creates a polygon of specified sides and side-length
;;; Produces
;;;   nothing
;;; Preconditions
;;;   To see result:
;;;   The turtle must be on the image at positive (x, y)
;;;   If the turtle is on the edge of image it must be facing toward the body of the image
;;; Postconditions
;;;   The top left corner of the polygon will be at the turtle's initial postion
;;;   The turtle returns to its initial position

(define turtle-polygon!
  (lambda (turtle side-length sides)
    (repeat sides
            (lambda ()
              (turtle-forward! turtle side-length)
              (turtle-turn! turtle (/ 360 sides))))))

;;; Code given in Assignment 5: Producing Playful Polygons
;;; Edited from documentation written by Toby Baratta and Wes Pollock
;;; Procedure:
;;;   turtle-centered-polygon!
;;; Parameters:
;;;   turtle, a turtle
;;;   radius, a real number
;;;   sides, a positive integer
;;; Purpose:
;;;   To create a centered polygon a specific radius and number of sides.
;;; Produces:
;;;   nothing
;;; Preconditions:
;;;   In order for the polygon to be completely visible on the image the turtle's
;;;   initial position must be (2xradius, radius)
;;; Postconditions:
;;;   Returns turtle to initial position:
;;;   such that (turtle ':angle), (turtle ':row), and (turtle ':col) before procedure
;;;   run is equal to (turtle ':angle), (turtle ':row), and (turtle ':col) after procedure run.
;;;   All sides of the polygon are equal in length
;;;   Every interior angle are equal.
;;;   The radii to each corner are equal. 

(define turtle-centered-polygon!
  (lambda (turtle radius sides)
    (let ([interior-angle (/ (* 180 (- sides 2)) sides)])
      (turtle-up! turtle)
      (turtle-forward! turtle radius)
      (turtle-down! turtle)
      (turtle-turn! turtle (- 180 (/ interior-angle 2)))
      (turtle-polygon! turtle (* 2 radius (sin (/ pi sides))) sides)
      (turtle-turn! turtle (/ interior-angle 2))
      (turtle-up! turtle)
      (turtle-forward! turtle radius)
      (turtle-turn! turtle 180)
      (turtle-down! turtle))))

;;; Procedure
;;;   turtle-changing-size-polygons!
;;; Parameters
;;;   turtle, a turtle
;;;   radius, a positive real number
;;;   delta-radius, a real number
;;;   sides, a positive integer
;;;   col1, a real number
;;;   row1, a real number
;;;   col2, a real number
;;;   row2, a real number
;;;   distance, a real number
;;; Purpose
;;;   Creates a line of polygons
;;; Produces
;;;   nothing
;;; Pre-conditions
;;;   To see the result of the procedure:
;;;   (or (and (< 0 col1 image-width) (< 0 row1 image-height))
;;;       (and (< 0 col2 image-width) (< 0 row2 image-height)))
;;; Post-conditions
;;;   The first polygon has a radius equal to radius the next polygon's radius decreases by delta-radius
;;;   All polygons have sides equal to number-of-sides.
;;;   The distance between the center of the polygons is distance
;;;   The first polygon is at (co1, row1), and the last is distance away from (col2, row2)

(define turtle-changing-size-polygons!
  (lambda (turtle radius delta-radius sides col1 row1 col2 row2 distance)
    (let* ([angle (atan (if (= row1 row2)
                            0
                            (/ (- row1 row2) (- col2 col1))))]
           [total-distance (sqrt (+ (square (- row1 row2)) (square (- col2 col1))))]
           [number-of-polygons (round (/ total-distance distance))])
      (for-each
       (lambda (n radius)
         (turtle-teleport! turtle  (+ col1 (* (* n distance) (cos angle))) (- row1 (* (* n distance) (sin angle))))
         (turtle-centered-polygon! turtle radius sides))
       (list-drop (iota (+ 1 number-of-polygons)) 1)
       (map (l-s + radius) (map (l-s * delta-radius) (iota number-of-polygons)))))))

; +----------------------------------+-----------------------------------------
; | Single-Spiral-Polygon Procedures |
; +----------------------------------+

;;; Procedure
;;;   turtle-spiral-changing-size-polygons!
;;; Parameters
;;;   turtle, a turtle
;;;   spacing, a positive real number
;;;   delta-angle, a real number
;;;   radius, a positive real number
;;;   delta-radius, a real number
;;;   sides, a positive integer
;;;   number-of-polygons, a positive integer
;;; Purpose
;;;   Creates a spiral of polygons with changing size
;;; Produces
;;;   nothing
;;; Pre-conditions
;;;   To see the result:
;;;   The turtle must be on the image at positive (x, y)
;;;   If the turtle is on the edge of image it must be facing toward the body of the image
;;; Post-conditions
;;;   The number of polygons is equal to number-of-polygons
;;;   The first polygon has a radius equal to radius the next polygon's radius decreases by delta-radius
;;;   All polygons have sides equal to number-of-sides.
;;;   The distance between the center of the polygons is spacing
;;;   The first polygon is at the turtle's position
;;;   Each "side" of the spiral is delta-angle more angled than the last.

(define turtle-spiral-changing-size-polygons!
  (lambda (turtle spacing delta-angle radius delta-radius sides number-of-polygons)
    (for-each (lambda (angle radius)
                (turtle-up! turtle)
                (turtle-forward! turtle spacing)
                (turtle-turn! turtle angle)
                (turtle-down! turtle)
                (turtle-centered-polygon! turtle radius sides))
              (map (l-s * delta-angle) (iota number-of-polygons))
              (map (l-s + radius) (map (l-s * delta-radius) (iota number-of-polygons))))))

;;; Procedure
;;;   turtle-spiral-changing-size-and-color-polygons!
;;; Parameters
;;;   turtle, a turtle
;;;   spacing, a positive real number
;;;   delta-angle, a real number
;;;   radius, a positive real number
;;;   delta-radius, a real number
;;;   sides, a positive integer
;;;   number-of-polygons, a positive integer
;;;   rgb1, a rgb color
;;;   rgb2, a rgb color
;;; Purpose
;;;   Creates a spiral of polygons with changing size and color
;;; Produces
;;;   nothing
;;; Pre-conditions
;;;   To see the result:
;;;   The turtle must be on the image at positive (x, y)
;;;   If the turtle is on the edge of image it must be facing toward the body of the image
;;; Post-conditions
;;;   The number of polygons is equal to number-of-polygons
;;;   The first polygon has a radius equal to radius the next polygon's radius decreases by delta-radius
;;;   All polygons have sides equal to number-of-sides.
;;;   The distance between the center of the polygons is spacing
;;;   The first polygon is at the turtle's position
;;;   Each "side" of the spiral is delta-angle more angled than the last.
;;;   The colors of the polygons blend from color1 to color2 in steps equal to number-of-polygons

(define turtle-spiral-changing-size-and-color-polygons!
  (lambda (turtle spacing delta-angle radius delta-radius sides number-of-polygons rgb1 rgb2)
    (for-each (lambda (angle radius color)
                (turtle-up! turtle)
                (turtle-forward! turtle spacing)
                (turtle-turn! turtle angle)
                (turtle-down! turtle)
                (turtle-set-color! turtle color)
                (turtle-centered-polygon! turtle radius sides))
              (map (l-s * delta-angle) (iota number-of-polygons))
              (map (l-s + radius) (map (l-s * delta-radius) (iota number-of-polygons)))
              (list-of-blends rgb1 rgb2 (- number-of-polygons 1)))))

; +----------------------------------+-------------------------------------------
; | Circle-Spiral-Polygon Procedures |
; +----------------------------------+

;;; Procedure
;;;   turtle-spiral-circle-changing-size-polygons!
;;; Parameters
;;;   turtle, a turtle
;;;   spacing, a positive real number
;;;   number-of-polygons, a positive integer
;;;   delta-angle, a real number
;;;   radius, a positive real number
;;;   delta-radius, a real number
;;;   sides, a positive integer
;;;   center-col, a real number
;;;   center-row, a real number
;;;   repeats, a positive integer
;;; Purpose
;;;   Creates multiple spirals of polygons with changing radii. Each spiral is angled equally from the last.
;;; Produces
;;;   nothing
;;; Pre-conditions
;;;   To see the result:
;;;   (and (<= 0 col1 image-width) (<= 0 row1 image-height))
;;; Post-conditions
;;;   The center of the spiral circle is at (center-col, center-row)
;;;   The angle of a "arm" of the spiral circle is (/ 360 repeats) more than the last
;;;   In each "arm":
;;;    The number of polygons is equal to number-of-polygons
;;;    The first polygon has a radius equal to radius the next polygon's radius decreases by delta-radius
;;;    All polygons have sides equal to number-of-sides.
;;;    The distance between the center of the polygons is spacing
;;;    The first polygon is at the turtle's position
;;;    Each "side" of the spiral is delta-angle more angled than the last.

(define turtle-spiral-circle-changing-size-polygons!
  (lambda (turtle spacing number-of-polygons delta-angle radius delta-radius sides center-col center-row repeats)
    (for-each
     (lambda (facing-angle)
       (turtle-teleport! turtle center-col center-row)
       (turtle-face! turtle facing-angle)
       (turtle-spiral-changing-size-polygons! turtle spacing delta-angle radius delta-radius sides number-of-polygons))
     (map (l-s * (round (/ 360 repeats))) (iota repeats)))))

;;; Procedure
;;;   turtle-spiral-circle-changing-size-and-color-polygons!
;;; Parameters
;;;   turtle, a turtle
;;;   spacing, a positive real number
;;;   number-of-polygons, a positive integer
;;;   delta-angle, a real number
;;;   radius, a positive real number
;;;   delta-radius, a real number
;;;   sides, a positive integer
;;;   center-col, a real number
;;;   center-row, a real number
;;;   repeats, a positive integer
;;;   rgb1, a rgb color
;;;   rgb2, a rgb color
;;; Purpose
;;;   Creates multiple spirals of polygons with changing radii and colors. Each spiral is angled equally from the last.
;;; Produces
;;;   nothing
;;; Pre-conditions
;;;  To see the result:
;;;   (and (<= 0 col1 image-width) (<= 0 row1 image-height))
;;; Post-conditions
;;;   The center of the spiral circle is at (center-col, center-row)
;;;   The angle of a "arm" of the spiral circle is (/ 360 repeats) more than the last
;;;   In each "arm":
;;;    The number of polygons is equal to number-of-polygons
;;;    The first polygon has a radius equal to radius the next polygon's radius decreases by delta-radius
;;;    All polygons have sides equal to number-of-sides.
;;;    The distance between the center of the polygons is spacing
;;;    The first polygon is at the turtle's position
;;;    Each "side" of the spiral is delta-angle more angled than the last.
;;;    The colors of the polygons blend from color1 to color2 in steps equal to number-of-polygons

(define turtle-spiral-circle-changing-size-and-color-polygons!
  (lambda (turtle spacing number-of-polygons delta-angle radius delta-radius sides center-col center-row repeats rgb1 rgb2)
    (for-each
     (lambda (facing-angle)
       (turtle-teleport! turtle center-col center-row)
       (turtle-face! turtle facing-angle)
       (turtle-spiral-changing-size-and-color-polygons! turtle spacing delta-angle radius delta-radius sides number-of-polygons rgb1 rgb2))
     (map (l-s * (round (/ 360 repeats))) (iota repeats)))))

;;; Procedure
;;;   turtle-spiral-circle-changing-size-and-spin-polygons!
;;; Parameters
;;;   turtle, a turtle
;;;   spacing, a positive real number
;;;   number-of-polygons, a positive integer
;;;   delta-angle, a real number
;;;   radius, a positive real number
;;;   delta-radius, a real number
;;;   sides, a positive integer
;;;   center-col, a real number
;;;   center-row, a real number
;;;   repeats, a positive integer
;;; Purpose
;;;   Creates multiple spirals of polygons with changing radii and different spin directions. 
;;; Produces
;;;   nothing
;;; Pre-conditions
;;;   To see the result:
;;;   (and (<= 0 col1 image-width) (<= 0 row1 image-height))
;;; Post-conditions
;;;   The center of the spiral circle is at (center-col, center-row)
;;;   The angle of a "arm" of the spiral circle is (/ 360 repeats) more than the last
;;;   Half of the "arms" spiral on one direction while the other half spiral in the other direction.
;;;   Each spiral is angled equally from the last.
;;;   In each "arm":
;;;    The number of polygons is equal to number-of-polygons
;;;    The first polygon has a radius equal to radius the next polygon's radius decreases by delta-radius
;;;    All polygons have sides equal to number-of-sides.
;;;    The distance between the center of the polygons is spacing
;;;    The first polygon is at the turtle's position
;;;    Each "side" of the spiral is delta-angle more angled than the last. 

(define turtle-spiral-circle-changing-size-and-spin-polygons!
  (lambda (turtle spacing number-of-polygons delta-angle radius delta-radius sides center-col center-row repeats)
    (for-each
     (lambda (facing-angle delta-angle)
       (turtle-teleport! turtle center-col center-row)
       (turtle-face! turtle facing-angle)
       (turtle-spiral-changing-size-polygons! turtle spacing delta-angle radius delta-radius sides number-of-polygons))
     (map (l-s * (round (/ 360 repeats))) (iota repeats))
     (if (even? repeats)
         (append (make-list (/ repeats 2) delta-angle) (make-list (/ repeats 2) (* -1 delta-angle)))
         (append (make-list (floor (/ repeats 2)) delta-angle) (list 0) (make-list (floor (/ repeats 2)) (* -1 delta-angle)))))))

; +----------------------------+-----------------------------------------------------
; | Perspective-grid Procedure |
; +----------------------------+

;;; Procedure
;;;   image-draw-one-point-perspective-grid!
;;; Parameters
;;;   image, an image
;;;   left, a real number
;;;   bottom, a real number
;;;   point-col, a real number
;;;   point-row, a real number
;;;   y-spacing, a positive real number
;;;   x-spacing a positive real number
;;;   x-number-of-lines, a positive integer
;;; Purpose
;;;   Creates a perspective grid, with the bottom left corner at (left, bottom), to a vanishing point at (point-col, point-row)
;;; Produces
;;;   nothing
;;; Pre-conditions
;;;   (<= left point-col)
;;;   (> bottom point-row)
;;; Post-conditions
;;;   The grid goes has (= y-number-of-lines (ceiling (/ (- bottom point-row) y-spacing)))
;;;   The right most bottom point is at ((+ left (* (- x-number-of-lines 1) x-spacing)), bottom)
;;;   Each x-line is x-spacing apart from the next
;;;   Each y-line is y-spacing apart from the next

(define image-draw-one-point-perspective-grid!
  (lambda (image left bottom point-col point-row y-spacing x-spacing x-number-of-lines)
    (let* ([y-number-of-lines (ceiling (/ (- bottom point-row) y-spacing))]
           [slope-left (if (= point-col left)
                           0
                           (/ (- bottom point-row) (- left point-col)))])
      (context-set-brush! "2. Hardness 100" 3)
      ;Vertical lines
      (for-each
       (lambda (col1)
         (image-draw-line! image
                           col1
                           bottom
                           point-col
                           point-row
                           ))
       (map (l-s + left)
            (map (l-s * x-spacing) (iota x-number-of-lines))))
      ;Horizontal lines
      (for-each
       (lambda (n row)
         (let ([slope-left-line (if (= left point-col)
                                    0
                                    (/ (- point-row bottom) (- left point-col)))]
               [slope-right-line (if (= (+ left (* (- x-number-of-lines 1) x-spacing)) point-col)
                                     0
                                     (/ (- bottom point-row) (- point-col (+ left (* (- x-number-of-lines 1) x-spacing)))))])
           (image-draw-line! image
                             (if (= slope-left-line 0)
                                 left
                                 (+ left (/ n slope-left-line)))
                             row
                             (if (= slope-right-line 0)
                                 (+ (* (- x-number-of-lines 1) x-spacing) left)
                                 (+ (* (- x-number-of-lines 1) x-spacing) left (/ n slope-right-line)))
                             row)))
       (map (l-s * y-spacing) (iota y-number-of-lines))
       (map (l-s - bottom)
            (map (l-s * y-spacing)
                 (iota y-number-of-lines)))))))

; +-----------------------+----------------------------------------------------------
; | Color-blend Procedure |
; +-----------------------+

;;; Edited from code written for Assignment 4: Exploring Colors (Wesley Pollock, Anita DeWitt, Michael Hurley)
;;; Procedure
;;;   list-of-blends
;;; Parameters
;;;   color1, a rgb color
;;;   color2, a rgb color
;;;   steps, a positive integer
;;; Purpose
;;;   Creates a list of colors which blend from color1 to color2
;;; Produces
;;;   color-list, a list of rgb colors
;;; Preconditions
;;;   [no additional]
;;; Postconditions
;;;   (= (length (list-of-blends color1 color2 steps)) (+ 1 steps))
;;;   (map rgb? (length (list-of-blends color1 color2 steps)))
;;;   will return a list of #t the length of color-list

(define list-of-blends
  (lambda (color1 color2 steps)
    (map irgb-new
         ;red component
         (map +
              (map inexact->exact
                   (map floor
                        (map *
                             (map (l-s * (/ 1 steps)) (iota (+ 1 steps)))
                             (make-list (+ 1 steps) (rgb-red color2)))))
              (map inexact->exact
                   (map floor
                        (map *
                             (map (l-s * (/ 1 steps)) (reverse (iota (+ 1 steps))))
                             (make-list (+ 1 steps) (rgb-red color1))))))
         ;green component
         (map +
              (map inexact->exact
                   (map floor
                        (map *
                             (map (l-s * (/ 1 steps)) (iota (+ 1 steps)))
                             (make-list (+ 1 steps) (rgb-green color2)))))
              (map inexact->exact
                   (map floor
                        (map *
                             (map (l-s * (/ 1 steps)) (reverse (iota (+ 1 steps))))
                             (make-list (+ 1 steps) (rgb-green color1))))))
         ;blue component
         (map +
              (map inexact->exact
                   (map floor
                        (map *
                             (map (l-s * (/ 1 steps)) (iota (+ 1 steps)))
                             (make-list (+ 1 steps) (rgb-blue color2)))))
              (map inexact->exact
                   (map floor
                        (map *
                             (map (l-s * (/ 1 steps)) (reverse (iota (+ 1 steps))))
                             (make-list (+ 1 steps) (rgb-blue color1)))))))))

; +-------------------------+----------------------------------------------------------
; | Space-object Procedures |
; +-------------------------+


;;; Procedure
;;;  image-draw-blended-planet!
;;; Parameters
;;;  image, an image
;;;  rgb1, a rgb value
;;;  rgb2, a rgb-value
;;;  size, a positive real number
;;;  left, a positive real number
;;;  top, a positive real number
;;;  width, width of image
;;;  height, height of image
;;; Purpose
;;;  To create a series of ellipses on top of eachother that blend from
;;;  one color to another as they decrease in size.
;;; Produces
;;;  Nothing, called for side-effects.
;;; Pre-conditions
;;;  (0 < left < width)
;;;  (0 < top < height)
;;;  (.7 < size)
;;; Post-conditions
;;;  Each ellipse in the series will gradually get smaller.
;;;  The width of the largest ellipse will equal (* size width .1).
;;;  The height of the largest ellipse will equal (* size height .1).
;;;  The largest ellipse will be colored rgb1.
;;;  The smallest ellipse will be colored rgb2.
;;;  The ellipses in between the largest and smallest will be a blend of rgb1 and rgb2.
;;;  The largest ellipse in the series will be positioned at left, top.

(define image-draw-blended-planet!
  (lambda (image rgb1 rgb2 size left top width height)
    (let* ([planet-width (* size width .1)]
           [planet-height (* size height .1)]
           [left-off-set (/ (- planet-width (* planet-width (cos (* pi .25)))) 20)]
           [top-off-set (/ (- planet-height (* planet-height (sin (* pi .25)))) 20)])
      (for-each (lambda (rgb inner-width inner-height left top)
                  (when (and (> height top 0) (> width left 0))
                    (context-set-fgcolor! rgb)
                    (image-select-ellipse! image REPLACE left top inner-width inner-height)
                    (image-fill-selection! image)))
                (list-of-blends rgb1 rgb2 19)
                (map (r-s * .05) (map (r-s * planet-width) (reverse (map increment (iota 20)))))
                (map (r-s * .05) (map (r-s * planet-height) (reverse (map increment (iota 20)))))
                (map (r-s + left) (map (r-s * left-off-set) (iota 20)))
                (map (r-s + top) (map (r-s * top-off-set) (iota 20)))
                )
      (image-select-nothing! image))))

;;; Procedure
;;;  image-draw-comet-...-...!
;;; Parameters
;;;  image, an image
;;;  num, an integer
;;;  size, a positive real number
;;;  rgb1, a rgb value
;;;  rgb2, a rgb value
;;;  left, a positive real number
;;;  top, a positive real number
;;;  width, width of image
;;;  height, height of image
;;; Purpose
;;;  to create a series of ellipses that progressively decrease in size
;;;  and blend from rgb1 to rgb2 on an image
;;; Produces
;;;  nothing, called for side effects
;;; Pre-condtions
;;;  (0 < left < width)
;;;  (0 < top < height)
;;; Post-conditions
;;;  The number of ellipses in the series will equal num.
;;;  Each ellipse in the series will gradually get smaller.
;;;  The width of the largest ellipse will equal (size * width * .05).
;;;  The height of the largest ellipse will equal (size * height * .05).
;;;  The largest ellipse will be colored rgb1, the smallest ellipses will be colored rgb2.
;;;  The ellipsess in between the largest and smallest will be a blend of rgb1 and rgb2.
;;;  The largest ellipse in the series will be positioned at left, top.
;;;  (image-draw-comet-up-left!) produces ellipses that increase their horizontal
;;;   and vertical postions as they decrease in size.
;;;  (image-draw-comet-up-right!) produces ellipses that increase their horizontal
;;;   and decrease their vertical postions as they decrease in size.
;;;  (image-draw-comet-down-right!) produces ellipses that decrease their horizontal
;;;   and vertical postions as they decrease in size.
;;;  (image-draw-comet-down-left!) produces ellipses that decrease their horizontal
;;;   and increase their vertical postions as they decrease in size.

(define image-draw-comet-up-left!
  (lambda (image num size rgb1 rgb2 left top width height)
    (let* ([comet-width (* size width .05)]
           [comet-height (* size height .05)])
      (for-each (lambda (rgb left top step-width step-height)
                  (context-set-fgcolor! rgb)
                  (image-select-ellipse! image REPLACE left top step-width step-height)
                  (image-fill-selection! image))
                (list-of-blends rgb1 rgb2 (- num 1))
                (map (r-s + left) (map (r-s * (/ comet-width 10)) (iota num)))
                (map (r-s + top) (map (r-s * (/ comet-height 10)) (iota num)))
                (map ceiling (map (r-s * (/ 1 num)) (map (r-s * comet-width) (reverse (map increment (iota num))))))
                (map ceiling (map (r-s * (/ 1 num)) (map (r-s * comet-height) (reverse (map increment (iota num)))))))
      (image-select-nothing! image))))
(define image-draw-comet-up-right!
  (lambda (image num size rgb1 rgb2 left top width height)
    (let* ([comet-width (* size width .05)]
           [comet-height (* size height .05)])
      (for-each (lambda (rgb left top step-width step-height)
                  (context-set-fgcolor! rgb)
                  (image-select-ellipse! image REPLACE left top step-width step-height)
                  (image-fill-selection! image))
                (list-of-blends rgb1 rgb2 (- num 1))
                (map (r-s + left) (map (r-s * -1) (map (r-s * (/ comet-width 10)) (iota num))))
                (map (r-s + top) (map (r-s * (/ comet-height 10)) (iota num)))
                (map ceiling (map (r-s * (/ 1 num)) (map (r-s * comet-width) (reverse (map increment (iota num))))))
                (map ceiling (map (r-s * (/ 1 num)) (map (r-s * comet-height) (reverse (map increment (iota num))))))
                )(image-select-nothing! image))))
(define image-draw-comet-down-right!
  (lambda (image num size rgb1 rgb2 left top width height)
    (let* ([comet-width (* size width .05)]
           [comet-height (* size height .05)])
      (for-each (lambda (rgb left top step-width step-height)
                  (context-set-fgcolor! rgb)
                  (image-select-ellipse! image REPLACE left top step-width step-height)
                  (image-fill-selection! image))
                (list-of-blends rgb1 rgb2 (- num 1))
                (map (r-s + left) (map (r-s * -1) (map (r-s * (/ comet-width 10)) (iota num))))
                (map (r-s + top) (map (r-s * -1) (map (r-s * (/ comet-height 10)) (iota num))))
                (map ceiling (map (r-s * (/ 1 num)) (map (r-s * comet-width) (reverse (map increment (iota num))))))
                (map ceiling (map (r-s * (/ 1 num)) (map (r-s * comet-height) (reverse (map increment (iota num))))))
                )(image-select-nothing! image))))
(define image-draw-comet-down-left!
  (lambda (image num size rgb1 rgb2 left top width height)
    (let* ([comet-width (* size width .05)]
           [comet-height (* size height .05)])
      (for-each (lambda (rgb left top step-width step-height)
                  (context-set-fgcolor! rgb)
                  (image-select-ellipse! image REPLACE left top step-width step-height)
                  (image-fill-selection! image))
                (list-of-blends rgb1 rgb2 (- num 1))
                (map (r-s + left) (map (r-s * (/ comet-width 10)) (iota num)))
                (map (r-s + top) (map (r-s * -1) (map (r-s * (/ comet-height 10)) (iota num))))
                (map ceiling (map (r-s * (/ 1 num)) (map (r-s * comet-width) (reverse (map increment (iota num))))))
                (map ceiling (map (r-s * (/ 1 num)) (map (r-s * comet-height) (reverse (map increment (iota num))))))
                )(image-select-nothing! image))))

; +------------------------+----------------------------------------------------------
; | image-series Procedure |
; +------------------------+

;;; Procedure
;;;   image-series
;;; Parameters
;;;   n, a integer
;;;   width, a positive integer
;;;   height, a positive integer
;;; Produces
;;;   A different image for each value of n, with specified width and height
;;;   Images in each section of 100 n are similar to each other,
;;;   while images in different sections have exhibit greater differences
;;; Produces
;;;   image, a image
;;; Preconditions
;;;   (<= 0 n 999)
;;; Postconditions
;;;   Images have:
;;;    for (<= 0 n 99)
;;;     a perspective grid
;;;     a large circle
;;;     a black background
;;;     a line of polygons
;;;    for (<= 100 n 199)
;;;     a black circle
;;;     single colored background
;;;     multiple spirals of polygons
;;;    for (<= 200 n 299)
;;;     polygon spiral-circle
;;;     one to two color blended background
;;;     sometimes a perspective grid
;;;    for (<= 300 n 399)
;;;     polygon spiral-circle with changing color
;;;     two color blended background
;;;    for (<= 400 n 499)
;;;     polygon spiral-circle with changing spin direction
;;;     one to two color blended background
;;;     a blended "planet"
;;;    for (<= 500 n 599)
;;;     polygon spiral-circle with changing color
;;;     one to two blended background
;;;     a blended "planet"
;;;    for (<= 600 n 699)
;;;     one or more color blended background
;;;     two blended "planets"
;;;    for (<= 700 n 799)
;;;     one or more color blended background
;;;     a blended "planet"
;;;     a spiral of polygons with changing color
;;;    for (<= 800 n 899)
;;;     one or more color blended background
;;;     a blended "planet"
;;;     a polygon spiral-circle with changing color
;;;     a "comet"
;;;    for (<= 900 n 999)
;;;     one or more color blended background
;;;     two blended "planets"
;;;     a "comet"

(define image-series
  (let* ([light-colors (vector "whitesmoke"
                               "white"
                               "snow"
                               "oldlace"
                               "powderblue"
                               "snow"
                               "wheat"
                               "lightgrey"
                               "lightcyan"
                               "ivory"
                               "ghostwhite"
                               "blanchedalmond"
                               "cornsilk"
                               "aliceblue"
                               "antiquewhite"
                               "azure"
                               "beige"
                               )]
         [light-colors-length (vector-length light-colors)]
         [dark-colors (vector "blueviolet"
                              "darkmagenta"
                              "darkorchid"
                              "darkslateblue"
                              "darkslategray"
                              "darkslategrey"
                              "darkviolet"
                              "firebrick"
                              "purple"
                              "sienna"
                              "teal"
                              "blue"
                              "darkblue"
                              "darkred"
                              "indigo"
                              "maroon"
                              "mediumblue"
                              "midnightblue"
                              "navy")]
         [dark-colors-length (vector-length dark-colors)])
    (lambda (n width height)
      (let* ([tens-place (- (mod n 100) (mod n 10))]
             [scale-factor (/ (+ width height) (* 2 300))]
             [dark-color (vector-ref dark-colors (mod n dark-colors-length))]
             [light-color (vector-ref light-colors (mod n light-colors-length))]
             [background (image-compute
                          (lambda (col row)
                            (cond
                              [(<= 0 n 99)
                               ;creates a black background
                               (rgb-new 0 0 0)]
                              [(<= 100 n 199)
                               ;creates a solid color background
                               (rgb-new (mod (- n 100) 100) (mod (- n 120) 75) (mod (- n 50) 150))]
                              [(<= 200 n 399)
                               ;creates a gradient background going from black on the left
                               ;to another color on the right
                               (rgb-new (* col (mod n 3) (/ 42 width))
                                        0
                                        (* col (mod n 6) (/ 42 width)))]
                              [(<= 400 n 599)
                               (cond [(= 0 (mod n 2))
                                      ;creates a gradient background going from black on the left
                                      ;to another color on the right
                                      (rgb-new (* col (mod n 2) (/ 63 width))
                                               0
                                               (* col (mod n 5) (/ 51 width)))]
                                     [(= 1 (mod n 2))
                                      ;creates a gradient background going from black on the top
                                      ;to another color on the bottom
                                      (rgb-new (* row (mod n 3) (/ 42 height))
                                               0
                                               (* row (mod n 6) (/ 42 height)))])]
                              [(<= 600 n 699)
                               ;creates a gradient background going from black in the upper-left corner
                               ;to other colors depending on the column and row
                               (rgb-new (* row (mod n 3) (/ 42 height))
                                        0
                                        (* col (mod n 7) (/ 42 width)))]
                              [(<= 700 n 799)
                               ;creates a gradient background going from black in the upper-left corner
                               ;to another color in the lower-right corner, depending on the column and row
                               (rgb-new (* col row (mod n 4) (/ 240 width height 2.5))
                                        0
                                        (* col row (mod n 5)(/ 255 width height)))]         
                              [(<= 800 n 999)
                               ;creates a gradient background going from black in the upper-left corner
                               ;to another color in the lower-right corner, depending on the column and row
                               ;with various white pixels.
                               (if (or (and (= col (quotient width (+ 1 (mod (round (* n .5)) width)))) (= row (quotient height (+ 1 (mod 1 height)))))
                                       (and (= col (quotient width (+ 1 (mod 2 width)))) (= row (quotient height (+ 1 (mod 2 height)))))
                                       (and (= col (quotient width (+ 1 (mod (mod n 5) width)))) (= row (quotient height (+ 1 (mod (mod n 6) height)))))
                                       (and (= col (quotient width (+ 1 (mod 2 width)))) (= row (quotient height (+ 1 (mod 1 height)))))
                                       (and (= col (quotient width (+ 1 (mod 1 width)))) (= row (quotient height (+ 1 (mod 3 height)))))
                                       (and (= col (quotient width (+ 1 (mod (mod n 5) width)))) (= row (quotient height (+ 2 (mod 4 height)))))
                                       (and (= col (quotient width (+ 1 (mod (mod n 3) width)))) (= row (quotient height (+ 1 (mod (mod n 4) height)))))
                                       (and (= col (quotient width (+ 1 (mod (mod n 2) width)))) (= row (quotient height (mod 2 height))))
                                       (and (= col (round (/ width 1.1))) (= row (round (/ height 3))))
                                       (and (= col (round (/ width 5))) (= row (round (/ width 1.1)))))
                                   (rgb-new 255 255 255)
                                   (rgb-new (* col row (mod n 4) (/ 240 width height 2.5))
                                            0
                                            (+ 30 (* col row (mod n 5)(/ 255 width height)))))]))
                          width height)]
             [spiral-turtle1 (turtle-new background)])
        (cond
          ;Images from 0 to 99 n
          [(<= 0 n 99)
           ;adds a ghostwhite circle
           (context-set-fgcolor! "ghostwhite")
           (image-select-ellipse! background
                                  REPLACE
                                  ;left, from 4/5 to 4/7 of the image-width
                                  (* width (/ 4 (+ 5 (mod n 3))))
                                  ;top
                                  0
                                  ;width, from 3/5 to 3/7 of the image-width
                                  (* (/ 3 (+ 5 (mod n 3))) width)
                                  ;height, from 3/5 to 3/7 of the image-height
                                  (* (/ 3 (+ 5 (mod n 3))) height))
           (image-fill-selection! background)
           (image-select-nothing! background)
           (let (;Varies the left end of the grid to be within 3/4 and 3/6 of the image-width
                 [grid-left (* width (/ 3 (+ 4 (mod n 3))))]
                 ;Varies the column vanishing point of the grid to be within the width to 7/5 of the image-width
                 [grid-point-col (* (/ 7 (+ 5 (mod n 3))) width)]
                 ;Varies the offset between 3-30 times the scale-factor
                 [polygon-offset (* scale-factor 3 (+ 1 (mod n 10)))])
             ;Creates a white perspective grid
             (context-set-fgcolor! "white")
             (image-draw-one-point-perspective-grid! background   
                                                     grid-left
                                                     height
                                                     grid-point-col
                                                     0
                                                     ;y-spacing, 2/10 of the height
                                                     (* 2 (/ height 10))
                                                     ;x-spacing, 2/10 of the height
                                                     (* 2 (/ width 10))
                                                     ;x-number-of-lines, varies between 5 and 9 lines 
                                                     (+ 5 (mod n 10)))
             ;Creates a line of polygons decreasing in size
             (turtle-set-color! spiral-turtle1 "white")
             (turtle-changing-size-polygons! spiral-turtle1
                                             ;radius, from 10/3 to 199/3 times the scale-factor       
                                             (* scale-factor (/ (+ 10 (mod n 100)) 3))
                                             ;delta-radius, from -10/6 to -199/6 times the scale-factor
                                             (* scale-factor (/ (+ 10 (mod n 100)) -6))
                                             ;sides, from 3 to 5
                                             (+ (mod n 3) 3)
                                             (- grid-left polygon-offset)
                                             height
                                             (- grid-point-col polygon-offset)
                                             0
                                             (* scale-factor 60))
             (image-show background))]
          ;Images from 100 to 199 n
          [(<= 100 n 199)
           ;Creates a large black circle
           (image-select-ellipse! background
                                  REPLACE
                                  ;left, from 1/5 to 1/50 times the image-width
                                  (* width (/ 2 (+ 10 tens-place)))
                                  ;varies top between 1/3-5/6 times the image-height
                                  (* height (/ 5 (+ 6 (/ tens-place 10))))
                                  ;varies ellipse-width between 10-910 times the width-scale factor
                                  (* (/ width 300) (+ 10 (* 10 tens-place)) )
                                  ;varies ellipse-width between 10-90 times the height-scale factor
                                  (* (/ height 300)(+ 10 (* 10 tens-place))))
           (context-set-fgcolor! "black")
           (image-fill-selection! background)
           (image-select-nothing! background)
           (let ([;Makes 1-6 spirals
                  number-of-spirals (+ 1 (mod n 6))])
             ;Creates spirals of polygons
             (for-each
              (lambda (y angle spacing sides number-of-polygons)
                (turtle-teleport! spiral-turtle1
                                  ;teleports turtle to column that varies 1/100-1/10 times the width
                                  (/ width (+ 10 tens-place))
                                  y)
                (turtle-face! spiral-turtle1
                              angle)
                (turtle-set-color! spiral-turtle1
                                   light-color)
                (turtle-spiral-changing-size-polygons! spiral-turtle1
                                                       spacing
                                                       ;delta-angle, from 0 to 3 times the scale-factor
                                                       (* scale-factor (mod n 4))
                                                       ;radius, if the delta-radius is positive, 1-10 times the scale-factor
                                                       ;else between 1-19 times the scale-factor
                                                       (if (<= 0 (mod n 4) 1)
                                                           (* scale-factor (+ 1 (mod n 10)))
                                                           (* scale-factor (/ (+ 5 tens-place) 5)))
                                                       ;delta-radius, either positive or negative 1-19 divided by the number-of-spirals 
                                                       (if (<= 0 (mod n 4) 1)
                                                           (* scale-factor (/ (/ (+ 5 tens-place) 5) number-of-spirals))
                                                           (* scale-factor (/ (/ (+ 5 tens-place) -5) number-of-spirals)))
                                                       sides
                                                       number-of-polygons))
              ;y, a list of values number-of-spirals long, ranging from (15-19.5) times the scale-factor and the number-of-spirals
              (map (l-s * (* scale-factor (+ 15 (/ tens-place 20)))) (list-drop (iota (+ 1 number-of-spirals)) 1))
              ;angle, a list of angles number-of-spirals long, ranging from (5-9) times the number-of-spirals
              (map (l-s * (+ 5 (mod n 5))) (list-drop (iota (+ 1 number-of-spirals)) 1))
              ;spacing,  a list of values number-of-spirals long, ranging from (1-100) times the scale-factor and the number-of-spirals
              (map (l-s * (* scale-factor (+ 1 (mod n 100)))) (list-drop (iota (+ 1 number-of-spirals)) 1))
              ;sides, a list of values number-of-spirals long, ranging from (1-5) plus the number-of-spirals
              (map (l-s + (+ 1 (mod n 5))) (list-drop (iota (+ 1 number-of-spirals)) 1))
              ;number-of-polygons, a list of values number-of-spirals long, ranging from (5-1) plus the number-of-spirals
              (reverse (map (l-s + (+ 1 (mod n 5))) (list-drop (iota (+ 1 number-of-spirals)) 1)))))
           (image-show background)]
          ;Images from 200 to 299 n
          [(<= 200 n 299)
           (context-set-fgcolor! dark-color)
           ;adds a perspective grid when the radius of the polygons is increasing
           (when (<= 0 (mod n 4) 1)
             (image-draw-one-point-perspective-grid! background
                                                     0
                                                     height
                                                     ;point-col, the column of the center of the polygon spirals
                                                     (* width (/ 4 (+ 5 (mod n 4))))
                                                     ;point-row, the row of the center of the polygon spirals
                                                     (* height (/ 1 (+ 7 (mod n 3))))
                                                     ;y-spacing, 2/10 of the height 
                                                     (* 2 (/ height 10))
                                                     ;x-spacing, 2/10 of the width
                                                     (* 2 (/ width 10))
                                                     ;x-number-of-lines, ranges from 10-55
                                                     (- (+ 10 tens-place) (/ tens-place 2))))
           (let (;makes 5-10 polygons
                 [number-of-polygons (+ 5 (mod n 6))])
             ;adds a light-colored spiral of polygons in circle with varying sizes
             (turtle-set-color! spiral-turtle1 light-color)
             (turtle-spiral-circle-changing-size-polygons! spiral-turtle1
                                                           ;spacing: from 10 to 149/5 times the scale-factor  
                                                           (* scale-factor (+ 10 (/ (mod n 100) 5)))
                                                           number-of-polygons
                                                           ;delta-angle, from 1 to 4 degrees
                                                           (+ 1 (mod n 4))
                                                           ;radius, if the delta-radius is positive, 1-10 times the scale-factor
                                                           ;else between 10-28 times the scale-factor
                                                           (if (<= 0 (mod n 4) 1)
                                                               (* scale-factor (+ 1 (mod n 10)))
                                                               (* scale-factor (+ 10 (/ tens-place 5))))
                                                           ;delta-radius, either from 10 to 28 or from -65/2 to -10 divided by the number-of-spirals
                                                           (if (<= 0 (mod n 4) 1)
                                                               (* scale-factor (/ (+ 10 (/ tens-place 5)) number-of-polygons))
                                                               (* scale-factor (/ (* -1 (+ 10 (/ tens-place 4))) number-of-polygons)))
                                                           ;sides, from 1 to 5 sides
                                                           (+ 1 (mod n 5))
                                                           ;center-col, from 4/5 to 4/7 of the width
                                                           (* width (/ 4 (+ 5 (mod n 4))))
                                                           ;center-row, from 1/7 to 1/9 of the height
                                                           (* height (/ 1 (+ 7 (mod n 3))))
                                                           ;repeats, 2 to 6 times
                                                           (+ 2 (mod n 5))
                                                           ))
           (image-show background)]
          ;Images from 300 to 399 n
          [(<= 300 n 399)
           ;adds spirals of polygons in a circular pattern with varying sizes and colors
           (turtle-spiral-circle-changing-size-and-color-polygons! spiral-turtle1
                                                                   ;spacing: from 10 to 37 times the scale-factor    
                                                                   (* scale-factor (+ 10 (/ tens-place 5) (mod n 10)))
                                                                   ;number-of-polygons, from 4 to 9 polygons
                                                                   (+ 4 (mod n 6))
                                                                   ;delta-angle, from 2 to 4 degrees
                                                                   (+ 2 (mod n 3))
                                                                   ;radius, if the delta-radius is positive, 2-6 times the scale-factor
                                                                   ;else between 10-40 times the scale-factor 
                                                                   (if (<= 0 (mod n 4) 1)
                                                                       (* scale-factor (+ 2 (mod n 5)))
                                                                       (* scale-factor (+ 10 (/ tens-place 3))))
                                                                   ;delta-radius, either from 1 to 4 or from -7 to -1 degrees
                                                                   (if (<= 0 (mod n 4) 1)
                                                                       (* scale-factor (+ 1 (mod n 4)))
                                                                       (* -1 (* scale-factor (+ 1 (mod n 7)))))
                                                                   ;sides, from 3 to 6 sides
                                                                   (+ 3 (mod n 4))
                                                                   ;center-col, from 1/5 to 1/8 of the width
                                                                   (* width (/ 1 (+ 5 (mod n 4))))
                                                                   ;center-row, from 6/7 to 6/9 of the height
                                                                   (* height (/ 6 (+ 7 (mod n 3))))
                                                                   ;repeats, from 3 to 6 times
                                                                   (+ 3 (mod n 4))
                                                                   ;color1, a color from the dark-colors vector
                                                                   (color->rgb dark-color)
                                                                   ;color2, a color from the light-colors vector
                                                                   (color->rgb light-color))
           (image-show background)]
          ;Images from 400 to 499 n
          [(<= 400 n 499)
           ;adds a white perspective grid
           (context-set-fgcolor! "white")
           (image-draw-one-point-perspective-grid! background
                                                   ;left, from -1/10 to -1/7 of the width
                                                   (* -1 width (/ 1 (+ 7 (mod n 4))))
                                                   height
                                                   ;point-col, from 7/6 to 10/6 of the width
                                                   (* width (/ (+ 7 (mod n 4)) 6))
                                                   (/ height 2)
                                                   (* 2 (/ height 30))
                                                   (* 2 (/ width 10))
                                                   ;x-number-of-lines, from 7 to 11 lines
                                                   (+ 7 (mod n 5)))
           ;adds a "planet"
           (image-draw-blended-planet! background
                                       ;rgb-a, a color in the light-colors vector
                                       (color->rgb light-color)
                                       ;rgb-b, a color in the dark-colors vector
                                       (color->rgb dark-color)
                                       ;size varies between 2-6
                                       (+ 2 (mod n 5))
                                       ;left varies between 1/5-2/7 times the image-width
                                       (* width (/ 2 (+ 7 (mod n 4))))
                                       ;top varies between 2/3-6/7 times the image-height
                                       (* height (/ 6 (+ 7 (mod n 3))))
                                       width
                                       height)
           ;adds dark colored spirals of polygons in a circular pattern with varying sizes and spin directions
           (turtle-set-color! spiral-turtle1 dark-color)
           (turtle-spiral-circle-changing-size-and-spin-polygons! spiral-turtle1
                                                                  ;spacing, from 10 to 49 times the scale-factor    
                                                                  (* scale-factor (+ 10 (/ tens-place 3) (mod n 10)))
                                                                  ;number-of-polygons, from 5 to 9 polygons
                                                                  (+ 5 (mod n 5))
                                                                  ;delta-angle, from 3 to 6 degrees
                                                                  (+ 3 (mod n 4))
                                                                  ;radius, if the delta-radius is positive, 5-12 times the scale-factor
                                                                  ;else between 10-65/2 times the scale-factor 
                                                                  (if (<= 0 (mod n 4) 1)
                                                                      (* scale-factor (+ 5 (mod n 8)))
                                                                      (* scale-factor (+ 10 (/ tens-place 4))))
                                                                  ;delta-radius, either from 1 to 4 or from -7 to -1 degrees
                                                                  (if (<= 0 (mod n 4) 1)
                                                                      (* scale-factor (+ 1 (mod n 4)))
                                                                      (* -1 (* scale-factor (+ (mod n 7) 1))))
                                                                  ;sides, from 3 to 6 sides
                                                                  (+ 3 (mod n 4))
                                                                  ;center-col, from 6/7 to 6/10 times the width
                                                                  (* width (/ 6 (+ 7 (mod n 4))))
                                                                  ;center-row, from 1/5 to 1/8 times the height
                                                                  (* height (/ 1 (+ 5 (mod n 3))))
                                                                  ;repeats, 2, 4, or 6 times
                                                                  (* 2 (+ 1 (mod n 3))))
           (image-show background)]
          ;Images from 500 to 599 n
          [(<= 500 n 599)
           ;adds a "planet"
           (image-draw-blended-planet! background
                                       ;rgb-a, the same color as the background at top and left
                                       (image-get-pixel background (* width (/ 4 (+ 5 (mod n 3)))) (* height (/ 1 (+ 5 (mod n 3)))))
                                       ;rgb-b, a color in the light-color vector
                                       (color->rgb light-color)
                                       ;size, varies between 18-19
                                       (+ 18 (mod n 2))
                                       ;left varies between 4/7-4/5 times the image-width
                                       (* width (/ 4 (+ 5 (mod n 3))))
                                       ;top varies between 1/7-1/5 times the image-height
                                       (* height (/ 1 (+ 5 (mod n 3))))
                                       width
                                       height)
           ;adds spirals of polygons in a circular pattern with varying sizes and colors
           (turtle-spiral-circle-changing-size-and-color-polygons! spiral-turtle1
                                                                   ;spacing, from 10 to 49 times the scale-factor    
                                                                   (* scale-factor (+ 10 (/ tens-place 3) (mod n 10)))
                                                                   ;number-of-polygons, from 6 to 11 polygons
                                                                   (+ 6 (mod n 6))
                                                                   ;delta-angle, from 3 to 6 degrees
                                                                   (+ 3 (mod n 3))
                                                                   ;radius, if the delta-radius is positive, 5-12 times the scale-factor
                                                                   ;else between 10-65/2 times the scale-factor
                                                                   (if (<= 0 (mod n 4) 1)
                                                                       (* scale-factor (+ 5 (mod n 8)))
                                                                       (* scale-factor (+ 10 (/ tens-place 4))))
                                                                   ;delta-radius, either from 1 to 4 or from -18 to -3 degrees
                                                                   (if (<= 0 (mod n 4) 1)
                                                                       (* scale-factor (+ 1 (mod n 4)))
                                                                       (* -1 (* scale-factor (+ 3 (/ tens-place 6)))))
                                                                   ;sides, from 3 to 6 sides
                                                                   (+ 3 (mod n 3))
                                                                   ;center-col, from 1/7 to 1/10 times the width
                                                                   (* width (/ 1 (+ 7 (mod n 4))))
                                                                   ;center-row, from 1/5 to 1/7 times the height
                                                                   (* height (/ 1 (+ 5 (mod n 3))))
                                                                   ;repeats, from 5 to 7 times
                                                                   (+ 5 (mod n 4))
                                                                   ;color1, a color from the dark-colors vector
                                                                   (color->rgb dark-color)
                                                                   ;color2, a color from the light-colors vector
                                                                   (color->rgb light-color))
           (image-show background)]
          ;Images from 600 to 699 n
          [(<= 600 n 699)
           ;When n is a multiple of 21: adds a dark gray perspective grid  
           (when (= 0 (mod n 21))
             (context-set-fgcolor! (rgb-new 20 20 20))
             (image-draw-one-point-perspective-grid! background
                                                     ;left, from -1/6 to -1/9 of the width
                                                     (* -1 width (/ 1 (+ 6 (mod n 4))))
                                                     height
                                                     ;point-col, from 7/6 to 10/6 of the width
                                                     (* width (/ (+ 7 (mod n 4)) 6))
                                                     (/ height 2)
                                                     (* 2 (/ height 30))
                                                     (* 2 (/ width 10))
                                                     ;x-number-of-lines, from 7 to 11 lines
                                                     (+ 7 (mod n 5))))
           ;adds a "planet" to the background
           (image-draw-blended-planet! background
                                       ;rgb-a, the same color as the background at top and left
                                       (image-get-pixel background (quotient width (+ 2 (mod n 3))) (quotient height (+ 2 (mod n 4))))
                                       ;rgb-b,
                                       ;varies red component between 0-67
                                       ;varies green component between 0-67
                                       ;varies blue component between 20-147
                                       (rgb-new (mod (- n 100) 67) (mod n 67) (+ 20 (mod n 127)))
                                       ;size, varies between 10-13
                                       (+ 10 (mod n 4))
                                       ;left, varies between 1/4-1/2 times the image-width
                                       (quotient width (+ 2 (mod n 3)))
                                       ;top, varies between 1/5-/2 the image-height
                                       (quotient height (+ 2 (mod n 4)))
                                       width height)
           ;adds another "planet" to the background
           (image-draw-blended-planet! background
                                       ;rgb-a
                                       ;varies red component between 0-255
                                       ;varies green component between 30-157
                                       (rgb-new (mod n 255) (+ 30 (mod n 127)) 0)
                                       ;rgb-b, a color in the light-color vector
                                       (color->rgb light-color)
                                       ;size varies between 2-4
                                       (+ 2 (mod n 3))
                                       ;left varies between 5/8-5/6 times the image-width
                                       (* width (/ 5 (+ 6 (mod n 3))))
                                       ;top varies between 1/5-1/2 times the image height
                                       (quotient height (+ 2 (mod n 4)))
                                       width 
                                       height)
           (image-show background)]
          ;Images from 700 to 799 n
          [(<= 700 n 799)
           ;adds a "planet" to the background
           (image-draw-blended-planet! background
                                       ;rgb-a, a color in the light-colors vector
                                       (color->rgb light-color)
                                       ;rgb-red and rgb-green vary between 0-126 (a shade of yellow)
                                       (rgb-new (mod n 127) 0 (mod n 127))
                                       ;size varies between 6-9
                                       (+ 6 (mod n 4))
                                       ;left varies between 1/4-1/2 times the image-width
                                       (quotient width (+ 2 (mod n 3)))
                                       ;top varies between 1/5-1/2 times the image-height
                                       (quotient height (+ 2 (mod n 4)))
                                       width height)
           (let (;From 6/7 to 6/9 times the width
                 [turtle-col (* width (/ 6 (+ 7 (mod n 3))))]
                 ;From 1/7 to 1/9 of the height
                 [turtle-row (* height (/ 1 (+ 7 (mod n 3))))])
             ;adds a single spiral of polygons with varying size and color at (turtle-col, turtle-row)
             (turtle-teleport! spiral-turtle1 turtle-col turtle-row)
             (turtle-face! spiral-turtle1 90)
             (turtle-spiral-changing-size-and-color-polygons! spiral-turtle1
                                                              ;spacing, from 35 to 39 times the scale-factor )
                                                              (* scale-factor (quotient n 20))
                                                              5
                                                              ;radius, from 3 to 27 times the scale-factor
                                                              (* scale-factor (* 3 (mod n 10)))
                                                              ;delta-radius, from -3 to -1 times the scale-factor
                                                              (* -1 scale-factor (mod n 4))
                                                              ;sides
                                                              (+ 3 (mod n 3))
                                                              16
                                                              ;rgb1, the color of the background at (turtle-col, turtle-row)
                                                              (image-get-pixel background turtle-col turtle-row)
                                                              ;rgb2, a color from the dark-colors vector
                                                              (color->rgb dark-color)))
           (image-show background)]
          ;Images from 800 to 899 n
          [(<= 800 n 899)
           ;adds spirals of polygons in a circular pattern with varying sizes and colors
           (turtle-spiral-circle-changing-size-and-color-polygons! spiral-turtle1
                                                                   ;spacing, from 10 to 19 times the scale-factor
                                                                   (* scale-factor (+ 10 (mod n 10)))
                                                                   ;number-of-polygons, from 5 to 9 polygons
                                                                   (+ 5 (mod n 5))
                                                                   ;delta-angle, from 3 to 5 degrees
                                                                   (+ 3 (mod n 3))
                                                                   ;radius, if the delta-radius is positive, 5-7 times the scale-factor
                                                                   ;else between 10-19 times the scale-factor
                                                                   (if (<= 0 (mod n 4) 1)
                                                                       (* scale-factor (+ 5 (mod n 3)))
                                                                       (* scale-factor (+ 10 (mod n 10))))
                                                                   ;delta-radius, either from 1 to 4 or from -7 to -1 degrees
                                                                   (if (<= 0 (mod n 4) 1)
                                                                       (* scale-factor (+ 1 (mod n 4)))
                                                                       (* -1 (* scale-factor (+ (mod n 7) 1))))
                                                                   ;sides, from 3 to 5 sides
                                                                   (+ 3 (mod n 3))
                                                                   ;center-col, from 6/7 to 6/10 times the width
                                                                   (* width (/ 6 (+ 7 (mod n 4))))
                                                                   ;center-row, from 1/5 to 1/7 times the height
                                                                   (* height (/ 1 (+ 5 (mod n 3))))
                                                                   ;repeats, from 3 to 6 times
                                                                   (+ 3 (mod n 4))
                                                                   ;rgb1, the color of the background at (center-col, center-row)
                                                                   (image-get-pixel background
                                                                                    (* width (/ 6 (+ 7 (mod n 4))))
                                                                                    (* height (/ 1 (+ 5 (mod n 3)))))
                                                                   ;rgb2, a color from the light-colors vector
                                                                   (color->rgb light-color))
           ;adds a "planet" to the background
           (image-draw-blended-planet! background
                                       ;rgb-a, rgb-red varies between 0-254
                                       ;       rgb-green varies between 30-156
                                       ;       rgb-blue is at 0
                                       (rgb-new (mod n 255) (+ 30 (mod n 127)) 0)
                                       ;rgb-b, a color in the light-colors vector
                                       (color->rgb light-color)
                                       ;size varies between 4-7
                                       (+ 4 (mod n 4))
                                       ;left varies between 1/4-1/2 times the image-width
                                       (quotient width (+ 2 (mod n 3)))
                                       ;top varies between 1/5-1/2 times the image height
                                       (quotient height (+ 2 (mod n 4)))
                                       width height)
           ;adds a "comet" to the background
           (let ([rgb-b
                  ;an rgb-value taken from the background at left top
                  (image-get-pixel background
                                   (quotient width 6)
                                   (quotient height (+ 2 (mod n 3))))]
                 [rgb-a
                  ;varies red component between 214-255
                  (rgb-new (- 255 (mod n 42))
                           0
                           0)])
             ;creates a comet going in one of four different directions
             (cond [(= 0 (mod n 4))
                    (image-draw-comet-up-left! background
                                               ;number of ellipses = (* 10 size)
                                               (* 10 (+ 1 (mod n 3)))
                                               ;size varies between 1-3
                                               (+ 1 (mod n 3))
                                               rgb-a
                                               rgb-b
                                               ;comet starts in the left half of the image
                                               (quotient width 6)
                                               ;comet starts in the upper half of the image
                                               (quotient height (+ 2 (mod n 3)))
                                               width height)]
                   [(= 1 (mod n 4))
                    (image-draw-comet-up-right! background
                                                ;number of ellipses = (* 10 size)
                                                (* 10 (+ 1 (mod n 3)))
                                                ;size varies between 1-3
                                                (+ 1 (mod n 3))
                                                rgb-a
                                                rgb-b
                                                ;comet starts in the right half of the image
                                                (+ (quotient width 2) (quotient width (+ 3 (mod n 5))))
                                                ;comet starts in the upper half of the image
                                                (quotient height (+ 2 (mod n 3)))
                                                width height)]
                   [(= 2 (mod n 4))
                    (image-draw-comet-down-left! background
                                                 ;number of ellipses = (* 10 size)
                                                 (* 10 (+ 1 (mod n 3)))
                                                 ;size varies between 1-3
                                                 (+ 1 (mod n 3))
                                                 rgb-a
                                                 rgb-b
                                                 ;comet starts in the left half of the image
                                                 (quotient width 6)
                                                 ;comet starts in the lower half of the image
                                                 (+ (quotient height 2) (quotient height (+ 3 (mod n 5))))
                                                 width height)]
                   [(= 3 (mod n 4))
                    (image-draw-comet-down-right! background
                                                  ;number of ellipses = (* 10 size)
                                                  (* 10 (+ 1 (mod n 3)))
                                                  ;size varies between 1-3
                                                  (+ 1 (mod n 3))
                                                  rgb-a
                                                  rgb-b
                                                  ;comet starts in the right half of the image
                                                  (+ (quotient width 2) (quotient width (+ 3 (mod n 5))))
                                                  ;comet starts in the lower half of the image
                                                  (+ (quotient height 2) (quotient height (+ 3 (mod n 5))))
                                                  width height)]))
           (image-show background)]
          ;Images from 900 to 999 n
          [(<= 900 n 999)
           ;adds a "planet" to the background
           (image-draw-blended-planet! background
                                       ;rgb-a, the color at the (left, top) of the background 
                                       (image-get-pixel background
                                                        (* width (/ 1 (+ 7 (mod n 3))))
                                                        (* height (/ 1 (+ 9 (mod n 4)))))
                                       ;rgb-b, a color in the dark-colors vector
                                       (color->rgb dark-color)
                                       ;size varies between 20-23
                                       (+ 20 (mod n 4))
                                       ;left varies between 1/9-1/7 times the image-width
                                       (* width (/ 1 (+ 7 (mod n 3))))
                                       ;top varies between 1/12-1/9 times the image-height
                                       (* height (/ 1 (+ 9 (mod n 4))))
                                       width height)
           ;adds a "comets" to the background
           (let ([rgb-c
                  ;varies red component between 201-230
                  ;varies green component between 1-100
                  (rgb-new (- 230 (mod n 30))
                           (- 100 (mod n 100))
                           0)]
                 [rgb-d
                  ;varies red component between 159-200
                  ;varies green component between 159-200
                  (rgb-new (- 200 (mod n 42))
                           (- 200 (mod n 42))
                           0)]
                 [num2
                  ;number of circles = (10 * size)
                  (* 10 (+ 1 (mod n 2)))]
                 [size2
                  ;size varies between 1-2
                  (+ 1 (mod n 2))])
             (cond [(= 1 (mod n 4))
                    (image-draw-comet-up-left! background
                                               num2
                                               size2
                                               rgb-c
                                               rgb-d
                                               (quotient width 6)
                                               (quotient height (+ 2 (mod n 3)))
                                               width height)]
                   [(= 2 (mod n 4))
                    (image-draw-comet-up-right! background
                                                num2
                                                size2
                                                rgb-c
                                                rgb-d
                                                (+ (quotient width 2) (quotient width (+ 3 (mod n 5))))
                                                (quotient height (+ 2 (mod n 3)))
                                                width height)]
                   [(= 3 (mod n 4))
                    (image-draw-comet-down-left! background
                                                 num2
                                                 size2
                                                 rgb-c
                                                 rgb-d
                                                 (quotient width 6)
                                                 (+ (quotient height 2) (quotient height (+ 3 (mod n 5))))
                                                 width height)]
                   [(= 0 (mod n 4))
                    (image-draw-comet-down-right! background
                                                  num2
                                                  size2
                                                  rgb-c
                                                  rgb-d
                                                  (+ (quotient width 2) (quotient width (+ 3 (mod n 5))))
                                                  (+ (quotient height 2) (quotient height (+ 3 (mod n 5))))
                                                  width height)]))
           (image-draw-blended-planet! background
                                       ;rgb-a, rgb-red varies between 0-254
                                       ;       rgb-green varies between 30-156
                                       ;       rgb-blue is at 0
                                       (rgb-new (mod n 255) (+ 30 (mod n 127)) 0)
                                       ;rgb-b, a color in the light-colors vector
                                       (color->rgb light-color)
                                       ;size varies between 1-4
                                       (+ 1 (mod n 4))
                                       ;left varies between 1/4-1/2 times the image-width
                                       (quotient width (+ 2 (mod n 3)))
                                       ;top varies between 1/5-1/2 times the image-height
                                       (quotient height (+ 2 (mod n 4)))
                                       width height)
           (image-show background)])))))