Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; 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.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")))))
4 ;A list-of-posns is either
5 ;1. an empty list or
6 ;2. (cons p lop) where p is a posn
7 ;and lop is a list-of-posns.
9 ;A polygon is either
10 ;1. (cons p empty) where p is a posn or
11 ;2. (cons p q) where p is a posn and q is a polygon.
12 ;(That is, a polygon includes all the elements
13 ;of a list-of-posns except the empty list.)
14 ;
15 ;draw-polygon : polygon -> true
16 ;Given a-poly (a polygon) which
17 ;represents the vertices of the polygon, draw this polygon
18 ;by connecting the dots between the posn elements
19 ;in the list. It first connects the last posn to
20 ;the first posn, then sequentially connects the
21 ;remaining posns to each other. It extracts
22 ;the last posn of a-poly by calling on the last
23 ;function. Returns true when evaluation completes.
25 (define (draw-polygon a-poly)
26 (cond
27 [(empty? (rest a-poly)) true]
28 [else (connect-dots (cons (last a-poly) a-poly))]))
29 ;
30 ;connect-dots : polygon -> boolean
31 ;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 (cond
37 [(empty? (rest a-poly)) true]
38 [else (and
39 (draw-solid-line (first a-poly)
40 (second a-poly)
41 'black)
42 (connect-dots (rest a-poly)))]))
44 ;Test
45 ;(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 -> posn
49 ;Given a-poly, finds the last posn element
50 ;within the list-of-posns and returns it.
52 (define (last a-poly)
53 (cond
54 [(empty? (rest a-poly)) (first a-poly)]
55 [else (last (rest a-poly))]))
57 ;Test
58 (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 -> polygon
61 ;Given a-poly and first-posn,
62 ;it adds first-posn to
63 ;the end of a-poly, returning
64 ;the new polygon. When add-at-end is called,
65 ;first-posn should be replaced with
66 ;(first a-poly), so that the function call should be
67 ;(add-at-end a-poly (first a-poly))
69 (define (add-at-end a-poly first-posn)
70 (cond
71 [(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 -> true
75 ;Draws polygon specified by a-poly, where
76 ;each posn in a-poly represents a vertex in
77 ;the polygon. It does so by using connect-dots
78 ;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 finally
81 ;connects the last dot to the 1st dot to finish
82 ;the polygon by calling on add-at-end.
84 (define (modified-draw-polygon a-poly)
85 (cond
86 [(empty? (rest a-poly)) true]
87 [else (connect-dots (add-at-end a-poly))]))