2

我需要一点帮助。我被困在一个我需要做的功能上。

新多边形是用make-polygon没有参数的函数创建的,并返回空的点列表。items您可以通过函数和确定和更改点列表set-items。我不知道如何编写set-items哪个应该有两个参数(多边形和点列表)并返回多边形(形式((1 1) (2 1) (5 2) :black))或类似的点列表)。它需要用make-point.

"POINT"
(defvar *point*)
(defun make-point ()
  (setf *point* (list (list 0 0) :black)))
(defun x (point)
  (caar point))
(defun y (point)
  (cadar point))
(defun set-x (point new-x)
  (setf (caar point) new-x) #| musi byt x point |#
  point)
(defun set-y (point new-y)
  (setf (cadar point) new-y) #| musi byt y point |#
  point)
"POLYGON"
(defvar *polygon*)
(defun make-polygon () 
   (setf *polygon* (list nil :black))) 
(defun items (polygon) 
   (car polygon))
4

1 回答 1

2

您可以将其定义为类似于set-x,set-y函数:

(defun set-items (polygon list-of-points)
  (setf (car polygon) list-of-points))

请注意,这将返回点列表。如果它应该返回多边形,它可以修改如下:

(defun set-items (polygon list-of-points)
  (setf (car polygon) list-of-points)
  polygon)

让我们试试它是否有效:

(let ((p1 (make-point))
      (p2 (make-point))
      (p3 (make-point))
      (poly (make-polygon)))
  (set-x p1 1)
  (set-y p1 1)
  (set-x p2 2)
  (set-y p2 1)
  (set-x p3 5)
  (set-y p3 2)
  (set-items poly (list p1 p2 p3))
  (loop for p in (items poly)
    do (format t "(~a, ~a) ~%" (x p) (y p))))
(1, 1) 
(2, 1) 
(5, 2) 
NIL
于 2018-10-01T15:40:52.883 回答