;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname 12.4.1) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp")))))
;A list-of-posns is either
;1. an empty list or
;2. (cons p lop) where p is a posn
;and lop is a list-of-posns.
;A polygon is either
;1. (cons p empty) where p is a posn or
;2. (cons p q) where p is a posn and q is a polygon.
;(That is, a polygon includes all the elements
;of a list-of-posns except the empty list.)
;
;draw-polygon : polygon -> true
;Given a-poly (a polygon) which
;represents the vertices of the polygon, draw this polygon
;by connecting the dots between the posn elements
;in the list. It first connects the last posn to
;the first posn, then sequentially connects the
;remaining posns to each other. It extracts
;the last posn of a-poly by calling on the last
;function. Returns true when evaluation completes.
(define (draw-polygon a-poly)
(cond
[(empty? (rest a-poly)) true]
[else (connect-dots (cons (last a-poly) a-poly))]))
;
;connect-dots : polygon -> boolean
;Given a-poly, connects the posns in sequential order.
;IE, the 1st posn connects to the 2nd, the 2nd to the 3rd,
;and so forth to the last dot.
(define (connect-dots a-poly)
(cond
[(empty? (rest a-poly)) true]
[else (and
(draw-solid-line (first a-poly)
(second a-poly)
'black)
(connect-dots (rest a-poly)))]))
;Test
;(define MRPOLY (cons (make-posn 50 50) (cons (make-posn 50 80) (cons (make-posn 100 80) empty))))
;(connect-dots MRPOLY)
;
;last : polygon -> posn
;Given a-poly, finds the last posn element
;within the list-of-posns and returns it.
(define (last a-poly)
(cond
[(empty? (rest a-poly)) (first a-poly)]
[else (last (rest a-poly))]))
;Test
(define MRPOLY (cons (make-posn 50 50) (cons (make-posn 50 80) (cons (make-posn 100 80) (cons (make-posn 140 230) (cons (make-posn 180 330) (cons (make-posn 240 220) (cons (make-posn 130 220) empty))))))))
;add-at-end : polygon posn -> polygon
;Given a-poly and first-posn,
;it adds first-posn to
;the end of a-poly, returning
;the new polygon. When add-at-end is called,
;first-posn should be replaced with
;(first a-poly), so that the function call should be
;(add-at-end a-poly (first a-poly))
(define (add-at-end a-poly first-posn)
(cond
[(empty? (rest a-poly)) (first a-poly)]
[else (cons (add-at-end a-poly first-posn) (cons first-posn empty))]))
;
;modified-draw-polygon : polygon -> true
;Draws polygon specified by a-poly, where
;each posn in a-poly represents a vertex in
;the polygon. It does so by using connect-dots
;to connect the dots of each vertex.
;Specifically, it connects the 1st dot to the 2nd,
;the 2nd to the 3rd, and so forth, and finally
;connects the last dot to the 1st dot to finish
;the polygon by calling on add-at-end.
(define (modified-draw-polygon a-poly)
(cond
[(empty? (rest a-poly)) true]
[else (connect-dots (add-at-end a-poly))]))