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