(defun left(l0)
   (if (null (cadr l0))
      (list (car l0) (list Ai nil nil) (caddr l0))
      (progn
         (if (> An (caaadr l0))
            (list (car l0) (right (cadr l0)) (caddr l0))
            (list (car l0) (left (cadr l0)) (caddr l0))
         )
      )
   )
)
(defun right(l0)
   (if (null (caddr l0))
      (list (car l0) (cadr l0) (list Ai nil nil))
      (progn
         (if (> An (car (caaddr l0)))
            (list (car l0) (cadr l0) (right (caddr l0)))
            (list (car l0) (cadr l0) (left (caddr l0)))
         )
      )
   )
)
(defun pro(l0)
   (if (not (null (cadr l0)))
      (pro (cadr l0))
   )
   (setq c (car l0))
   (setq C1 (car c))
   (setq C2 (cadr c))
   (if (= C1 (car (last b)))
      (if (or (and G (> C2 (cadr (last b))))
              (and (null G) (< C2 (cadr (last b)))))
          (setq b (subst c (last b) b))
      )
      (setq b (append b (list c)))
   )
   (if (not (null (caddr l0)))
      (pro (caddr l0))
   )
)
(defun so(bb)
   (setq l0 (list (car bb) nil nil))
   (setq N (length bb))
   (while (> N 1)
      (setq N (1- N))
      (setq Ai (nth N bb))
      (setq An (car Ai))
      (if (> An (caar l0))
         (setq l0 (right l0))
         (setq l0 (left l0))
      )
   )
   (setq b (list nil))
   (pro l0)
   (setq b (cdr b))
)
(defun sots(b)
   (setq K2 t)
   (setq N (length b))
   (setq Ts1 (* 1.5 Ts) ll (list nil))
   (while (> N 1)
      (setq N (1- N))
      (setq K (nth N b) K1 (nth (1- N) b))
      (setq Ct (car K))
      (if (> Ts1 (- Ct (car K1)))
         (progn
            (setq HH (list K) Ct (- Ct Ts1))
            (setq N (1- N))
            (if (= N 0)
               (setq HH (append HH (list (nth N b))) K2 nil)
               (progn
                  (while (and K2 (> Ts1 (- Ct (car (nth (1- N) b)))))
                     (setq HH (append HH (list (nth N b))) Ct (- Ct Ts1))
                     (setq N (1- N))
                     (if (= N 0)
                        (setq K2 nil)
                     )
                  )
                  (setq HH (append HH (list (nth N b))) Ct (- Ct Ts1))
               )
            )
            (setq ll (append ll (list HH)))
         )
         (setq ll (append ll (list (nth N b))))
      )
   )
   (if K2 (setq ll (append ll (list (car b)))))
   (setq ll (cdr ll))
)
(defun maxii(K1 K)
   (setq Cx (cadar K1) Ci Cx)
   (while (> K 0)
      (setq K (1- K))
      (setq K2 (cadr (nth K K1)))
      (if (<= Cx K2) (setq Cx K2))
      (if (> Ci K2) (setq Ci K2))
   )
   (list Ci Cx)
)
(defun hdraw()
   (setq H (rtos (abs (- Pta (car Po))) 2 Ac))
   (if (< L Ts) (setq Lg (/ L 4.))
       (setq Lg (/ Ts 3.))
   )
   (if (> PHd Ptd)
      (command "line" (list Pta (+ Ptd Lg))
                      (list Pta (- PHd Lg)) ""
               "text" (list (+ Pta (/ Ts 3.)) PHd)
                Ts "90" H
      )
      (Command "line" (list Pta (- Ptd Lg))
                      (list Pta (+ PHd Lg)) ""
               "text" "r" (list (+ Pta (/ Ts 3.)) PHd)
               Ts "90" H
      )
   )
)
(defun vdraw()
   (setq V (rtos (abs (- Ptd (cadr Po))) 2 Ac))
   (if (< L Ts) (setq Lg (/ L 4.))
        (setq Lg (/ Ts 3.))
   )
   (if (> PVd Pta)
      (command "line" (list (+ Pta Lg) Ptd)
                      (list (- PVd Lg) Ptd) ""
               "text" (list PVd (- Ptd (/ Ts 3.)))
               Ts "0" V
      )
      (Command "line" (list (- Pta Lg) Ptd)
                      (list (+ PVd Lg) Ptd) ""
               "text" "R" (list PVd (- Ptd (/ Ts 3.)))
               Ts "0" V
      )
   )
)
(defun cdim(ll)
   (setq N (length ll))
   (while (> N 0)
      (setq N (1- N))
      (setq K1 (nth N ll))
      (if (atom (car K1))
         (progn
            (setq Pta (car K1) Ptd (cadr K1))
            (setq L (abs (- PHd Ptd)))
            (hdraw)
         )
         (progn
            (setq M (length K1))
            (setq PHt (car K1))
            (setq Pta (car Pht) Ptd (cadr Pht))
            (setq Cix (maxii K1 M))
            (setq Ci (car Cix) Cx (cadr Cix) Pta1 Pta)
            (if (> PHd Ptd)
               (setq L (abs (- PHd Cx)))
               (setq L (abs (- PHd Ci)))
            )
            (hdraw)
            (setq L (- L Lg Lg) I 1)
            (setq Phg (- PHd Lg) Pgh (+ PHd Lg))
            (setq Esp (/ L (float M)))
            (while (> M I)
               (setq PHt (nth I K1) Ie (* I Esp))
               (setq Pta (car Pht) Ptd (cadr Pht))
               (setq Pti (- Pta1 (* Ts1 I)))
               (setq H (rtos (abs (- Pta (car Po))) 2 Ac))
               (if (> PHd Ptd)
                  (command "line" (list Pta (+ Ptd Lg))
                            (list Pta (- Phg Ie))
                            (list Pti (- Phg Ie))
                            (list Pti Phg) ""
                            "text" (list (+ Pti (/ Ts 3.)) PHd)
                            Ts "90" H
                  )
                  (Command "line" (list Pta (- Ptd Lg))
                            (list Pta (+ Pgh Ie))
                            (list Pti (+ Pgh Ie))
                            (list Pti Pgh) ""
                            "text" "r"
                            (list (+ Pti (/ Ts 3.)) PHd)
                            Ts "90" H
                  )
               )
               (setq I (1+ I))
            )
         )
      )
   )
)
(defun cdim1(ll)
   (setq N (length ll))
   (while (> N 0)
      (setq N (1- N))
      (setq K1 (nth N ll))
      (if (atom (car K1))
         (progn
            (setq Ptd (car K1) Pta (cadr K1))
            (setq L (abs (- PVd Pta)))
            (vdraw)
         )
         (progn
            (setq M (length K1))
            (setq PVt (car K1))
            (setq Ptd (car PVt) Pta (cadr PVt))
            (setq Cix (maxii K1 M))
            (setq Ci (car Cix) Cx (cadr Cix) Ptd1 Ptd)
            (if (> PVd Pta)
               (setq L (abs (- PVd Cx)))
               (setq L (abs (- PVd Ci)))
            )
            (vdraw)
            (setq L (- L Lg Lg) I 1)
            (setq Phg (- PVd Lg) Pgh (+ PVd Lg))
            (setq Esp (/ L (float M)))
            (while (> M I)
               (setq PVt (nth I K1) Ie (* I Esp))
               (setq Ptd (car PVt) Pta (cadr PVt))
               (setq Pti (- Ptd1 (* Ts1 I)))
               (setq V (rtos (abs (- Ptd (cadr Po))) 2 Ac))
               (if (> PVd Pta)
                  (command "line" (list (+ Pta Lg) Ptd)
                            (list (- Phg Ie) Ptd)
                            (list (- Phg Ie) Pti)
                            (list Phg Pti) ""
                            "text" (list PVd (- Pti (/ Ts 3.)))
                            Ts "0" V
                  )
                  (Command "line" (list (- Pta Lg) Ptd)
                            (list (+ Pgh Ie) Ptd)
                            (list (+ Pgh Ie) Pti)
                            (list Pgh Pti) ""
                            "text" "r"
                            (list PVd (- Pti (/ Ts 3.)))
                            Ts "0" V
                  )
               )
               (setq I (1+ I))
            )
         )
      )
   )
)
(defun chdim(bb)
   (if (> PHd (cadadr bb)) (setq G t) (setq G nil))
   (cdim (sots (so bb)))
)
(defun cvdim(bb)
   (if (> PVd (cadadr bb)) (setq G t) (setq G nil))
   (cdim1 (sots (so bb)))
)
(defun vdim()
   (cond
      ((null Po) (*error* "Please input orginal Point !!!"))
      ((null Pvd) (*error* "Please input horizontal level !!!"))
      ((null Ts) (*error* "Please input text size !!!"))
      ((null Ac) (*error* "Please input Accurcy !!!"))
      (t (while (setq PVt (getpoint "\nV dim:"))
            (setq Pta (car PVt) Ptd (cadr PVt))
            (setq L (abs (- PVd Pta)))
            (vdraw)
         )
      )
   )
)
(defun hdim()
   (cond
      ((null Po) (*error* "Please input orginal Point !!!"))
      ((null Phd) (*error* "Please input vertical level !!!"))
      ((null Ts) (*error* "Please input text size !!!"))
      ((null Ac) (*error* "Please input Accurcy !!!"))
      (t (while (setq PHt (getpoint "\nH dim:"))
            (setq Pta (car PHt) Ptd (cadr PHt))
            (setq L (abs (- PHd Ptd)))
            (hdraw)
         )
      )
   )
)
