;; avg2: num, num --> num ;; (define (avg2 a b) (/ (+ a b) 2)) ;; midpoint: posn, posn --> posn ;; (define (midpoint p1 p2) (make-posn (avg2 (posn-x p1) (posn-x p2)) (avg2 (posn-y p1) (posn-y p2)))) ;; manhattan-distance: posn, posn --> number ;; N.B. The Manhattan distance is the ;; distance if you can travel n/s or e/w, but not diagonal. ;; (define (manhattan-distance p q) (+ (abs (- (posn-x p) (posn-x q))) (abs (- (posn-y p) (posn-y q))))) ;; too-close?: posn,posn,posn --> boolean ;; Return true if any of p1,p2,p3 near each other. ;; ;; NB You can change the constant close-enough, ;; to tweak too-close?'s threshold. ;; (define close-enough 2) (define (too-close? p1 p2 p3) (or (<= (manhattan-distance p1 p2) close-enough) (<= (manhattan-distance p2 p3) close-enough) (<= (manhattan-distance p3 p1) close-enough))) ;;; A polyline is a list of piecewise connected line segments. ;;; We represent a polyline as a list of posns (the vertices). ;; draw-polyline: list-of-posn --> boolean ;; draw-polyline-color: list-of-posn, color --> boolean ;; (define (draw-polyline pts) (draw-polyline-color pts BLACK)) (define (draw-polyline-color pts color) (cond [(empty? pts) false] ; A polyline of no points? Absurd! [(empty? (rest pts)) (draw-solid-line (first pts) (first pts))] [else (begin (draw-solid-line (first pts) (second pts) color) (draw-polyline-color (rest pts) color))])) ;; draw-triangle: posn posn posn --> true ;; (define (draw-triangle a b c) (draw-polyline-color (list a b c a) BLUE))