-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw7.rb
405 lines (367 loc) · 10.7 KB
/
hw7.rb
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# University of Washington, Programming Languages, Homework 7, hw7.rb
# (See also ML code)
# a little language for 2D geometry objects
# each subclass of GeometryExpression, including subclasses of GeometryValue,
# needs to respond to messages preprocess_prog and eval_prog
#
# each subclass of GeometryValue additionally needs:
# * shift
# * intersect, which uses the double-dispatch pattern
# * intersectPoint, intersectLine, and intersectVerticalLine for
# for being called by intersect of appropriate clases and doing
# the correct intersection calculuation
# * (We would need intersectNoPoints and intersectLineSegment, but these
# are provided by GeometryValue and should not be overridden.)
# * intersectWithSegmentAsLineResult, which is used by
# intersectLineSegment as described in the assignment
#
# you can define other helper methods, but will not find much need to
# Note: geometry objects should be immutable: assign to fields only during
# object construction
# Note: For eval_prog, represent environments as arrays of 2-element arrays
# as described in the assignment
class GeometryExpression
# do *not* change this class definition
Epsilon = 0.00001
end
class GeometryValue
# do *not* change methods in this class definition
# you can add methods if you wish
private
# some helper methods that may be generally useful
def real_close(r1,r2)
(r1 - r2).abs < GeometryExpression::Epsilon
end
def real_close_point(x1,y1,x2,y2)
real_close(x1,x2) && real_close(y1,y2)
end
# two_points_to_line could return a Line or a VerticalLine
def two_points_to_line(x1,y1,x2,y2)
if real_close(x1,x2)
VerticalLine.new x1
else
m = (y2 - y1).to_f / (x2 - x1)
b = y1 - m * x1
Line.new(m,b)
end
end
public
# we put this in this class so all subclasses can inherit it:
# the intersection of self with a NoPoints is a NoPoints object
def intersectNoPoints np
np # could also have NoPoints.new here instead
end
# we put this in this class so all subclasses can inhert it:
# the intersection of self with a LineSegment is computed by
# first intersecting with the line containing the segment and then
# calling the result's intersectWithSegmentAsLineResult with the segment
def intersectLineSegment seg
line_result = intersect(two_points_to_line(seg.x1,seg.y1,seg.x2,seg.y2))
line_result.intersectWithSegmentAsLineResult seg
end
def preprocess_prog
self
end
end
class NoPoints < GeometryValue
# do *not* change this class definition: everything is done for you
# (although this is the easiest class, it shows what methods every subclass
# of geometry values needs)
# However, you *may* move methods from here to a superclass if you wish to
# Note: no initialize method only because there is nothing it needs to do
def eval_prog env
self # all values evaluate to self
end
def preprocess_prog
self # no pre-processing to do here
end
def shift(dx,dy)
self # shifting no-points is no-points
end
def intersect other
other.intersectNoPoints self # will be NoPoints but follow double-dispatch
end
def intersectPoint p
self # intersection with point and no-points is no-points
end
def intersectLine line
self # intersection with line and no-points is no-points
end
def intersectVerticalLine vline
self # intersection with line and no-points is no-points
end
# if self is the intersection of (1) some shape s and (2)
# the line containing seg, then we return the intersection of the
# shape s and the seg. seg is an instance of LineSegment
def intersectWithSegmentAsLineResult seg
self
end
end
class Point < GeometryValue
# *add* methods to this class -- do *not* change given code and do not
# override any methods
# Note: You may want a private helper method like the local
# helper function inbetween in the ML code
attr_reader :x, :y
def initialize(x,y)
@x = x
@y = y
end
def eval_prog env
self # all values evaluate to self
end
def preprocess_prog
self # no pre-processing to do here
end
def shift(dx,dy)
Point.new(@x + dx, @y + dy)
end
def intersect other
other.intersectPoint self
end
def intersectPoint p
if real_close_point(@x, @y, p.x, p.y)
self
else
NoPoints.new
end
end
def intersectLine line
if real_close(@y, line.m * @x + line.b)
self
else
NoPoints.new
end
end
def intersectVerticalLine vline
if real_close(@x, vline.x)
self
else
NoPoints.new
end
end
def intersectWithSegmentAsLineResult seg
if in_between(@x, seg.x1, seg.x2) && in_between(@y, seg.y1, seg.y2)
self
else
NoPoints.new
end
end
private
def in_between (val, end1, end2)
epsilon = GeometryExpression::Epsilon
(end1 - epsilon <= val && end2 + epsilon >= val) || (end1 + epsilon >= val && end2 - epsilon <= val)
end
end
class Line < GeometryValue
# *add* methods to this class -- do *not* change given code and do not
# override any methods
attr_reader :m, :b
def initialize(m,b)
@m = m
@b = b
end
def eval_prog env
self # all values evaluate to self
end
def preprocess_prog
self # no pre-processing to do here
end
def shift(dx,dy)
Line.new(@m, @b + dy - @m * dx)
end
def intersect other
other.intersectLine self
end
def intersectPoint p
p.intersectLine self
end
def intersectLine line
if real_close(@m, line.m) && real_close(@b, line.b)
self
elsif real_close(@m, line.m) && !real_close(@b, line.b)
NoPoints.new
else
x = (line.b - @b) / (@m - line.m)
Point.new(x, @m * x + @b)
end
end
def intersectVerticalLine vline
Point.new(vline.x, @m * vline.x + @b)
end
def intersectWithSegmentAsLineResult seg
seg
end
end
class VerticalLine < GeometryValue
# *add* methods to this class -- do *not* change given code and do not
# override any methods
attr_reader :x
def initialize x
@x = x
end
def eval_prog env
self # all values evaluate to self
end
def preprocess_prog
self # no pre-processing to do here
end
def shift(dx,dy)
VerticalLine.new(@x + dx)
end
def intersect other
other.intersectVerticalLine self
end
def intersectPoint p
p.intersectVerticalLine self
end
def intersectLine line
line.intersectVerticalLine self
end
def intersectVerticalLine vline
if real_close(@x, vline.x)
self
else
NoPoints.new
end
end
def intersectWithSegmentAsLineResult seg
seg
end
end
class LineSegment < GeometryValue
# *add* methods to this class -- do *not* change given code and do not
# override any methods
# Note: This is the most difficult class. In the sample solution,
# preprocess_prog is about 15 lines long and
# intersectWithSegmentAsLineResult is about 40 lines long
attr_reader :x1, :y1, :x2, :y2
def initialize (x1,y1,x2,y2)
@x1 = x1
@y1 = y1
@x2 = x2
@y2 = y2
end
def eval_prog env
self # all values evaluate to self
end
def preprocess_prog
self # no pre-processing to do here
end
def shift(dx,dy)
LineSegment.new(@x1 + dx, @y1 + dy, @x2 + dx, @y2 + dy)
end
def intersect other
other.intersectLineSegment self
end
def intersectPoint p
p.intersectLineSegment self
end
def intersectLine line
line.intersectLineSegment self
end
def intersectVerticalLine vline
vline.intersectLineSegment self
end
def intersectWithSegmentAsLineResult seg
if real_close(x1, x2)
# on vertical line
first_seg = y1 < seg.y1 ? self : seg
second_seg = y1 < seg.y1 ? seg : self
if real_close(first_seg.y2, second_seg.y1)
Point.new(first_seg.x2, first_seg.y2) # Touch at one point
elsif first_seg.y2 < second_seg.y1
NoPoints.new # No touch
elsif first_seg.y2 > second_seg.y2
LineSegment.new(second_seg.x1, second_seg.y1, second_seg.x2, second_seg.y2) # second seg is inside
else
LineSegment.new(second_seg.x1, second_seg.y1, first_seg.x2, first_seg.y2) # Overlapping
end
else
first_seg = x1 < seg.x1 ? self : seg
second_seg = x1 < seg.x1 ? seg : self
if real_close(first_seg.x2, second_seg.x1)
Point.new(first_seg.x2, first_seg.y2) # Touch at one point
elsif first_seg.x2 < second_seg.x1
NoPoints.new # No touch
elsif first_seg.x2 > second_seg.x2
LineSegment.new(second_seg.x1, second_seg.y1, second_seg.x2, second_seg.y2) # second seg is inside
else
LineSegment.new(second_seg.x1, second_seg.y1, first_seg.x2, first_seg.y2) # Overlapping
end
end
end
def preprocess_prog
if real_close_point(x1, y1, x2, y2)
Point.new(x1, y1)
elsif real_close(x1, x2) && y2 < y1
LineSegment.new(x2, y2, x1, y1)
elsif !real_close(x1, x2) && x2 < x1
LineSegment.new(x2, y2, x1, y1)
else
self
end
end
end
# Note: there is no need for getter methods for the non-value classes
class Intersect < GeometryExpression
# *add* methods to this class -- do *not* change given code and do not
# override any methods
def initialize(e1,e2)
@e1 = e1
@e2 = e2
end
def eval_prog env
@e1.eval_prog(env).intersect(@e2.eval_prog(env))
end
def preprocess_prog
Intersect.new(@e1.preprocess_prog, @e2.preprocess_prog)
end
end
class Let < GeometryExpression
# *add* methods to this class -- do *not* change given code and do not
# override any methods
# Note: Look at Var to guide how you implement Let
def initialize(s,e1,e2)
@s = s
@e1 = e1
@e2 = e2
end
def eval_prog env
@e2.eval_prog([[@s, @e1.eval_prog(env)]] + env)
end
def preprocess_prog
Let.new(@s, @e1.preprocess_prog, @e2.preprocess_prog)
end
end
class Var < GeometryExpression
# *add* methods to this class -- do *not* change given code and do not
# override any methods
def initialize s
@s = s
end
def eval_prog env # remember: do not change this method
pr = env.assoc @s
raise "undefined variable" if pr.nil?
pr[1]
end
def preprocess_prog
self
end
end
class Shift < GeometryExpression
# *add* methods to this class -- do *not* change given code and do not
# override any methods
def initialize(dx,dy,e)
@dx = dx
@dy = dy
@e = e
end
def eval_prog env # remember: do not change this method
eval_result = @e.eval_prog env
eval_result.shift(@dx, @dy)
end
def preprocess_prog
Shift.new(@dx, @dy, @e.preprocess_prog)
end
end