;;; ======================================================= ;;; CMPU-101, Spring 2013 ;;; Assignment 6 ;;; ======================================================= (display "\n CS101 Assignment 6, Spring 2013") (display "\n Please type your name here\n\n") ;; IMPORTANT! MAKE SURE YOUR LINES OF CODE DO NOT WRAP WHEN ;; YOUR WINDOW IS ABOUT 80 COLUMNS ACROSS. (require 2htdp/image) (require 2htdp/universe) (display "\n\n-----------------------------------\n") (display "Problem 1: Drawing a grid") (display " \n-----------------------------------\n") ; This is a 3 part problem that will produce a square ; grid with dimensions 800 X 800, like that shown below. ; ; NOTE: The first 2 parts of this problem are like the ; last problem on lab 8. . ; ; A list of posn pairs is either ; 1. empty, or ; 2. it's a (cons posnpr lopp), where posnpr is a list containing 2 ; posns and lopp is a list of posn pairs. ; (display "\n\n -------------------------------------------------------\n") (display " Problem 1(a): Making endpoints of horizontal grid lines") (display "\n -------------------------------------------------------\n") ; ; Write a function called make-hor-endpoints, that consumes 2 numbers, a ; side-length and spacing, and creates a list of lists. The contract, ; header, and purpose for this function are given below. ; ; Each inner list will contain 2 posns. These posns represent the ends of ; a line that will cross the scene from left to right. ; ; Suppose side-length is 800 and spacing is 20. The first inner list will ; contain posns (0,20) and (800,20), the second inner list will contain ; posns (0,40) and (800,40), the third inner list will contain (0,60) and ; (800, 60) and so on. It may help you to sketch the empty scene and ; the endpoints before writing the function. With the given values of ; side-length and spacing, the x coordinate in each posn pair will be 0 ; and 800, and the y coordinate will be in increments of 20 pixels, from ; 20 to 800. ; ;;Contract: ;;Header: ;;Purpose: ;;Pre-function tests to uncomment: ;;(this is a test with a smaller side-length and spacing) ;(check-expect (make-hor-endpoints 40 10) ; (list (list (make-posn 0 10) (make-posn 40 10)) ; (list (make-posn 0 20) (make-posn 40 20)) ; (list (make-posn 0 30) (make-posn 40 30)))) ;;Function definition: ;;Post-function printfs to uncomment: ;(printf "(make-hor-endpoints 100 20) => ~a~%" (make-hor-endpoints 100 20)) ;(printf "(make-hor-endpoints 20 10) => ~a~%" (make-hor-endpoints 20 10)) (newline) (newline) (display "\n\n -------------------------------------------------------\n") (display " Problem 1(b): Making endpoints of vertical grid lines") (display "\n -------------------------------------------------------\n") ; ; Write a function called make-vert-endpoints, that consumes 2 numbers, a ; side-length and spacing, and creates a list of lists. The contract, ; header, and purpose for this function are given below. ; ; Each inner list will contain 2 posns. These posns represent the ends of ; a line that will cross the scene from top to bottom. ; ; Suppose side-length is 800 and spacing is 20. The first inner list will ; contain posns (20,0) and (20,800), the second inner list will contain ; posns (40,0) and (40,800), the third inner list will contain (60,0) and ; (60,800) and so on. It may help you to sketch the empty scene and ; the endpoints before writing the function. The y coordinate in each posn ; pair will be 0 and 800, and the x coordinate will be in increments of ; 20 pixels, from 20 to 800. ; ;;Contract: ;;Header: ;;Purpose: ;;Pre-function tests to uncomment: ;(check-expect (make-vert-endpoints 40 10) ; (list (list (make-posn 10 0) (make-posn 10 40)) ; (list (make-posn 20 0) (make-posn 20 40)) ; (list (make-posn 30 0) (make-posn 30 40)))) ;;Function definition: ;;Post-function printfs to uncomment: ;(printf "(make-vert-endpoints 100 20) => ~a~%" (make-vert-endpoints 100 20)) ;(printf "(make-vert-endpoints 20 10) => ~a~%" (make-vert-endpoints 20 10)) (newline) (newline) (display "\n\n -------------------------------------------------------\n") (display " Problem 1(c): Drawing lines between posn pairs ") (display "\n -------------------------------------------------------\n") ; ; Write a function called draw-lines that creates a scene that shows ; the grid shown at the start of problem 2. ; ; You should use the built-in function scene+line as part of the ; draw-lines function (instead of place-image). The scene+line ; function consumes 6 arguments: an image, the x and y coordinates of ; one posn, the x and y coordinates of another posn, and a color, and ; draws a line of the specified color between one posn and the other. ; ; The draw-lines function should consume a list of posn pairs and ; produce a scene with lines between each pair of posns in the list. ; ; Since this function returns a scene, there is a constant defined ; for the empty-scene below. ; ;;Uncomment the following constants for use in your function. ;(define SIDE 800) ;; Dimensions of grid ;(define MT (empty-scene SIDE SIDE)) ;; Base case for scene ;(define CLR "black") ;; Line color ;;Contract: (draw-lines list-of-posn-pairs) -> scene (image) ;;Header: (define draw-lines (lambda (lopp) ... )) ;;Purpose: Add lines with endpoints specified by each posn pair to scene ;;Pre-function tests: By visual inspection. ;;Function definition: ;;Post-function printfs: ;(printf "(draw-lines (make-hor-endpoints 800 20)) => ~%") ;(draw-lines (make-hor-endpoints 800 20)) ;(printf "(draw-lines (make-vert-endpoints 800 20)) => ~%") ;(draw-lines (make-vert-endpoints 800 20)) ;(define ENDPTS (append (make-hor-endpoints 800 20) ; (make-vert-endpoints 800 20))) ;(printf "(draw-lines ENDPTS) => ~%") ;(draw-lines ENDPTS) (newline) (newline) (display "\n\n----------------------------------------\n") (display "Problem 2: Moving a circle across a grid") (display " \n----------------------------------------\n") ; ; Write a simulation of a small red circle moving left to right ; across a grid. ; ; Uncomment the constants defined below and use them in a big-bang ; simulation. ; ;; CONSTANTS USED IN SIMULATION: ;(define RADIUS 10) ;(define DIAMETER (* RADIUS 2)) ;(define MODE "solid") ;(define COLOR "red") ;(define SEGMENT (circle RADIUS MODE COLOR)) ;(define GRID ; (draw-lines ; (append (make-hor-endpoints SIDE DIAMETER) ; (make-vert-endpoints SIDE DIAMETER)))) ; ; For this simulation, the world state w-s will be the current ; x coordinate of a red circle on a grid. The big-bang function ; will have 2 clauses, the on-draw and on-tick clauses. ; ; For the on-draw clause, you should complete a function called ; draw-segment that consumes a positive natural number called ; w-s. This function should place the SEGMENT image at (x,y) ; coordinate (w-s, DIAMETER) on top of the GRID scene created ; above. ; ; For the on-tick clause, you should complete a function called ; go-left that consumes a positive natural number called w-s and ; increases w-s by DIAMETER each time it is called. ; ;;Contract: (draw-segment number) -> scene (image) ;;Header: (define draw-segment (lambda (w-s) ...)) ;;Purpose: Render result of placing SEGMENT at position (w-s,DIAMETER) ;; on the background GRID. ;;Pre-function tests: By visual inspection. ;;Function definition: ;(define draw-segment ; (lambda (w-s) (newline) (newline) ;;Contract: (go-right number) -> number ;;Header: (define go-right (lambda (w-s) ...)) ;;Purpose: Add DIAMETER to w-s. ;;Pre-function tests: ;(check-expect (go-right 20) 40) ;(check-expect (go-right 40) 60) ;;Function definition: ;(define go-right ; (lambda (w-s) (newline) (newline) ;; Lastly, uncomment the following invocation of the big-bang ;; function. When you press Run, you should see a red circle ;; moving from left to right across a grid. ;(big-bang DIAMETER ; (on-draw draw-segment) ; (on-tick go-right .25)) ;; The clock will tick every quarter sec. (newline) (newline)