Browse Source

Doing 2.49

master
parent
commit
85f1830f6b
  1. 39
      2.47.scm
  2. 49
      2.48.scm
  3. 10
      2.49.scm
  4. 55
      imagelib.scm

39
2.47.scm

@ -1,11 +1,14 @@
(load "displaylib.scm")
(title "Exercise 2.46")
(doc "A two-dimensional vector v running from the origin to a point can be represented as a pair consisting of an x-coordinate and a y-coordinate. Implement a data abstraction for vectors by giving a constructor make-vect and corresponding selectors xcor-vect and ycor-vect. In terms of your selectors and constructor, implement procedures add-vect, sub-vect, and scale-vect that perform the operations vector addition, vector subtraction, and multiplying a vector by a scalar:
(x1,y1) + (x2,y2) = (x1+x2 , y1+y2)
(x1,y1) - (x2,y2) = (x1-x2 , y1-y2)
s . (x,y) = (sx, sy)
")
(title "Exercise 2.47")
(doc "Here are two possible constructors for frames:
(define (make-frame origin edge1 edge2)
(list origin edge1 edge2))
(define (make-frame origin edge1 edge2)
(cons origin (cons edge1 edge2)))
For each constructor supply the appropriate selectors to produce an implementation for frames.")
(define (make-vect x y) (list x y))
(define (xcor-vect v) (car v))
@ -27,17 +30,29 @@
(define half-half (make-vect 0.5 0.5))
; ------------------------
(print "List representation")
(define (make-frame origin edge1 edge2)
(list origin edge1 edge2))
(define (origin-frame f) (car f))
(define (edge1-frame f) (cadr f))
(define (edge1-frame f) (caddr f))
(print (make-frame origin xunit yunit))
(define (edge2-frame f) (caddr f))
(define frame (make-frame origin xunit yunit))
(display frame)(newline)
(display (origin-frame frame))(newline)
(display (edge1-frame frame))(newline)
(display (edge2-frame frame))(newline)
(newline)
(print "Cons representation")
(define (make-frame-cons origin edge1 edge2)
(cons origin (cons edge1 edge2)))
(define (origin-frame-cons f) (car f))
(define (edge1-frame-cons f) (car (cdr f))
(define (edge1-frame-cons f) (cdr (cdr f))
(define (edge1-frame-cons f) (car (cdr f)))
(define (edge2-frame-cons f) (cdr (cdr f)))
(define frame-cons (make-frame-cons origin xunit yunit))
(display frame-cons)(newline)
(display (origin-frame-cons frame-cons))(newline)
(display (edge1-frame-cons frame-cons))(newline)
(display (edge2-frame-cons frame-cons))(newline)

49
2.48.scm

@ -0,0 +1,49 @@
(load "displaylib.scm")
(title "Exercise 2.48")
(doc "A directed line segment in the plane can be represented as a pair of vectors -- the vector running from the origin to the start-point of the segment, and the vector running from the origin to the end-point of the segment. Use your vector representation from exercise 2.46 to define a representation for segments with a constructor make-segment and selectors start-segment and end-segment.")
; --- Vectors 2.46 ---
(define (make-vect x y) (list x y))
(define (xcor-vect v) (car v))
(define (ycor-vect v) (cadr v))
(define (add-vect v w) (make-vect
(+ (xcor-vect v) (xcor-vect w))
(+ (ycor-vect v) (ycor-vect w))))
(define (sub-vect v w) (make-vect
(- (xcor-vect v) (xcor-vect w))
(- (ycor-vect v) (ycor-vect w))))
(define (scale-vect s v) (make-vect
(* s (xcor-vect v))
(* s (ycor-vect v))))
(define origin (make-vect 0 0))
(define one-one (make-vect 1 1))
(define xunit (make-vect 1 0))
(define yunit (make-vect 0 1))
(define half-half (make-vect 0.5 0.5))
; -- Frames 2.47 --
; --- frame with list representation ---
(define (make-frame origin edge1 edge2)
(list origin edge1 edge2))
(define (origin-frame f) (car f))
(define (edge1-frame f) (cadr f))
(define (edge2-frame f) (caddr f))
; --- frame with cons representation ---
(define (make-frame-cons origin edge1 edge2)(cons origin (cons edge1 edge2)))
(define (origin-frame-cons f) (car f))
(define (edge1-frame-cons f) (car (cdr f)))
(define (edge2-frame-cons f) (cdr (cdr f)))
; ---------------------
; START
; ---------------------
(define (make-segment start end) (list start end))
(define (start-segment s) (car s))
(define (end-segment s) (cadr s))
(print "START")
(define segment (make-segment half-half one-one))
(display segment)(newline)
(display (start-segment segment))(newline)
(display (end-segment segment))(newline)

10
2.49.scm

@ -0,0 +1,10 @@
(load "displaylib.scm")
(load "imagelib.scm")
(title "Exercise 2.49")
(doc "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.")
; Exercise start

55
imagelib.scm

@ -0,0 +1,55 @@
; --- Vectors 2.46 ---
(define (make-vect x y) (list x y))
(define (xcor-vect v) (car v))
(define (ycor-vect v) (cadr v))
(define (add-vect v w) (make-vect
(+ (xcor-vect v) (xcor-vect w))
(+ (ycor-vect v) (ycor-vect w))))
(define (sub-vect v w) (make-vect
(- (xcor-vect v) (xcor-vect w))
(- (ycor-vect v) (ycor-vect w))))
(define (scale-vect s v) (make-vect
(* s (xcor-vect v))
(* s (ycor-vect v))))
(define origin (make-vect 0 0))
(define one-one (make-vect 1 1))
(define xunit (make-vect 1 0))
(define yunit (make-vect 0 1))
(define half-half (make-vect 0.5 0.5))
; -- Frames 2.47 --
; --- frame with list representation ---
(define (make-frame origin edge1 edge2)
(list origin edge1 edge2))
(define (origin-frame f) (car f))
(define (edge1-frame f) (cadr f))
(define (edge2-frame f) (caddr f))
; --- frame with cons representation ---
(define (make-frame-cons origin edge1 edge2)(cons origin (cons edge1 edge2)))
(define (origin-frame-cons f) (car f))
(define (edge1-frame-cons f) (car (cdr f)))
(define (edge2-frame-cons f) (cdr (cdr f)))
; --- frame segment ---
(define (make-segment start end) (list start end))
(define (start-segment s) (car s))
(define (end-segment s) (cadr s))
; -- defined function from SICP text
(define (frame-coord-map frame)
(lambda (v)
(add-vect
(origin-frame frame)
(add-vect (scale-vect (xcor-vect v)
(edge1-frame frame))
(scale-vect (ycor-vect v)
(edge2-frame frame))))))
(define (segments->painter segment-list)
(lambda (frame)
(for-each
(lambda (segment)
(draw-line
((frame-coord-map frame) (start-segment segment))
((frame-coord-map frame) (end-segment segment))))
segment-list)))
Loading…
Cancel
Save