正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 30.1.7 Here is one straightforward way. Assume that we have some a procedural definition of geor primitive drawing method called ( define(ge。 rge rect) (draw-line reet .250.35.5) draw-line r x1xt4:5.35 that takes a rectangle as input, and a set of coordinate values (e.g. x and y start point, and x and y end point), and draws a 89911.0g line from start to end within that rectangle. Note, details of the rectangle are irrelevant, we are just burying them beneath the abstraction. Thus, the first expression would draw a line starting 25 units to the right of the lower left hand corner, and ending at /4 enmatdtraw-line rect.15.60,85)) a point. 35 units to the right and 5 units up from the lower left hand corner Given that, here is a definition of George. Note what this does. Each expression gives a start and end point relative to the origin of the rectangle, and draw-line then sketches the line between those points Blech! This is far too specific, right? Given this definition of George, how do i create a rotated George or a flipped George? This is not obvious, and for an important reason. Here I have intertwined the action of drawing with the data representing George. I have not separated those actions, and moreover, I have chosen a very low-level representation for the elements of George. I really need to isolate these two aspects if I am to have any hope of drawing different pictures of George Slide 30.1.8 Data abstractions for lines So lets fix this. first we need some data abstractions to isolate points from the use of points. What is a point or a vector? It is define p1(make-vect 23))just a way of gluing together an x coordinate and ay (xcor p1))2 coordinate. so we can create an abstraction for this This has a (ycor p1)+3 constructor(called ma ke-vect) and two selectors(called xcor and yor) 48 1001 SICP Slide 30.1.9 Note that there is an inherent contract between these two Data abstractions for lines components: whatever method we use to glue things together in the constructor, we can get the parts back out using the efine pi(make-vect23》 selectors but the details of how we do that don t matter to p1)→2 things that simply want to use these objects rp1)→3 48m 6001CP6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 30.1.7 Here is one straightforward way. Assume that we have some primitive drawing method called draw-line, that takes a rectangle as input, and a set of coordinate values (e.g. x and y start point, and x and y end point), and draws a line from start to end within that rectangle. Note, details of the rectangle are irrelevant, we are just burying them beneath the abstraction. Thus, the first expression would draw a line starting .25 units to the right of the lower left hand corner, and ending at a point .35 units to the right and .5 units up from the lower left hand corner. Given that, here is a definition of George. Note what this does. Each expression gives a start and end point, relative to the origin of the rectangle, and draw-line then sketches the line between those points. Blech! This is far too specific, right? Given this definition of George, how do I create a rotated George or a flipped George? This is not obvious, and for an important reason. Here I have intertwined the action of drawing with the data representing George. I have not separated those actions, and moreover, I have chosen a very low-level representation for the elements of George. I really need to isolate these two aspects if I am to have any hope of drawing different pictures of George. Slide 30.1.8 So let's fix this. First, we need some data abstractions, to isolate points from the use of points. What is a point or a vector? It is just a way of gluing together an x coordinate and a y coordinate, so we can create an abstraction for this. This has a constructor (called make-vect) and two selectors (called xcor and ycor). Slide 30.1.9 Note that there is an inherent contract between these two components: whatever method we use to glue things together in the constructor, we can get the parts back out using the selectors, but the details of how we do that don’t matter to things that simply want to use these objects
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有