SICP Exercise 2.49 segments-painter
Exercise 2.49. Use segments->painter to define the following primitive painters:
a. The painter that draws the outline of the designated frame.
b. The painter that draws an ``X'' by connecting opposite corners of the frame.
c. The painter that draws a diamond shape by connecting the midpoints of the sides of the frame.
d. The wave painter.
SOLUTION
The code and tests are here.
Note: The implementation of the procedure segments->painter in the textbook is different from the similarly named procedure in the sicp plt package.
The plt package procedure takes a list of segments and draws them. Moreover, this procedure draws segments only inside the unit square where 0 <= x,y <= 1. Any points ouside the unit square are simply ignored (at least, there is no visual output)
The textbook implementation also takes a list of segments but instead of drawing them directly, evaluates to a procedure that takes a frame as its input. This procedure when supplied with a frame, maps all the segments from the originally supplied list to this frame and then draws them. Now, the 'draw-line' procedure in the textbook implementation has no equivalent in the plt package. So in order to use the plt package implementation of segments->painter, I have modified the textbook implementation of segments->painter.
Finally, to prevent a name-clash, I have renamed the textbook procedure by appending a '-local' suffix.
a. The painter that draws the outline of the designated frame.
b. The painter that draws an ``X'' by connecting opposite corners of the frame.
c. The painter that draws a diamond shape by connecting the midpoints of the sides of the frame.
d. The wave painter.
SOLUTION
The code and tests are here.
The plt package procedure takes a list of segments and draws them. Moreover, this procedure draws segments only inside the unit square where 0 <= x,y <= 1. Any points ouside the unit square are simply ignored (at least, there is no visual output)
The textbook implementation also takes a list of segments but instead of drawing them directly, evaluates to a procedure that takes a frame as its input. This procedure when supplied with a frame, maps all the segments from the originally supplied list to this frame and then draws them. Now, the 'draw-line' procedure in the textbook implementation has no equivalent in the plt package. So in order to use the plt package implementation of segments->painter, I have modified the textbook implementation of segments->painter.
Finally, to prevent a name-clash, I have renamed the textbook procedure by appending a '-local' suffix.
Comments
Post a Comment