;;; This file contains various definitions and functions for HW2. ;;; It defines structures for lines, vertices, polygons, and scenes. ;;; A scene is a collection of polygons, a start, and a goal state. ;;; ;;; After loading this file, you should be able to print the contents of scene-3.22 ;;; [1]> scene-3.22 ;;; ;;; An important function is vertices-visible-from. Pass it a vertex and the scene ;;; and it returns all vertices visible from that vertex. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Defstructure Definition ;;; Don't worry to much about these (defmacro defstructure (type-and-args &rest slots) "This is just like DEFSTRUCT, except it keeps track of :include types, for the benefit of METHOD-FOR, and it makes printing go through PRINT-STRUCTURE." (if (atom type-and-args) (setf type-and-args (list type-and-args))) (let* ((type (first type-and-args)) (args (rest type-and-args)) (supertype (or (second (assoc ':include args)) 'structure)) (print-fn (if (null (assoc ':print-function args)) '((:print-function (lambda (x s d) (declare (ignore d)) (print-structure x s))))))) `(progn (setf (get ',type ':supertype) ',supertype) (defstruct (,type ,@print-fn ,@args) ,@slots)))) (defmethod print-structure ((structure t) stream) "Print a structure. You can specialize this function. It will be called to print anything defined with DEFSTRUCTURE." ;(format stream "#" (type-of structure)) (cond ( (equal (type-of structure) 'vertex) (format stream "<~A ~A>" (type-of structure) (vertex-xy structure))) ( t (format stream "<~A>" (type-of structure))) ) ) ;;; End of defstructure stuff ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;; normal defstructs ;;;;;;; ;;; Note that a line is a list of two numbers (12 55) (defstruct line xy1 xy2) ;;; Polygon ;;; vertices is the list of vertices ;;; n is the number of vertices ;;; if needed, use create-polygon to create new polygons. (defstruct polygon vertices n) ;;; Vertex (defstructure vertex xy ;; the xy point for the vertex c-neighbor ;; neighbour in clockwise direction a-neighbor ;; neighbour in anti-clockwise direction visible ;; list of vertices visible from here ) ;;; scene (defstruct scene polygons ; polygons comprising scene start ; vertex for start goal ; vertex for goal ) ;;;; Line util functions ;;;;;; ;;; returns the x coord. ;;; argument lxy is the xy pair (x y) (defun xy-x (lxy) (first lxy)) ;;; returns the y coord. ;;; argument lxy is the xy pair (x y) (defun xy-y (lxy) (first (last lxy))) (defun intersects (l1 l2) ;;; l1 is line ab; l2 is line cd ;;; assume the lines cross at alpha a + (1-alpha) b, ;;; also known as beta c + (1-beta) d ;;; line segments intersect if 0