-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawing-shapes.bak
155 lines (124 loc) · 5.2 KB
/
drawing-shapes.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname drawing-shapes) (read-case-sensitive #t) (teachpacks ((lib "draw.rkt" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.rkt" "teachpack" "htdp")) #f)))
(define-struct circle(center radius color))
;; DATA EXAMPLES
;(make-circle (make-posn 1 1) 100 'red)
;(make-circle (make-posn 10 10) 40 'blue)
(define (draw-a-circle c)
(draw-circle (circle-center c)
(circle-radius c)
(circle-color c)))
(define (clear-a-circle c)
(clear-circle (circle-center c)
(circle-radius c)
(circle-color c)))
(define (in-circle? c p)
(<= (sqrt (+ (sqr (- (posn-x (circle-center c))
(posn-x p)))
(sqr (- (posn-y (circle-center c))
(posn-y p)))))
(circle-radius c)))
(in-circle? (make-circle (make-posn 6 5) 1 'blue) (make-posn 6 5)) "should be" true
;(in-circle? (make-circle (make-posn 6 5) 1 'green) (make-posn 5.5 5)) "should be" true
;(in-circle? (make-circle (make-posn 6 5) 1 'yellow) (make-posn 1 5)) "should be" false
(define (translate-circle circle delta)
(make-circle
(make-posn (+ delta(posn-x(circle-center circle)))
(posn-y(circle-center circle)))
(circle-radius circle)
(circle-color circle)))
(start 300 300)
(draw-a-circle
(translate-circle
(make-circle(make-posn 1 1) 100 'red)100))
(draw-a-circle(make-circle(make-posn 10 10) 40 'blue))
(clear-a-circle(make-circle(make-posn 10 10) 40 'blue))
(define (draw-and-clear-circle a-circle wait-time)
(and(draw-a-circle a-circle)
(sleep-for-a-while wait-time)
(clear-a-circle a-circle)))
(define test-circle (make-circle(make-posn 1 1) 100 'green))
(draw-and-clear-circle test-circle 1)
(define (move-circle delta a-circle)
(cond
[(draw-and-clear-circle a-circle 1/2) (translate-circle a-circle delta)]
[else a-circle]))
(draw-a-circle
(move-circle 10
(move-circle 10
(move-circle 10
(move-circle 10 test-circle)))))
;; A rectangle is a structure:
;; (make-rectangle P W H)
;; where P is a posn, W is a number and H is a number.
(define-struct rectangle (nw-corner width height color))
;; DATA EXAMPLES
(define example-rectangle1 (make-rectangle (make-posn 20 20) 260 260 'red))
(define example-rectangle2 (make-rectangle (make-posn 60 60) 180 180 'blue))
#|
;; Template
(define (fun-for-rectangle a-rectangle)
... (rectangle-nw-corner a-rectangle) ...
... (rectangle-width a-rectangle) ...
... (rectangle-height a-rectangle) ...
... (rectangle-color a-rectangle) ...)
|#
; -------------------------------------------------------------------------
;; draw-a-rectangle : rectangle -> true
;; to draw a-rect
(define (draw-a-rectangle a-rectangle)
(draw-solid-rect
(rectangle-nw-corner a-rectangle)
(rectangle-width a-rectangle)
(rectangle-height a-rectangle)
(rectangle-color a-rectangle)))
;; EXAMPLES
(start 300 300)
(draw-a-rectangle example-rectangle1)
(draw-a-rectangle example-rectangle2)
; -------------------------------------------------------------------------
;; in-rectangle? : rectangle posn -> boolean
;; to determine if a-posn is in a-rectangle, or not
(define (in-rectangle? a-rectangle a-posn)
(and (<= (posn-x (rectangle-nw-corner a-rectangle))
(posn-x a-posn)
(+ (posn-x (rectangle-nw-corner a-rectangle))
(rectangle-width a-rectangle)))
(<= (posn-y (rectangle-nw-corner a-rectangle))
(posn-y a-posn)
(+ (posn-y (rectangle-nw-corner a-rectangle))
(rectangle-height a-rectangle)))))
;; EXAMPLES AS TESTS
(in-rectangle? example-rectangle1 (make-posn 0 0)) "should be" false
(in-rectangle? example-rectangle1 (make-posn 25 0)) "should be" false
(in-rectangle? example-rectangle1 (make-posn 0 25)) "should be" false
(in-rectangle? example-rectangle1 (make-posn 25 25)) "should be" true
; -------------------------------------------------------------------------
;; translate-rectangle : rectangle number -> rectangle
;; to translate a-rectangle horizontally by x pixels
(define (translate-rectangle a-rectangle x)
(make-rectangle (make-posn
(+ x (posn-x (rectangle-nw-corner a-rectangle)))
(posn-y (rectangle-nw-corner a-rectangle)))
(rectangle-width a-rectangle)
(rectangle-height a-rectangle)
(rectangle-color a-rectangle)))
;; EXAMPLES AS TESTS
(translate-rectangle example-rectangle1 30)
"should be"
(make-rectangle (make-posn 50 20) 260 260 'red)
; -------------------------------------------------------------------------
;; clear-a-rectangle : rectangle -> true
;; to erase a rectangle
(define (clear-a-rectangle a-rectangle)
(clear-solid-rect
(rectangle-nw-corner a-rectangle)
(rectangle-width a-rectangle)
(rectangle-height a-rectangle)))
;; EXAMPLES
(start 300 300)
(draw-a-rectangle example-rectangle1)
(draw-a-rectangle example-rectangle2)
(clear-a-rectangle example-rectangle1)
(clear-a-rectangle example-rectangle2)