From: Morton Goldberg Date: 2006-12-06T13:00:25+09:00 Subject: Re: Turtle Graphics (#104) On Dec 5, 2006, at 9:00 PM, Dema wrote: > Thanks for pointing that out. I don't even know how all the sample > drawings were right with that huge bug in the code. A bug in Turtle#toward has no effect on reproducing the sample designs because toward is not used in any of the sample turtle scripts. Turtle#toward is not part of core turtle graphics -- it is part of the optional non-local-geometry extensions found in some, but by no means all, turtle graphic packages. Implementing toward is can be considered an extra-credit exercise. > Morton Goldberg wrote: > >> Your solution passes all the unit tests I supplied and is certainly >> good enough to reproduce all the sample designs. So you have good >> reason to think it's completely correct. However, one of the optional >> methods has a problem. >> >>> # Turn to face the given point. >>> def toward(pt) >>> @heading = atan(pt[0].to_f / pt[1].to_f) / DEG >>> end IMO, Turtle#toward is the most difficult turtle command to get fully right. The version I posted as part of my solution is correct, but it's not the best that can be done. A better implementation would have been: # Turn to face the given point. def toward(pt) x2, y2 = pt must_be_number(x2, 'pt.x') must_be_number(y2, 'pt.y') x1, y1 = xy set_h(atan2(x2 - x1, y2 - y1) / DEG) end But that's not what I had when I wrote the quiz -- this version incorporates an improvement I saw in Matthew Moss' solution. Regards, Morton