next up previous
Next: 4 xlispstat Up: 3 例題:Xwindow操作機能の追加 Previous: 3.3 マウス操作イベントをとる例

3.4 C 曲線の表示

以上の拡張を行うと以下のリスプ関数を利用できるようになる.
     (GRAPH-INIT)
     (GRAPH-MOVETO x y)
     (GRAPH-LINETO x y)
     (GRAPH-CIRCLE x y radius)
     (GRAPH-CLOSE)
     (GRAPH-CLEAR)
     (GRAPH-GETPIXEL x y)
     (GRAPH-PUTPIXEL x y value)
graph-initにより初期化をし,graph-movetoでペンを移動し,graph-linetoで線 を描くという単純なモデルである. Xwindow環境でmakeすればこれらの関数を使うことができるようになる. C カーブは以下のように可能である.
(setq pi/4 (/ 3.1415926 4))
(setq 1/sqrt2 (/ 1.0 (sqrt 2.0)))
(defun float-vector (&rest l) (mapcar #'float l))
(defun point (x y) (float-vector x y))
(defun x (p) (car p))
(defun y (p) (cadr p))
(defun v+ (p1 p0)
  (point (+ (x p1) (x p0)) (+ (y p1) (y p0))))
(defun v- (p1 p0)
  (point (- (x p1) (x p0)) (- (y p1) (y p0))))
(defun scale (s v) (point (* (x v) s) (* (y v) s)))
(defun distance (p0 p1)
  (let* ((dv (v- p1 p0)) (dx (x dv)) (dy (y dv)))
        (sqrt (+ (* dx dx) (* dy dy)))))
(defun rotate (v rad)
  (let ((x (x v)) (y (y v)))
        (point (+ (* (cos rad) x) (* (sin rad) y))
                (- (* (cos rad) y) (* (sin rad) x)))))
;;;
(defun draw-line (p0 p1)
  (graph-moveto (x p0) (y p0))
  (graph-lineto (x p1) (y p1)))
;;;
;;;     C curve
;;;
(defun draw-ccurve (&optional
                    (p0 (point 190 300))
                    (p1 (point 450 300))
                    (min 20))
  (cond
   ((< (distance p0 p1) min) (draw-line p0 p1))
   (t (let ((p2 (v+ p0
                   (scale 1/sqrt2
                          (rotate (v- p1 p0) pi/4)))))
        (draw-ccurve p0 p2 min)
        (draw-ccurve p2 p1 min)))))
curve.lspファイルには,これらの関数定義がなされている.
~/lecture/soft3/note03/xlisp/xlisp2.1x> xlisp
XLISP version 2.1, Copyright (c) 1989, by David Betz
> (load "curve.lsp")
; loading "curve.lsp"
try (graph-init) and (draw-ccurve)
 (graph-clear) , (draw-dragon)
T
> (graph-init)
__CYGWIN__ defined
T
> (draw-ccurve)
T
> (graph-clear)
T
> (draw-dragon)
T
> (exit)
図 6: C カーブ
\includegraphics[width=7.0cm]{/home/inaba/eps/lecture/fig/ccurvefine.eps}


generated through LaTeX2HTML. M.Inaba 平成18年5月7日