Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #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 12687dd9 2023-08-04 jrmu ;A list-of-posns is either
5 12687dd9 2023-08-04 jrmu ;1. an empty list or
6 12687dd9 2023-08-04 jrmu ;2. (cons p lop) where p is a posn
7 12687dd9 2023-08-04 jrmu ;and lop is a list-of-posns.
8 12687dd9 2023-08-04 jrmu
9 12687dd9 2023-08-04 jrmu ;A polygon is either
10 12687dd9 2023-08-04 jrmu ;1. (cons p empty) where p is a posn or
11 12687dd9 2023-08-04 jrmu ;2. (cons p q) where p is a posn and q is a polygon.
12 12687dd9 2023-08-04 jrmu ;(That is, a polygon includes all the elements
13 12687dd9 2023-08-04 jrmu ;of a list-of-posns except the empty list.)
14 12687dd9 2023-08-04 jrmu ;
15 12687dd9 2023-08-04 jrmu ;draw-polygon : polygon -> true
16 12687dd9 2023-08-04 jrmu ;Given a-poly (a polygon) which
17 12687dd9 2023-08-04 jrmu ;represents the vertices of the polygon, draw this polygon
18 12687dd9 2023-08-04 jrmu ;by connecting the dots between the posn elements
19 12687dd9 2023-08-04 jrmu ;in the list. It first connects the last posn to
20 12687dd9 2023-08-04 jrmu ;the first posn, then sequentially connects the
21 12687dd9 2023-08-04 jrmu ;remaining posns to each other. It extracts
22 12687dd9 2023-08-04 jrmu ;the last posn of a-poly by calling on the last
23 12687dd9 2023-08-04 jrmu ;function. Returns true when evaluation completes.
24 12687dd9 2023-08-04 jrmu
25 12687dd9 2023-08-04 jrmu (define (draw-polygon a-poly)
26 12687dd9 2023-08-04 jrmu (cond
27 12687dd9 2023-08-04 jrmu [(empty? (rest a-poly)) true]
28 12687dd9 2023-08-04 jrmu [else (connect-dots (cons (last a-poly) a-poly))]))
29 12687dd9 2023-08-04 jrmu ;
30 12687dd9 2023-08-04 jrmu ;connect-dots : polygon -> boolean
31 12687dd9 2023-08-04 jrmu ;Given a-poly, connects the posns in sequential order.
32 12687dd9 2023-08-04 jrmu ;IE, the 1st posn connects to the 2nd, the 2nd to the 3rd,
33 12687dd9 2023-08-04 jrmu ;and so forth to the last dot.
34 12687dd9 2023-08-04 jrmu
35 12687dd9 2023-08-04 jrmu (define (connect-dots a-poly)
36 12687dd9 2023-08-04 jrmu (cond
37 12687dd9 2023-08-04 jrmu [(empty? (rest a-poly)) true]
38 12687dd9 2023-08-04 jrmu [else (and
39 12687dd9 2023-08-04 jrmu (draw-solid-line (first a-poly)
40 12687dd9 2023-08-04 jrmu (second a-poly)
41 12687dd9 2023-08-04 jrmu 'black)
42 12687dd9 2023-08-04 jrmu (connect-dots (rest a-poly)))]))
43 12687dd9 2023-08-04 jrmu
44 12687dd9 2023-08-04 jrmu ;Test
45 12687dd9 2023-08-04 jrmu ;(define MRPOLY (cons (make-posn 50 50) (cons (make-posn 50 80) (cons (make-posn 100 80) empty))))
46 12687dd9 2023-08-04 jrmu ;(connect-dots MRPOLY)
47 12687dd9 2023-08-04 jrmu ;
48 12687dd9 2023-08-04 jrmu ;last : polygon -> posn
49 12687dd9 2023-08-04 jrmu ;Given a-poly, finds the last posn element
50 12687dd9 2023-08-04 jrmu ;within the list-of-posns and returns it.
51 12687dd9 2023-08-04 jrmu
52 12687dd9 2023-08-04 jrmu (define (last a-poly)
53 12687dd9 2023-08-04 jrmu (cond
54 12687dd9 2023-08-04 jrmu [(empty? (rest a-poly)) (first a-poly)]
55 12687dd9 2023-08-04 jrmu [else (last (rest a-poly))]))
56 12687dd9 2023-08-04 jrmu
57 12687dd9 2023-08-04 jrmu ;Test
58 12687dd9 2023-08-04 jrmu (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))))))))
59 12687dd9 2023-08-04 jrmu
60 12687dd9 2023-08-04 jrmu ;add-at-end : polygon posn -> polygon
61 12687dd9 2023-08-04 jrmu ;Given a-poly and first-posn,
62 12687dd9 2023-08-04 jrmu ;it adds first-posn to
63 12687dd9 2023-08-04 jrmu ;the end of a-poly, returning
64 12687dd9 2023-08-04 jrmu ;the new polygon. When add-at-end is called,
65 12687dd9 2023-08-04 jrmu ;first-posn should be replaced with
66 12687dd9 2023-08-04 jrmu ;(first a-poly), so that the function call should be
67 12687dd9 2023-08-04 jrmu ;(add-at-end a-poly (first a-poly))
68 12687dd9 2023-08-04 jrmu
69 12687dd9 2023-08-04 jrmu (define (add-at-end a-poly first-posn)
70 12687dd9 2023-08-04 jrmu (cond
71 12687dd9 2023-08-04 jrmu [(empty? (rest a-poly)) (first a-poly)]
72 12687dd9 2023-08-04 jrmu [else (cons (add-at-end a-poly first-posn) (cons first-posn empty))]))
73 12687dd9 2023-08-04 jrmu ;
74 12687dd9 2023-08-04 jrmu ;modified-draw-polygon : polygon -> true
75 12687dd9 2023-08-04 jrmu ;Draws polygon specified by a-poly, where
76 12687dd9 2023-08-04 jrmu ;each posn in a-poly represents a vertex in
77 12687dd9 2023-08-04 jrmu ;the polygon. It does so by using connect-dots
78 12687dd9 2023-08-04 jrmu ;to connect the dots of each vertex.
79 12687dd9 2023-08-04 jrmu ;Specifically, it connects the 1st dot to the 2nd,
80 12687dd9 2023-08-04 jrmu ;the 2nd to the 3rd, and so forth, and finally
81 12687dd9 2023-08-04 jrmu ;connects the last dot to the 1st dot to finish
82 12687dd9 2023-08-04 jrmu ;the polygon by calling on add-at-end.
83 12687dd9 2023-08-04 jrmu
84 12687dd9 2023-08-04 jrmu (define (modified-draw-polygon a-poly)
85 12687dd9 2023-08-04 jrmu (cond
86 12687dd9 2023-08-04 jrmu [(empty? (rest a-poly)) true]
87 12687dd9 2023-08-04 jrmu [else (connect-dots (add-at-end a-poly))]))