(require 2htdp/image) (require 2htdp/universe) ;;; ======================================================= ;;; CMPU-101, Spring 2013 ;;; Lab 8 ;;; ======================================================= (display "\n CS101 Lab 8, Spring 2013") (display "\n Please write your name here\n\n") ;; IMPORTANT! MAKE SURE YOUR LINES OF CODE DO NOT WRAP WHEN YOUR ;; WINDOW IS ABOUT 80 COLUMNS ACROSS (SEE A COACH OR YOUR PROF IF YOU ;; ARE UNSURE HOW TO DO THIS). (display "\n\n----------------------------\n") (display "Problem 1(a): Generate posn list") (display "\n----------------------------\n") ; ; A list of posns is either ; 1. empty, or ; 2. it's a (cons p lop), where p is a posn and lop is a list of posns ; that is one element shorter than (cons p lop). ; ; Write a function gen-posn-list that consumes one positive natural ; number n, and produces a list of n posns with x and y fields that ; are random numbers generated between 0 and SIDE (that is, the x field ; is a random number between 0 and SIDE and the y field is a random ; number between 0 and SIDE). ; ; SIDE is a constant that represents the width and height of an empty ; scene and you should define this to be 800. ; ; Some results of running this function in the interactions window are ; shown below: ; ; > (gen-posn-list 3) ; (#(struct:posn 474 23) #(struct:posn 496 420) #(struct:posn 660 415)) ; > (gen-posn-list 2) ; (#(struct:posn 522 189) #(struct:posn 31 617)) ; > (gen-posn-list 0) ; empty ; ; You should use the function you write for this problem to generate ; lists of posns for the printf statements in problem 1(b). ; ;;Constant definitions: ;;Contract: ;;Header: ;;Purpose: ;;Pre-function tests: Can be done to test length of list. Contents ;; of posns are set at random, so can't be easily ;; tested. ;;Function definition: ;(define gen-posn-list ;;Post-function printfs: (uncomment after writing the function) ;(printf "(gen-posn-list 0) => ~a~%" (gen-posn-list 0)) ;(printf "(gen-posn-list 13) => ~a~%" (gen-posn-list 13)) ;(printf "(gen-posn-list 3) => ~a~%" (gen-posn-list 3)) (newline) (newline) (display "\n\n-------------------------------------------------------\n") (display " Problem 1(b): Connect the dots ") (display "\n-------------------------------------------------------\n") ; ; Write a function called connect-the-dots that consumes a list of ; posns (the type of list created in problem 1(a) and produces a ; scene in which each consecutive pair of posns is connected by a line, ; including a line between the last posn and the first posn on the list, ; to form a path linking all posns in the input list. The result ; produced by this function should be a scene containing a polygon ; (a plane figure that is bounded by a closed path, composed of a ; finite sequence of straight line segments (i.e., by a closed ; polygonal chain)). ; ; You should use the built-in function scene+line as part of the ; connect-the-dots function (instead of using place-image as you ; usually have when you create a scene). The scene+line function ; consumes 6 arguments: an image, the x and y coordinates of one posn ; (numbers), the x and y coordinates of another posn (numbers), and a ; color. The scene+line function draws a line of a specified color ; between the first x and y coordinate values and the second x and y ; coordinate values. The image consumed as the first argument to ; scene+line will be the result of a recursive call that eventually ; returns the base case, an empty scene. ; ; Since function connect-the-dots returns a scene, you should define ; a constant for the empty-scene, as well as for the line color. You ; can add any other constants you need to avoid writing literal values ; in your code. You are free to choose a line color. ; ; If the list of posns is < 3 in length, a polygon cannot be formed, ; so your function must include checks for lists that are < 3 posns ; long and produce a string "Error, not enough points for a polygon." ; ; Note that the function you wrote for part (a) is used in the printf ; statements for this problem, but not in the actual function body. ; ;;Constant definitions: ;;Contract: ;;Header: ;;Purpose: ;;Pre-function tests: Can test the cases that produce a string, the ;;other cases can be tested by visual inspection. ;;Function definition: ;(define connect-the-dots ;;Post-function printfs: (uncomment after writing the function) ;(printf "(connect-the-dots (gen-posn-list 8)) => ~a~%" ; (connect-the-dots (gen-posn-list 8))) ;(printf "(connect-the-dots (gen-posn-list 4)) => ~a~%" ; (connect-the-dots (gen-posn-list 4))) ;(printf "(connect-the-dots (gen-posn-list 0)) => ~a~%" ; (connect-the-dots (gen-posn-list 0))) (newline) (newline) (display "\n\n--------------------------------------\n") (display "Problem 2: Creating a list of posn pairs") (display "\n--------------------------------------\n") ; ; This problem is not related to the functions you wrote in problem ; 1, but it will help prepare you for homework assignment 6. ; ; A list of posn pairs is either ; 1. empty, or ; 2. it's a (cons (list p q) lopp), where p and q are posns and lopp is ; a list of posn pairs. ; ; Write a function called make-endpoints, that consumes 3 numbers, ; n, width, and depth, and creates a list of n lists that each contain ; 2 posns. The posn-x field of each posn should be a random number ; between 1 and width, and the posn-y field should be a random number ; between 1 and depth. ; ; Each inner list will contain 2 posns. ; ; Suppose n=5, width=800 and depth=200. The following invocations of ; make-endpoints could produce a list like the one shown below: ; ; > (make-endpoints 3 800 200) ; ((#(struct:posn 474 23) #(struct:posn 496 120)) ; (#(struct:posn 660 415)(#(struct:posn 474 23)) ; (#(struct:posn 96 40) #(struct:posn 660 115)))) ; ;;Contract: ;;Header: ;;Purpose: ;;Pre-function tests: Can be done to test length of list. Contents ;; of posns are set at random, so those can't be ;; easily tested. ;;Function definition: ;(define make-endpoints ;;Post-function printfs: (uncomment after writing the function) ;(printf "(make-endpoints 9 200 200) => ~a~%" (make-endpoints 9 200 200)) ;(printf "(make-endpoints 3 400 200) => ~a~%" (make-endpoints 3 400 200)) ;(printf "(make-endpoints 0 200 200) => ~a~%" (make-endpoints 0 200 200)) (newline) (newline)