Exercise 163: Adapt the second example for render-poly to connect-dots.
I was unable to answer this question. We needed to take the supplied render-poly function and convert it to use connect-dots.
This was the best I was able to come up with
; Polygon -> Image
; to render the given polygon p into MT
(define (render-poly p)
(cond ((empty? p) p)
(else (
(connect-dots (first p))
(render-poly (rest p))
))))
The issue with this is that you can't call two functions in a row like that. (At least not with the knowledge we have right now)
If you try and run this, you will get an error:
function call: expected a function after the open parenthesis, but received #<image>
I don't think there is a way to do this without changing render-poly or connect-dots signatures.
Finally I had to sneak a peak at the answer... (which is luckily given shortly further down in the text)
; Polygon -> Image |
; add the Polygon p into an image in MT |
(define (render-polygon p) |
(render-line (connect-dots p) (first p) (last p))) |
In hind-sight this is fairly obvious - or at least the part about using render-line. connect-dots is going to draw the actual polygon for us and return our image. What I don't understand is why we need to draw a line between (first p) and (last p).
Presumably the function connect-dots is going to draw all but this last line (due to the bug in the first implementation given) and we are filling in this last line manually at the end.
This is not very well explained - but hopefully it will become clear as we progress on to question 164. Either I missed something obvious in the text, or I'm not sure how we could have answered this given the information we had.
No comments:
Post a Comment