In the picture language in SICP I'm having trouble understanding how the transform-painter
procedure works:
(define (transform-painter painter origin corner1 corner2)
(lambda (frame)
(let ((m (frame-coord-map frame)))
(let ((new-origin (m origin)))
(painter
(make-frame new-origin
(sub-vect (m corner1) new-origin)
(sub-vect (m corner2) new-origin)))))))
Specifically why the vector subtraction of the edge mappings of the argument frame by the new-origin is necessary. I would have thought the mapping of the new corners, i.e (m corner1)
and (m corner2)
, could serve as the new edges of the transformed frame.
I've done some calculation and I want to know where i'm mixed up.
Taking flip-vert
as an example,
(define (flip-vert painter)
(transform-painter painter
(make-vect 0.0 1.0) ; new origin
(make-vect 1.0 1.0) ; new end of edge1
(make-vect 0.0 0.0))) ; new end of edge2
let flip-vert
take as argument a frame with the following dimensions,
Origin = (0, 0), Edge1 = (4, 0), Edge2 = (0, 4)
Accordingly the mapping formula would become (0,0) + x(4,0) + y(0, 4)
.
In the case of flip-vert
the origin
, corner1
, and corner2
passed as arguments to transform-painter
are (0,1)
, (1,1)
and (0,0)
respectively.
So after placing them in the mapping formula, one after the other, we get the results
new-origin = (0,0)+0*(4,0)+1*(0,4) = (0,4)
mapped-corner1 = (0,0)+1*(4,0)+1*(0,4) = (4,4)
mapped-corner2 = (0,0)
Now the transform-painter
takes the final two results and subtracts them by the new-origin
to build the new frame, resulting in a frame with the following dimensions.
Origin = (0,4), New-corner1 = (4,0), New-corner2 = (0,-4)
Looking at these points on a coordinate plane the results don't seem right, while the original corner results before the subtractions took place, do. What am I doing wrong?