-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiamond.rkt
247 lines (224 loc) · 7.47 KB
/
diamond.rkt
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#lang racket
;;; File:
;;; diamond.rkt
;;; Summary:
;;; A variety of procedures for working with diamonds.
;;; Author:
;;; Samuel A. Rebelsky
;;;
;;; IMPORTANT: If you are going to edit this file (or any of the
;;; imported files), make sure that you've updated the DrRacket editor
;;; to treat `sstruct` like `struct`.
(require racket/generic)
(require racket/include)
(require (prefix-in 2htdp: 2htdp/image))
(require "sstruct.rkt")
(require "cloneable.rkt")
(permit-cloneable)
(permit-done)
(require "type-predicates.rkt")
(require "colors.rkt")
(require "point.rkt")
(require "line.rkt")
(require "image-core.rkt")
(require "polygon.rkt")
(provide (all-defined-out))
; +----------------+-------------------------------------------------
; | Solid diamonds |
; +----------------+
(sstruct %solid-diamond %solid-polygon (width height)
#:cloneable
#:methods gen:img-fname
[(define .image-fname
(lambda (img dir)
(format "~a/solid-~a-~ax~a-diamond.png"
(or dir ".")
(color->color-name (image-color img))
(diamond-width img)
(diamond-height img))))]
#:methods gen:img-make-desc
[(define image-make-desc
(lambda (img)
(format "a solid ~a ~a-by-~a diamond"
(describe-color (image-color img))
(diamond-width img)
(diamond-height img))))]
#:methods gen:img-make-stru
[(define image-make-stru
(lambda (img)
(list 'solid-diamond
(diamond-width img)
(diamond-height img)
(image-color img))))]
#:done)
(define solid-diamond? %solid-diamond?)
(define solid-diamond-width %solid-diamond-width)
(define solid-diamond-height %solid-diamond-height)
;;; (solid-diamond width height color [desc]) -> image?
;;; width : nonnegative-real?
;;; height : nonnegative-real?
;;; color : color?
;;; description : string?
;;; A polygon whose vertices are given by `points` and whose color is `color`.
(define solid-diamond
(lambda (width
height
color
[description #f])
(param-check! solid-diamond 1 nonnegative-real? width)
(param-check! solid-diamond 2 nonnegative-real? height)
(param-check! solid-diamond 3 color? color)
(when description
(param-check! solid-diamond 4 string? description))
(%solid-diamond description
#f
#f
#f
(color->rgb color)
(diamond-points width height)
width
height)))
(sstruct %outlined-diamond %outlined-polygon (width height)
#:cloneable
#:methods gen:img-fname
[(define .image-fname
(lambda (img dir)
(format "~a/outlined-~a-~a-~ax~a-diamond.png"
(or dir ".")
(color->color-name (image-color img))
(line-width img)
(diamond-width img)
(diamond-height img))))]
#:methods gen:img-make-desc
[(define image-make-desc
(lambda (img)
(format "an outlined ~a ~a-by-~a diamond"
(describe-color (image-color img))
(diamond-width img)
(diamond-height img))))]
#:methods gen:img-make-stru
[(define image-make-stru
(lambda (img)
(list 'outlined-diamond
(diamond-width img)
(diamond-height img)
(image-color img)
(line-width img))))]
#:done)
; +-------------------+----------------------------------------------
; | Outlined diamonds |
; +-------------------+
(define outlined-diamond? %outlined-diamond?)
(define outlined-diamond-width %outlined-diamond-width)
(define outlined-diamond-height %outlined-diamond-height)
;;; (outlined-diamond width height color line-width [desc]) -> image?
;;; width : nonnegative-real?
;;; height : nonnegative-real?
;;; color : color?
;;; line-width : positive-integer?
;;; description : string?
;;; An outlined diamond whose inner size is width-by-height with
;;; an outlined of `line-width`.
(define outlined-diamond
(lambda (width
height
color
line-width
[description #f])
(param-check! outlined-diamond 1 nonnegative-real? width)
(param-check! outlined-diamond 2 nonnegative-real? height)
(param-check! outlined-diamond 3 color? color)
(param-check! outlined-diamond 4 positive-integer? line-width)
(when description
(param-check! outlined-diamond 5 string? description))
(%outlined-diamond description
#f
#f
#f
(color->rgb color)
(diamond-points width height)
line-width
width
height)))
;;; (diamond-polygon? poly) -> boolean?
;;; poly : polygon?
;;; Determine if poly is a diamond.
(define diamond-polygon?
(lambda (poly)
(and (polygon? poly)
(= 4 (polygon-sides poly))
(= (polygon-side poly 0)
(polygon-side poly 1)
(polygon-side poly 2)
(polygon-side poly 3)))))
;;; (diamond? img) -> boolean?
;;; img : image?
;;; Determine if `img` is a diamond.
(define diamond?
(lambda (img)
(or (solid-diamond? img)
(outlined-diamond? img)
(diamond-polygon? img)
(and (transformed? img)
(preserved? img)
(diamond? (subimage img))))))
;;; (diamond-height d) -> real?
;;; d : diamond?
;;; Determine the height of a diamond
(define diamond-height
(lambda (d)
(cond
[(solid-diamond? d)
(solid-diamond-height d)]
[(outlined-diamond? d)
(outlined-diamond-height d)]
[(diamond-polygon? d)
(distance (polygon-point d 1) (polygon-point d 3))]
[(and (transformed? d) (preserved? d))
(diamond-height (subimage d))]
[else
(error 'diamond-height "not a diamond ~a" d)])))
;;; (diamond-width d) -> real?
;;; d : diamond?
;;; Determine the width of a diamond
(define diamond-width
(lambda (d)
(cond
[(solid-diamond? d)
(solid-diamond-width d)]
[(outlined-diamond? d)
(outlined-diamond-width d)]
[(diamond-polygon? d)
(distance (polygon-point d 0) (polygon-point d 2))]
[(and (transformed? d) (preserved? d))
(diamond-width (subimage d))]
[else
(error 'diamond-width "not a diamond ~a" d)])))
;;; (diamond width height mode color-or-pen [description]) -> image?
;;; width : nonnegative-real?
;;; height : nonnegative-real?
;;; mode : (one-of "solid" "outline" integer?)
;;; color-or-pen : (any-of color? pen?)
;;; description : string?
;;; Create the described diamond.
(define diamond
(lambda (width height mode color-or-pen [description #f])
(2htdp-style 'diamond
(lambda (color)
(solid-diamond width height color description))
(lambda (color line-width)
(outlined-diamond width height color line-width description))
mode
color-or-pen)))
; +----------------------------------+-------------------------------
; | Miscellaneous diamond procedures |
; +----------------------------------+
;;; (diamond-points width height) -> (list-of point?)
;;; width :positive-real?
;;; height : positive-real?
;;; Determine the points for a width-by-height diamond.
(define diamond-points
(lambda (width height)
(let* ([w (/ width 2)]
[h (/ height 2)])
(list (pt w 0) (pt width h) (pt w height) (pt 0 h)))))