-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path10-1-communicating-objects.rkt
executable file
·177 lines (142 loc) · 4.85 KB
/
10-1-communicating-objects.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
#lang racket
(require rackunit)
(require "extras.rkt")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Balls Communicating with an external agent
;;; this is considered poor OO design
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define Ball0<%>
(interface ()
;; -> Integer
;; return x, y coords of center and radius, all in pixels
get-x
get-y
get-r))
(define Ball0%
(class* object% (Ball0<%>)
(init-field x y r) ; interpretation omitted...
(super-new)
(define/public (get-x) x)
(define/public (get-y) y)
(define/public (get-r) r)))
;; Ball0<%> Ball0<%> -> Boolean
;; Do these two balls intersect?
;; This is an ordinary function, outside of any class.
(define (intersects? b1 b2)
(circles-intersect?
(send b1 get-x) (send b1 get-y) (send b1 get-r)
(send b2 get-x) (send b2 get-y) (send b2 get-r)))
;; Would balls with these coordinates intersect?
(define (circles-intersect? x1 y1 r1 x2 y2 r2)
(<=
(+ (sqr (- x1 x2)) (sqr (- y1 y2)))
(sqr (+ r1 r2))))
(begin-for-test
(check-true
(let ((ball1 (new Ball0% [x 0][y 0][r 10]))
(ball2 (new Ball0% [x 0][y 10][r 10])))
(and
(intersects? ball1 ball2)
(intersects? ball2 ball1))))
(check-false
(let ((ball1 (new Ball0% [x 0][y 0][r 10]))
(ball2 (new Ball0% [x 20][y 10][r 10])))
(or
(intersects? ball1 ball2)
(intersects? ball2 ball1)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BALLS COMMUNICATING BY PULL
;; computation gets done in this ball
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define Ball-Pull<%>
(interface ()
;; -> Integer
;; return x, y coords of center and radius, all in pixels
get-x
get-y
get-r
;; Ball1<%> -> Boolean
;; Does this ball intersect the other one?
intersects?
))
;; A Ball1 is a (new Ball1% [x Integer][y Integer][r Integer])
(define Ball1%
(class* object% (Ball-Pull<%>)
(init-field x y r) ; interpretation omitted...
(super-new)
;; Does the other ball intersect this one?
;; STRATEGY: Ask the other ball for its data
(define/public (intersects? other-b)
(coordinates-intersect?
(send other-b get-x)
(send other-b get-y)
(send other-b get-r)))
;; Integer^3 -> Boolean
;; GIVEN: the coordinates of some ball
;; RETURNS: would that ball intersect this one?
;; This is a private method (an ordinary function, but inside the
;; class). Note that it refers to the fields of this object.
(define (coordinates-intersect? other-x other-y other-r)
(<= (+ (sqr (- x other-x)) (sqr (- y other-y)))
(sqr (+ r other-r))))
(define/public (get-x) x)
(define/public (get-y) y)
(define/public (get-r) r)
))
(begin-for-test
(check-true
(let ((ball1 (new Ball1% [x 0][y 0][r 10]))
(ball2 (new Ball1% [x 0][y 10][r 10])))
(and
(send ball1 intersects? ball2)
(send ball2 intersects? ball1))))
(check-false
(let ((ball1 (new Ball1% [x 0][y 0][r 10]))
(ball2 (new Ball1% [x 20][y 10][r 10])))
(or
(send ball1 intersects? ball2)
(send ball2 intersects? ball1)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; BALLS COMMUNICATING BY PUSH
;;; computation gets done in other-ball
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define Ball-Push<%>
(interface ()
;; not needed for this example!
;; ;; -> Integer
;; ;; return x, y coords of center and radius, all in pixels
;; get-x
;; get-y
;; get-r
;; Ball-Push<%> -> Boolean
;; does this ball intersect the other one?
intersects?
;; Integer^3 -> Boolean
;; Would a ball with the given x,y,r intersect this one?
intersect-responder
))
;; A Ball2 is a (new Ball2% [x Integer][y Integer][r Integer])
(define Ball2%
(class* object% (Ball-Push<%>)
(init-field x y r) ; interpretation omitted...
(super-new)
(define/public (intersects? other-b)
(send other-b intersect-responder x y r))
;; Integer^3 -> Boolean
(define/public (intersect-responder other-x other-y other-r)
(<= (+ (sqr (- x other-x)) (sqr (- y other-y)))
(sqr (+ r other-r))))
))
(begin-for-test
(check-true
(let ((ball1 (new Ball2% [x 0][y 0][r 10]))
(ball2 (new Ball2% [x 0][y 10][r 10])))
(and
(send ball1 intersects? ball2)
(send ball2 intersects? ball1))))
(check-false
(let ((ball1 (new Ball2% [x 0][y 0][r 10]))
(ball2 (new Ball2% [x 20][y 10][r 10])))
(or
(send ball1 intersects? ball2)
(send ball2 intersects? ball1)))))