; hw7#1 ; CS 381 homework 7 problem 1 ; Jacob Lundberg ;; ;; Problem One ;; ; MAKE-POINT creates a list ordered correctly to represent a point (x y). (defun MAKE-POINT (&key (x 0) (y 0)) ; If x any y are not a valid locus we will fail and return nil. (if (and (numberp x) (numberp y)) ; valid numbers so make a list (list x y) ; invalid locus so return nil nil ) ) ; POINT-P is a predicate to determine if a list has the correct form to be a point. ; That is, a point as defined by MAKE-POINT, which creates two dimentsional points. (defun POINT-P (point1) (cond ; nothing there ((null point1) nil) ; only one dimension ((null (cdr point1)) nil) ; too many dimensions ((not (null (cddr point1))) nil) ; first dimension is not a number ((not (numberp (car point1))) nil) ; second dimension is not a number ((not (numberp (cadr point1))) nil) ; if we survive all the tests above, it is a point (T) ) ) ; POINT-X and POINT-Y are accessors for a point made with MAKE-POINT. (defun POINT-X (point1) (if (POINT-P point1) (car point1) nil)) (defun POINT-Y (point1) (if (POINT-P point1) (cadr point1) nil)) ; MAKE-SEGMENT creates a list of two points as created by MAKE-POINT, thus defining a line segment. (defun MAKE-SEGMENT (&key (point1 '(0 0)) (point2 '(0 0))) ; if point1 and point2 are not valid points then fail and return nil (if (and (POINT-P point1) (POINT-P point2)) ; we have valid points (list point1 point2) ; we have invalid points nil ) ) ; SEGMENT-P is a predicate to determine if a list has the correct form to be a segment. ; That is, a segment as defined by MAKE-SEGMENT, which creates two dimentsional segments. (defun SEGMENT-P (seg1) (cond ; nothing there ((null seg1) nil) ; only one point ((null (cdr seg1)) nil) ; too many points ((not (null (cddr seg1))) nil) ; first point isn't ((not (POINT-P (car seg1))) nil) ; second point isn't ((not (POINT-P (cadr seg1))) nil) ; if we survive all the tests above, it is a segment (T) ) ) ; SEGMENT-POINT1 and SEGMENT-POINT2 are accessors for a segment made with MAKE-SEGMENT. (defun SEGMENT-POINT1 (seg1) (if (SEGMENT-P seg1) (car seg1) nil)) (defun SEGMENT-POINT2 (seg2) (if (SEGMENT-P seg2) (cadr seg2) nil)) ; MIDPOINT returns the midpoint of a line segment created using MAKE-SEGMENT. ; Care has been taken to mimic structure access syntax here. (defun MIDPOINT (seg1) (MAKE-POINT ; find center X value :x (/ (+ (POINT-X (SEGMENT-POINT1 seg1)) (POINT-X (SEGMENT-POINT2 seg1))) 2) ; find center Y value :y (/ (+ (POINT-Y (SEGMENT-POINT1 seg1)) (POINT-Y (SEGMENT-POINT2 seg1))) 2) ) )