The dashed line option is used when an array of t and nil values are provided.
You must provide your own plot function, making this highly portable as it separates the algorithm from the graphics lib.
Here is the algorithm I'll post the error below:
Code: Select all
(defun draw-line-dashed(x y x2 y2 plot-function &optional pattern)
(let* ((dx (abs (- x2 x)))
(dy (abs (- y2 y)))
(sx (if (< x x2) 1 -1))
(sy (if (< y y2) 1 -1))
(err (- dx dy))
(e2 nil)
(p-len (length pattern))
(p-iter 0))
(loop
(if (= p-iter p-len)
(setf p-iter 0))
(if (> p-len 0)
(if (aref pattern p-iter)
(funcall plot-function x y))
(funcall plot-function x y))
(incf p-iter)
(and (= x x2) (= y y2) (return-from draw-line-dashed))
(setf e2 (* 2 err))
(when (> e2 (* -1 dy))
(setf err (- err dy))
(setf x (+ x sx)))
(when (and (= x x2) (= y y2))
(if (> p-len 0)
(if (aref pattern p-iter)
(funcall plot-function x y))
(funcall plot-function x y))
(return-from draw-line-dashed))
(when (< e2 dx)
(setf err (+ err dx))
(setf y (+ y sy))))))
If just the pattern is provided with x2 and y2 being both numeric constants and a dash pattern it works.
On the other hand, you can seem to be able to use (random number) on x2 and y2 without a dash pattern and it works....
For example this line will fail:
Assume *pixel-buffer* is a 2d array of 0's or 1's.
Code: Select all
(defparameter *pixel-buffer* (make-array '(480 480) :initial-element 0))
(defun plot(x y)
(setf (aref *pixel-buffer* x y) 1))
(defparameter *dash* (make-array 4 :initial-contents '(t t nil nil)))
(dotimes (i 100)
(draw-line-dashed 240 240
(round (+ 240 (* (cos (* i .0628)) 230)))
(round (+ 240 (* (sin (* i .0628)) 230)))
#'plot *dash*))