;; draw-spider consumes a posn, a number, and 3 colors (symbols)
;;
;; (draw-spider position body-radius body-color head-color legs-color)
;;   will draw a sipder with a body centered at the x,y coordinates specified
;;   by the posn structure position, with a radius of body-radius, and of color
;;   body-color, with a head above the body of color head-color, and 8 legs
;;   proportional to the body of color legs-color.
;;
;; sample usage:
;; (draw-spider (make-posn 100 100) 30 'black 'red 'black)
;;
;; note that this requires the teachpack draw.ss and requires that the start
;; function has already been called to create a window

(define (draw-spider position body-radius body-color head-color legs-color)
  (and
   (draw-legs position (posn-x position) (posn-y position) (* body-radius 2) legs-color)
   (draw-solid-disk (make-posn (posn-x position) (- (posn-y position) body-radius))
                    (floor (* body-radius .3)) head-color)
   (draw-solid-disk position body-radius body-color)
   )
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; All functions beyond this point are helper functions for the main function ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (draw-legs position x y radius color)
  (and
   (draw-solid-line position (make-posn (- x radius) (- y radius)) color)
   (draw-left-foot (- x radius) (- y radius) radius color)
   (draw-solid-line position (make-posn (- x (floor (* radius 1.3))) (- y (floor (* radius .4)))) color)
   (draw-left-foot (- x (floor (* radius 1.3))) (- y (floor (* radius .4))) radius color)
   (draw-solid-line position (make-posn (- x (floor (* radius 1.3))) (+ y (floor (* radius .4)))) color)
   (draw-left-foot (- x (floor (* radius 1.3))) (+ y (floor (* radius .4))) radius color)
   (draw-solid-line position (make-posn (- x radius) (+ y radius)) color)
   (draw-left-foot (- x radius) (+ y radius) radius color)

   (draw-solid-line position (make-posn (+ x radius) (- y radius)) color)
   (draw-right-foot (+ x radius) (- y radius) radius color)
   (draw-solid-line position (make-posn (+ x (floor (* radius 1.3))) (- y (floor (* radius .4)))) color)
   (draw-right-foot (+ x (floor (* radius 1.3))) (- y (floor (* radius .4))) radius color)
   (draw-solid-line position (make-posn (+ x (floor (* radius 1.3))) (+ y (floor (* radius .4)))) color)
   (draw-right-foot (+ x (floor (* radius 1.3))) (+ y (floor (* radius .4))) radius color)
   (draw-solid-line position (make-posn (+ x radius) (+ y radius)) color)
   (draw-right-foot (+ x radius) (+ y radius) radius color)
   )
  )

(define (draw-left-foot x y radius color)
  (draw-solid-line (make-posn x y) (make-posn (- x (floor (* radius .1))) (+ y (floor (* radius .4)))) color))

(define (draw-right-foot x y radius color)
  (draw-solid-line (make-posn x y) (make-posn (+ x (floor (* radius .1))) (+ y (floor (* radius .4)))) color))