## Blob

1 ;; The first three lines of this file were inserted by DrScheme. They record metadata2 ;; about the language level of this file in a form that our tools can easily process.3 #reader(lib "htdp-beginner-reader.ss" "lang")((modname 12.3.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")))))4 ;A list-of-posns is either5 ;1. an empty list or6 ;2. (cons p lop) where p is a posn7 ;and lop is a list-of-posns.9 ;A polygon is either10 ;1. (cons p empty) where p is a posn or11 ;2. (cons p q) where p is a posn and q is a polygon.12 ;(That is, a polygon includes all the elements13 ;of a list-of-posns except the empty list.)14 ;15 ;draw-polygon : polygon -> true16 ;Given a-poly (a polygon) which17 ;represents the vertices of the polygon, draw this polygon18 ;by connecting the dots between the posn elements19 ;in the list. It first connects the last posn to20 ;the first posn, then sequentially connects the21 ;remaining posns to each other. It extracts22 ;the last posn of a-poly by calling on the last23 ;function. Returns true when evaluation completes.25 (define (draw-polygon a-poly)26 (cond27 [(empty? (rest a-poly)) true]28 [else (connect-dots (cons (last a-poly) a-poly))]))29 ;30 ;connect-dots : polygon -> boolean31 ;Given a-poly, connects the posns in sequential order.32 ;IE, the 1st posn connects to the 2nd, the 2nd to the 3rd,33 ;and so forth to the last dot.35 (define (connect-dots a-poly)36 (cond37 [(empty? (rest a-poly)) true]38 [else (and39 (draw-solid-line (first a-poly)40 (second a-poly)41 'black)42 (connect-dots (rest a-poly)))]))44 ;Test45 ;(define MRPOLY (cons (make-posn 50 50) (cons (make-posn 50 80) (cons (make-posn 100 80) empty))))46 ;(connect-dots MRPOLY)47 ;48 ;last : polygon -> posn49 ;Given a-poly, finds the last posn element50 ;within the list-of-posns and returns it.52 (define (last a-poly)53 (cond54 [(empty? (rest a-poly)) (first a-poly)]55 [else (last (rest a-poly))]))57 ;Test58 (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))))))))60 ;add-at-end : polygon posn -> polygon61 ;Given a-poly and first-posn,62 ;it adds first-posn to63 ;the end of a-poly, returning64 ;the new polygon. When add-at-end is called,65 ;first-posn should be replaced with66 ;(first a-poly), so that the function call should be67 ;(add-at-end a-poly (first a-poly))69 (define (add-at-end a-poly first-posn)70 (cond71 [(empty? (rest a-poly)) (first a-poly)]72 [else (cons (add-at-end a-poly first-posn) (cons first-posn empty))]))73 ;74 ;modified-draw-polygon : polygon -> true75 ;Draws polygon specified by a-poly, where76 ;each posn in a-poly represents a vertex in77 ;the polygon. It does so by using connect-dots78 ;to connect the dots of each vertex.79 ;Specifically, it connects the 1st dot to the 2nd,80 ;the 2nd to the 3rd, and so forth, and finally81 ;connects the last dot to the 1st dot to finish82 ;the polygon by calling on add-at-end.84 (define (modified-draw-polygon a-poly)85 (cond86 [(empty? (rest a-poly)) true]87 [else (connect-dots (add-at-end a-poly))]))