-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#lang racket | ||
; Exercise 2.2. Consider the problem of representing line segments in a plane. | ||
; Each segment is represented as a pair of points: a starting point and an ending point. | ||
|
||
(require "../common.rkt") | ||
|
||
(define (make-point x y ) | ||
(cons x y)) | ||
|
||
(define (get-x p) | ||
(car p)) | ||
|
||
(define (get-y p) | ||
(cdr p)) | ||
|
||
(define (make-segment start-p end-p) | ||
(cons start-p end-p)) | ||
|
||
(define (get-start segment) | ||
(car segment)) | ||
|
||
(define (get-end segment) | ||
(cdr segment)) | ||
|
||
(define (get-midpoint segment) | ||
(let ((start-x (get-x (get-start segment))) | ||
(end-x (get-x (get-end segment))) | ||
(start-y (get-y (get-start segment))) | ||
(end-y (get-y (get-end segment)))) | ||
(make-point (/ (+ start-x end-x) 2.0) | ||
(/ (+ start-y end-y) 2.0)))) | ||
|
||
(test "make-point" | ||
(λ () | ||
(assert "x is car" (car (make-point -5 7)) -5) | ||
(assert "y is cdr" (cdr (make-point -5 7)) 7) | ||
(assert "is pair" (pair? (make-point -5 7)) #t))) | ||
|
||
(test "get-x" | ||
(λ () | ||
(assert-simple (get-x (make-point -5 7)) -5))) | ||
|
||
(test "get-y" | ||
(λ () | ||
(assert-simple (get-y (make-point -5 7)) 7))) | ||
|
||
(define (with-data) | ||
(define p1 (make-point 3.0 4.0)) | ||
(define p2 (make-point 6.0 7.0)) | ||
(test "make-segment" | ||
(λ () | ||
(assert "start is car" (car (make-segment p1 p2)) p1) | ||
(assert "end is cdr" (cdr (make-segment p1 p2)) p2))) | ||
(test "get endpoints" | ||
(λ () | ||
(assert "start" (get-start (make-segment p1 p2)) p1) | ||
(assert "end" (get-end (make-segment p1 p2)) p2))) | ||
(test "mid-point" | ||
(λ () | ||
(assert "x of midpoint" (get-x (get-midpoint (make-segment p1 p2))) 4.5) | ||
(assert "y of midpoint" (get-y (get-midpoint (make-segment p1 p2))) 5.5) | ||
(assert "y of midpoint" (get-end (make-segment p1 p2)) p2)))) | ||
|
||
(with-data) |