sicp每日一题[2.3]
Exercise 2.3
Implement a representation for rectangles in a plane. (Hint: You may want to make use of Exercise 2.2.) In terms of your constructors and selectors, create procedures that compute the perimeter and the area of a given rectangle. Now implement a different representation for rectangles. Can you design your system with suitable abstraction barriers, so that the same perimeter and area procedures will work using either representation?
首先借用 Exercise 2.2 的函数来表示矩形,我们已经有了线段的表示方法,那矩形就是2组平行且相等的线段,且邻边垂直。为了简单起见,把矩形左下角的顶点坐标设为(0, 0),且一条边落在 x 轴上。然后采用矩形左边的那条边和对角线交点坐标来确定一个矩形,同样为了简便起见,把交点坐标设为(0, 0)。可以看出 make-point, x-point, y-point; make-segment, start-segment, end-segment; make-rect, make-rect-by-meeting-point; get-line-length, get-perimeter, get-area 是四层不同的函数,某一层的函数实现逻辑的改变,不会影响到其他层的调用。
; 用左下的顶点和经过该顶点的两条邻边来确定一个矩形,为简单起见,左下的顶点坐标设为(0, 0)
; length, width 分别表示矩形的长和宽
(define (make-rect length width)
(let ((left-bottom (make-point 0 0)))
(cons (make-segment left-bottom (make-point (car left-bottom) (+ (cdr left-bottom) width))) ; 竖直的那条边
(make-segment left-bottom (make-point (+ (car left-bottom) length) (cdr left-bottom)))))) ; 水平的那条边
; 用矩形的对角线交点和左边的边来确定一个矩形,为简单起见,对角线交点坐标设为(0, 0)
; left-line 表示左边的边
(define (make-rect-by-meeting-point left-line)
(cons left-line (make-segment (start-segment left-line) (make-point (- 0 (x-point (start-segment left-line)))
(y-point (start-segment left-line))))))
; 用两点间距离公式 d = sqrt(x^2 + y^2) 计算线段的长度
(define (get-line-length segment)
(sqrt (+ (square (- (x-point (end-segment segment))
(x-point (start-segment segment))))
(square (- (y-point (end-segment segment))
(y-point (start-segment segment)))))))
; 计算矩形的周长
(define (get-perimeter rect)
(* 2 (+ (get-line-length (car rect))
(get-line-length (cdr rect)))))
; 计算矩形的面积
(define (get-area rect)
(* (get-line-length (car rect))
(get-line-length (cdr rect))))
; 设置一个长为7宽为5的矩形
(define test-rect1 (make-rect 7 5))
(display "矩形test-rect1周长为:")
(display (get-perimeter test-rect1))
(newline)
(display "矩形test-rect1面积为:")
(display (get-area test-rect1))
(newline)
; 设置一个长为7宽为5的矩形
(define A (make-point -3.5 -2.5))
(define B (make-point -3.5 2.5))
(define left-line (make-segment A B))
(define test-rect2 (make-rect-by-meeting-point left-line))
(display "矩形test-rect2周长为:")
(display (get-perimeter test-rect2))
(newline)
(display "矩形test-rect2面积为:")
(display (get-area test-rect2))
(newline)
; 执行结果
矩形test-rect1周长为:24.000000282646763
矩形test-rect1面积为:35.00000070672435
矩形test-rect2周长为:24.000000282646763
矩形test-rect2面积为:35.00000070672435