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 11.3.5) (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 ;;Produces an intenger between n and m (both endpoints included)
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu ;; random-n-m : integer integer -> integer
7 12687dd9 2023-08-04 jrmu ;; ...
8 12687dd9 2023-08-04 jrmu ;; Assume: n < m
9 12687dd9 2023-08-04 jrmu (define (random-n-m n m)
10 12687dd9 2023-08-04 jrmu (+ (random (- (+ m 1) n)) n))
11 12687dd9 2023-08-04 jrmu
12 12687dd9 2023-08-04 jrmu ;riot : natural-number natural-number natural-number -> boolean
13 12687dd9 2023-08-04 jrmu ;Draws a grid grid-length long and grid-length wide
14 12687dd9 2023-08-04 jrmu ;with num-lines vertically and num-lines
15 12687dd9 2023-08-04 jrmu ;horizontally and n balloons randomly scattered in
16 12687dd9 2023-08-04 jrmu ;the grid. Returns true once evaluation completes.
17 12687dd9 2023-08-04 jrmu ;This function does this by calling
18 12687dd9 2023-08-04 jrmu ;(drawgrid grid-length num-lines whichlength?) to draw the grid lines and
19 12687dd9 2023-08-04 jrmu ;(draw-balloons grid-length num-balloons) to draw the balloons.
20 12687dd9 2023-08-04 jrmu
21 12687dd9 2023-08-04 jrmu ;Examples
22 12687dd9 2023-08-04 jrmu ;(riot 15 500 5)
23 12687dd9 2023-08-04 jrmu ;Starts a canvas 500px wide by 500px long and creates
24 12687dd9 2023-08-04 jrmu ;5 lines vertically and 5 lines horizontally.
25 12687dd9 2023-08-04 jrmu ;It then draws 15 red-circles on the canvas that represent
26 12687dd9 2023-08-04 jrmu ;balloons.
27 12687dd9 2023-08-04 jrmu
28 12687dd9 2023-08-04 jrmu (define (riot num-balloons grid-length num-lines)
29 12687dd9 2023-08-04 jrmu (and
30 12687dd9 2023-08-04 jrmu (start grid-length grid-length)
31 12687dd9 2023-08-04 jrmu (drawgrid grid-length num-lines 1)
32 12687dd9 2023-08-04 jrmu (draw-balloons num-balloons grid-length)))
33 12687dd9 2023-08-04 jrmu
34 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
35 12687dd9 2023-08-04 jrmu ;drawgrid : natural-number natural-number natural-number -> boolean
36 12687dd9 2023-08-04 jrmu ;Draws num-lines horizontal lines and
37 12687dd9 2023-08-04 jrmu ;num-lines vertical lines given grid-length.
38 12687dd9 2023-08-04 jrmu ;We do this by drawing the
39 12687dd9 2023-08-04 jrmu ;horizontal and vertical lines corresponding to whichline?
40 12687dd9 2023-08-04 jrmu ;then drawing the remainder of the lines by recursively calling
41 12687dd9 2023-08-04 jrmu ;on drawgrid. For example, if whichline? is 2, we draw the
42 12687dd9 2023-08-04 jrmu ;second horizontal and second vertical line (in that order).
43 12687dd9 2023-08-04 jrmu ;
44 12687dd9 2023-08-04 jrmu ;Examples
45 12687dd9 2023-08-04 jrmu ;(drawgrid 100 0 1)
46 12687dd9 2023-08-04 jrmu ;true
47 12687dd9 2023-08-04 jrmu
48 12687dd9 2023-08-04 jrmu ;(drawgrid 100 1 1)
49 12687dd9 2023-08-04 jrmu ;Draws one horizontal line and one vertical
50 12687dd9 2023-08-04 jrmu ;line in the center of the canvas.
51 12687dd9 2023-08-04 jrmu ;The horizontal line starts from (make-posn 50 0)
52 12687dd9 2023-08-04 jrmu ;and goes to (make-posn 50 100). The vertical line
53 12687dd9 2023-08-04 jrmu ;starts from (make-posn 0 50) and goes to
54 12687dd9 2023-08-04 jrmu ;(make-posn 100 50).
55 12687dd9 2023-08-04 jrmu
56 12687dd9 2023-08-04 jrmu ;(drawgrid 100 2 1)
57 12687dd9 2023-08-04 jrmu ;Draws two horizontal lines and two vertical lines.
58 12687dd9 2023-08-04 jrmu ;The horizontal lines start from (make-posn 33 0)
59 12687dd9 2023-08-04 jrmu ;and (make-posn 67 0) and go to
60 12687dd9 2023-08-04 jrmu ;(make-posn 33 100) and to (make-posn 67 100),
61 12687dd9 2023-08-04 jrmu ;respectively. The vertical lines start from
62 12687dd9 2023-08-04 jrmu ;(make-posn 0 33) and (make-posn 0 67) and go to
63 12687dd9 2023-08-04 jrmu ;(make-posn 100 33) and (make-posn 100 67),
64 12687dd9 2023-08-04 jrmu ;respectively.
65 12687dd9 2023-08-04 jrmu ;
66 12687dd9 2023-08-04 jrmu ;Template
67 12687dd9 2023-08-04 jrmu ;(define (drawgrid grid-length num-lines))
68 12687dd9 2023-08-04 jrmu ;;
69 12687dd9 2023-08-04 jrmu ;Template
70 12687dd9 2023-08-04 jrmu (define (drawgrid grid-length num-lines whichline?)
71 12687dd9 2023-08-04 jrmu (cond
72 12687dd9 2023-08-04 jrmu [(zero? num-lines) true]
73 12687dd9 2023-08-04 jrmu [(> whichline? num-lines) true]
74 12687dd9 2023-08-04 jrmu [(and
75 12687dd9 2023-08-04 jrmu (>= num-lines 1)
76 12687dd9 2023-08-04 jrmu (<= whichline? num-lines))
77 12687dd9 2023-08-04 jrmu (and
78 12687dd9 2023-08-04 jrmu (draw-solid-line (make-posn 0
79 12687dd9 2023-08-04 jrmu (* whichline?
80 12687dd9 2023-08-04 jrmu (round (/ grid-length (+ num-lines 1)))))
81 12687dd9 2023-08-04 jrmu (make-posn grid-length
82 12687dd9 2023-08-04 jrmu (* whichline?
83 12687dd9 2023-08-04 jrmu (round (/ grid-length (+ num-lines 1)))))
84 12687dd9 2023-08-04 jrmu 'black)
85 12687dd9 2023-08-04 jrmu (draw-solid-line (make-posn (* whichline?
86 12687dd9 2023-08-04 jrmu (round (/ grid-length (+ num-lines 1))))
87 12687dd9 2023-08-04 jrmu 0)
88 12687dd9 2023-08-04 jrmu (make-posn (* whichline?
89 12687dd9 2023-08-04 jrmu (round (/ grid-length (+ num-lines 1))))
90 12687dd9 2023-08-04 jrmu grid-length)
91 12687dd9 2023-08-04 jrmu 'black)
92 12687dd9 2023-08-04 jrmu (drawgrid grid-length num-lines (add1 whichline?)))]))
93 12687dd9 2023-08-04 jrmu ;
94 12687dd9 2023-08-04 jrmu ;Contract, Purpose, Header
95 12687dd9 2023-08-04 jrmu ;draw-balloons : natural-number natural-number -> boolean
96 12687dd9 2023-08-04 jrmu ;Draw num-balloons of balloons randomly
97 12687dd9 2023-08-04 jrmu ;scattered in a grid of width and height grid-length.
98 12687dd9 2023-08-04 jrmu ;It does so by drawing one solid disk colored 'red
99 12687dd9 2023-08-04 jrmu ;with radius R centered at
100 12687dd9 2023-08-04 jrmu ;(make-posn (random-n-m 0 grid-length) (random-n-m 0 grid-length)
101 12687dd9 2023-08-04 jrmu ;that is, a disk centered at a random point within
102 12687dd9 2023-08-04 jrmu ;the grid. It then draws the remaining balloons
103 12687dd9 2023-08-04 jrmu ;by calling on itself
104 12687dd9 2023-08-04 jrmu ;to draw one less balloon than it started with.
105 12687dd9 2023-08-04 jrmu ;It returns true when finished.
106 12687dd9 2023-08-04 jrmu ;
107 12687dd9 2023-08-04 jrmu ;Examples
108 12687dd9 2023-08-04 jrmu ;(draw-balloons 0 grid-length)
109 12687dd9 2023-08-04 jrmu ;true
110 12687dd9 2023-08-04 jrmu ;(draw-balloons 1 grid-length)
111 12687dd9 2023-08-04 jrmu ;Draws one balloon randomly scattered
112 12687dd9 2023-08-04 jrmu ;in the canvas grid-length wide and high.
113 12687dd9 2023-08-04 jrmu ;
114 12687dd9 2023-08-04 jrmu (define (draw-balloons num-balloons grid-length)
115 12687dd9 2023-08-04 jrmu (cond
116 12687dd9 2023-08-04 jrmu [(zero? num-balloons) true]
117 12687dd9 2023-08-04 jrmu [(draw-solid-disk (make-posn (random-n-m 0 grid-length)
118 12687dd9 2023-08-04 jrmu (random-n-m 0 grid-length))
119 12687dd9 2023-08-04 jrmu RADIUS
120 12687dd9 2023-08-04 jrmu 'red)
121 12687dd9 2023-08-04 jrmu (draw-balloons (sub1 num-balloons) grid-length)]))
122 12687dd9 2023-08-04 jrmu
123 12687dd9 2023-08-04 jrmu (define RADIUS 3)
124 12687dd9 2023-08-04 jrmu (riot 50 500 20)