# CP104 Lecture Notes - Lecture 4: Euclidean Distance

37 views3 pages
25 Sep 2016
School
Course
hayjayshay and 38575 others unlocked
2
Verified Note
2 documents

## Document Summary

Structure is a way of bundling several pieces of data together to form a single package. Define our own structures, automatically getting functions that create structures or functions that extract data from structures. A posn is a built-in structure that has 2 fields containing numbers; represent x and y coordinates. The contract is: make-posn: num num posn. There is one selector function per number, posn-x and posn-y. Myposn) => true (make-posn (+ 4 4) (- 2 1)) simplifies to (make-posn 8 1) The function distance (define first-point (make-posn 1 1)) (define second-point (make-posn 4 5)) ;; produces the euclidean distance between posn1 and posn2. ;; examples: (distance first-point second-point) => 5 (define (distance posn1 posn2) (sqrt (+ (sqr (- (posn-x posn1) (posn-x posn2))) (sqr (- (posn-y posn1) (posn-y posn2))))) ;;test for distance (check-expect (distance first-point second-point) 5) ;;produces the posn of point scaled by factor. ;;examples: (scale (make-posn 3 4) 0. 5) => (make-posn 1. 5 2)

## Get access

\$8 USD/m\$10 USD/m
Billed \$96 USD annually
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class