-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircle.rkt
173 lines (155 loc) · 5.22 KB
/
circle.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
#lang racket
;;; File:
;;; circle.rkt
;;; Summary:
;;; A variety of procedures for working with circles.
;;; 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 "ellipse.rkt")
(provide (all-defined-out))
; +---------+--------------------------------------------------------
; | Circles |
; +---------+
(sstruct %solid-circle %solid-ellipse ()
#:cloneable
#:methods gen:img-fname
[(define .image-fname
(lambda (img dir)
(format "~a/solid-~a-circle-~a.png"
(or dir ".")
(color->color-name (image-color img))
(circle-diameter img))))]
#:methods gen:img-make-desc
[(define image-make-desc
(lambda (img)
(format "a solid ~a circle with diameter ~a"
(describe-color (image-color img))
(ellipse-width img))))]
#:methods gen:img-make-pict
[(define image-make-pict
(lambda (img)
(2htdp:ellipse (ellipse-width img)
(ellipse-height img)
"solid"
(color->2htdp (image-color img)))))]
#:methods gen:img-make-stru
[(define image-make-stru
(lambda (img)
(list 'solid-circle (ellipse-width img) (image-color img))))]
#:done)
(define solid-circle? %solid-circle?)
;;; (solid-circle diameter color [description]) -> image?
;;; diameter : nonnegative-integer?
;;; color : color?
;;; description : string?
;;; Create a solid circle with the given diameter and color.
(define solid-circle
(lambda (diameter color [description #f])
(param-check! solid-circle 1 nonnegative-real? diameter)
(param-check! solid-circle 2 color? color)
(when description
(param-check! solid-circle 3 string? description))
(%solid-circle description
#f
#f
#f
color
diameter
diameter)))
(sstruct %outlined-circle %outlined-ellipse ()
#:cloneable
#:methods gen:img-fname
[(define .image-fname
(lambda (img dir)
(format "~a/outlined-~a-~a-circle-~a.png"
(or dir ".")
(line-width img)
(color->color-name (image-color img))
(circle-diameter img))))]
#:methods gen:img-make-desc
[(define image-make-desc
(lambda (img)
(format "an outlined ~a circle with diameter ~a"
(describe-color (image-color img))
(ellipse-width img))))]
#:methods gen:img-make-stru
[(define image-make-stru
(lambda (img)
(list 'outlined-circle
(ellipse-width img)
(image-color img)
(line-width img))))]
#:done)
(define outlined-circle? %outlined-circle?)
;;; (outlined-circle diameter color [description]) -> image?
;;; diameter : nonnegative-integer?
;;; color : color?
;;; line-width : positive-integer?
;;; description : string?
;;; Create an outlined circle with the given diameter and color.
(define outlined-circle
(lambda (diameter color line-width [description #f])
(param-check! outlined-circle 1 nonnegative-real? diameter)
(param-check! outlined-circle 2 color? color)
(param-check! outlined-circle 3 positive-real? line-width)
(when description
(param-check! outlined-circle 4 string? description))
(%outlined-circle description
#f
#f
#f
color
diameter
diameter
line-width)))
;;; (circle? img) -> boolean?
;;; img : image?
;;; Determine if the image is a circle.
(define circle?
(lambda (img)
(or (solid-circle? img)
(outlined-circle? img)
(and (ellipse? img)
(<= (abs (- (ellipse-width img) (ellipse-height img))) 0.01))
(and (transformed? img)
(preserved? img)
(circle? (subimage img))))))
;;; (circle-diameter circ) -> real?
;;; circ : circle?
;;; Determine the diameter of a circle.
(define circle-diameter
(lambda (circ)
(ellipse-width circ)))
;;; (circle diameter mode color-or-pen [description]) -> image?
;;; diameter : nonnegative-real?
;;; mode : (one-of "solid" "outline" integer?)
;;; color-or-pen : (any-of color? pen?)
;;; description : string?
;;; Create the described circle.
(define circle
(lambda (diameter mode color-or-pen [description #f])
(2htdp-style 'circle
(lambda (color)
; (displayln (format "Creating a ~a solid circle" color))
(solid-circle diameter color description))
(lambda (color line-width)
; (displayln (format "Creating a ~a outlined circle" color))
(outlined-circle diameter color line-width description))
mode
color-or-pen)))