请LISP高手帮忙注释此代码
(defun recover_snap ( reactor_object lisp_list / )(setvar "osmode" $$mpt_osmode)
(vlr-remove $$mptReactor)
)
(defun mpt ( / a b mx my mz )
(while (not (setq a (getpoint "\nFirst Point : "))))
(while (not (setq b (getpoint a "\nSecond Point: "))))
(setq mx (/ (+ (car b) (car a)) 2.0))
(setq my (/ (+ (cadr b) (cadr a)) 2.0))
(setq mz (/ (+ (caddr b) (caddr a)) 2.0))
(setq $$mpt_osmode (getvar "osmode"))
(setvar "osmode" 0)
(setq $$mptReactor (vlr-lisp-reactor data '((:vlr-lispEnded . recover_snap))))
(setq pt (list mx my mz))
)
;;******************************************************
;;预定义一些函数
;;定义平方函数
(defun sqr (x)
(* x x)
)
;;定义求一元二次方程的函数
(defun roots (a b c / t1 t2 x1 x2)
(setq a (float a) b (float b) c (float c))
(if (/= a 0)
(progn
(setq t1 (- (* b b) (* 4 a c)))
(if (>= t1 0.0)
(progn
(setq t2 (sqrt t1))
(setq x1 (/ (- t2 b) (* 2 a)))
(setq x2 (/ (- 0.0 t2 b) (* 2 a)))
(list x1 x2)
)
(progn
(alert "\n根是复数.")
(setvar "cmdecho" oce)
(quit)
)
)
)
(progn
(if (/= b 0)
(setq x1 (list (/ (- c) b) (/ (- c) b)))
(progn
(if (/= c 0)
(progn
(alert "\n无解")
(setvar "cmdecho" oce)
(quit)
)
(progn
(princ "\n无穷多个解")
(list 0.0 1.0)
)
)
)
)
)
)
)
;;;*************************************
;;;取点,并进行座标转换以及判断和坐标交换
(defun C:aaa (/ p1 p2 p3 p4 pch pm dm pm1 pm2 k1 k2 k3 k b1 b2 b3 oldmode oce xxx yyy zzz
rmin rmax short1 long1 short2 long2 intp do p23 0.5h yy kk bb sx1 sx2 sy1 sy2)
;;(defun C:aaa ()
(graphscr)
(setq oldmode (getvar "osmode"))
(setq oce (getvar "cmdecho"))
(setvar "cmdecho" 0)
(VL-LOAD-COM)
(setq AcadObject (vlax-get-acad-object)
AcadDocument (vla-get-ActiveDocument Acadobject)
mSpace (vla-get-ModelSpace Acaddocument)
)
;;取点,并进行座标转换-----------------
(setq p1 (getpoint "请输入第一点:\n"))
(setq p2 (getpoint "请输入第二点:\n"))
(setq p3 (getpoint "请输入第三点:\n"))
(setq p4 (getpoint "请输入第四点:\n"))
;;car: Returns the first element of a list
;;cadr:Returns the second element of a list
(setq p1 (list (car p1) (cadr p1)))
(setq p2 (list (car p2) (cadr p2)))
(setq p3 (list (car p3) (cadr p3)))
(setq p4 (list (car p4) (cadr p4)))
;;定义两矢量之差----------------------
(defun sub (x y)
(list (- (car x) (car y)) (- (cadr x) (cadr y)))
)
;;定义矢量之叉积,即二阶行列式之值-----
(defun det2 (p1 p2)
(- (* (car p1) (cadr p2)) (* (car p2) (cadr p1)))
)
;;定义三点的行列式,即三点之倍面积-----
(defun det (p1 p2 p3)
(+ (det2 p1 p2) (det2 p2 p3) (det2 p3 p1))
)
;;定义没有方向的夹角------------------
(defun ang (p1 p2 p3 / x)
(setq x (abs (- (angle p1 p3) (angle p1 p2))))
(if (< (abs (sin x)) 1e-8) (setq x 0)
(progn (if (> x pi) (setq x (- (* 2 pi) x)) (setq x x) ) ) ) )
;;判断点是否在某三点形成的三角形内----
(defun inner (p1 p2 p3 p4 / x)
(setq x (- (* 2 pi) (+ (ang p1 p2 p3) (ang p1 p3 p4) (ang p1 p4 p2))))
(if (< (abs x) 1e-8) (setq x T) ))
续
;;------------------------------------;;进入程序主段------------------------
(cond ( (= 0 (* (ang p1 p2 p3) (ang p2 p3 p4) (ang p3 p4 p1) (ang p4 p1 p2)))
(progn (alert "有三点在同一条直线上\n请重新输入") (setvar "cmdecho" oce) (princ)))
( (or nil (inner p1 p2 p3 p4) (inner p2 p3 p4 p1) (inner p3 p4 p1 p2) (inner p4 p1 p2 p3))
(progn (alert "这是一个凹四边形\n无解") (setvar "cmdecho" oce) (princ)))
(t
(progn
;;判断3、4点是否在同一边,否则交换2、3
(defun same (p1 p2 p3 p4)
(if (< 0 (* (det p1 p2 p3) (det p1 p2 p4))) (list p2 p3) (list p3 p2)))
;;交换坐标,使之成为顺序排列----------
(setq pch (same p1 p2 p3 p4))
(setq p2 (car pch) p3 (cadr pch))
(setq pch (same p1 p4 p3 p2))
(setq p4 (car pch) p3 (cadr pch))
;;取中点为原点------------------------
(setq pm (midp (midp p1 p3) (midp p2 p4)))
(setq pm (list (car pm) (cadr pm)))
(setq p1 (sub p1 pm) p2 (sub p2 pm) p3 (sub p3 pm) p4 (sub p4 pm))
;;定义直线斜率------------------------
(defun tank (p1 p2)
(/ (- (cadr p1) (cadr p2)) (- (car p1) (car p2))))
;;定义旋转90度函数--------------------
(defun rot90 (x) (list (- (cadr x)) (car x)))
(defun rot-90 (x) (list (cadr x) (- (car x))))
;;判断是不是平行四边形----------------
(setq dm (distance (midp p1 p3) (midp p2 p4)))
(if (< dm 1e-8)
(progn
(if (and (< (abs (- (ang p1 p2 p4) (/ pi 2))) 1e-8)
(or nil (< (abs (cos (angle p1 p2))) 1e-8) (< (abs (cos (angle p1 p4))) 1e-8)))
(progn
(princ "这是一个平行xy轴线的长方形")
(setvar "osmode" 0)
(command ".ucs" "O" pm)
(command ".line" p1 p2 p3 p4 "C")
(command ".ellipse" "C"'(0 0) (midp p1 p2) (midp p1 p4))
(command "ucs" "P")
(setvar "osmode" oldmode)
(setvar "cmdecho" oce)
(princ)
);;判断是不是平行xy轴线的长方形----
(progn
(if (or nil (< (abs (cos (angle p1 p2))) 1e-8) (< (abs (cos (angle p1 p4))) 1e-8))
(setq p1 (rot90 p1) p2 (rot90 p2) p3 (rot90 p3) p4 (rot90 p4) yyy 1)
(setq yyy 0)
)
(setq b1 (/ (det2 p1 p2) (- (car p1) (car p2))))
(setq b2 (/ (det2 p2 p3) (- (car p2) (car p3))))
(setq k1 (tank p1 p2) k2 (tank p2 p3))
(setq kk (- (sqr k1) (sqr k2)))
(setq bb (- (sqr b1) (sqr b2)))
(if (and (< (abs (- (ang '(0 0) p1 p2) (/ pi 2))) 1e-8)
(< (abs (* (sin (angle p1 p3)) (sin (angle p2 p4)) ) ) 1e-8))
(progn
(if (< (abs (sin (angle p1 p3))) 1e-8) (setq zzz 1) (setq zzz 0))
(if (< (distance p1 p3) (distance p2 p4))
(setq rmin (/ (distance p1 p3) 2) rmax(/ (distance p2 p4) 2) xxx 0)
(setq rmin (/ (distance p2 p4) 2) rmax(/ (distance p1 p3) 2) xxx 1))
(alert "这是菱形,在这个方向有多解!\n请给出一个距离,如果距离大于半对角线长,\n将给出一个指定的内切椭圆")
(setq short1 (getdist "\n请输入一个距离:"))
(if (<= (- (sqr rmin) (sqr short1)) 1e-4)
(setq short1 (/ rmin 2)))
(setq long1 (/ (* (sqrt (- (sqr rmin) (sqr short1))) rmax) rmin) )
(if (or nil (< short1 1e-4) (< long1 1e-4) (< (/ short1 long1) 1e-4) (< (/ long1 short1) 1e-4))
(setq short1 (/ rmin 2)) )
(setq long1 (/ (* (sqrt (- (sqr rmin) (sqr short1))) rmax) rmin) )
(setvar "osmode" 0)
(command ".ucs" "O" pm)
(command ".line" p1 p2 p3 p4 "C")
(if (or nil (and (= xxx 1) (= zzz 1)) (and (= xxx 0) (= zzz 0)))
(command ".ellipse" "C" '(0 0) (polar '(0 0) 0 long1) (polar '(0 0) (/ pi 2) short1))
(command ".ellipse" "C" '(0 0) (polar '(0 0) 0 short1) (polar '(0 0) (/ pi 2) long1))
)
(setvar "osmode" oldmode)
(setvar "cmdecho" oce)
(princ)
);;判断是不是平行xy轴线的菱形---
(progn
(setq yy (/ (- (* (sqr k1) (sqr b2)) (* (sqr b1) (sqr k2)) ) kk ) )
(if (or nil (< (/ kk bb) 0) (< yy 0))
(progn (alert "平行四边形在这个方向无解") (setvar "cmdecho" oce) (princ))
(progn
(if (= yyy 1)
(setq long1 (sqrt yy) short1 (sqrt (/ bb kk))
p1 (rot-90 p1) p2 (rot-90 p2) p3 (rot-90 p3) p4 (rot-90 p4))
(setq long1 (sqrt (/ bb kk)) short1 (sqrt yy))
)
(princ "这是一个可解平行四边形")
(setvar "osmode" 0)
(command ".ucs" "O" pm)
(command ".line" p1 p2 p3 p4 "C")
(command ".ellipse" "C" '(0 0) (polar '(0 0) 0 long1) (polar '(0 0) (/ pi 2) short1))
(command "ucs" "P")
(setvar "osmode" oldmode)
(setvar "cmdecho" oce)
(princ)
)
)
)
)
)
)
)
续
(progn;;定义旋转90度函数--------------------
(setq pm1 (midp p1 p3) pm2 (midp p2 p4))
(if (< (abs (cos (angle pm1 pm2))) 1e-8)
(setq p1 (rot90 p1) p2 (rot90 p2) p3 (rot90 p3) p4 (rot90 p4) xxx 1)
(setq xxx 0)
)
;;定义重新排列点序列函数--------------
(defun rearray (p1 p2 p3 p4)
(cond ((< (abs (cos (angle p1 p2))) 1e-8) (list p2 p3 p4 p1))
((< (abs (cos (angle p2 p3))) 1e-8) (list p3 p4 p1 p2))
((< (abs (cos (angle p3 p4))) 1e-8) (list p4 p1 p2 p3))
(t(list p1 p2 p3 p4)) ) )
;;计算并交换点------------------------
(setq pch (rearray p1 p2 p3 p4))
(setq p1 (car pch) p2 (cadr pch) p3 (caddr pch) p4 (cadddr pch))
(if (< (abs (sin (- (angle p1 p2) (angle p4 p3)))) 1e-8)
(setq p1 (cadr pch) p2 (caddr pch) p3 (cadddr pch) p4 (car pch))
(princ)
)
;;判断是不是梯形----------------------
(cond ((and (< (abs (sin (- (angle p1 p4) (angle p2 p3)))) 1e-8)
(< (abs (sin (angle p1 p4))) 1e-8))
(progn
(setq pm1(midp p1 p3)pm2 (midp p2 p4))
(setq dm (midp pm1 pm2))
(setq intp (inters p1 p3 p2 p4))
(setq do (inters intp (polar intp (/ Pi 2) 1) pm1 pm2 nil))
(setq p23(inters do (polar do (/ pi 2) 1) p2p3nil))
(setq 0.5h (distance do p23))
(setq b1 (distance do (inters do p23 p1 p2 nil)))
(if (<= (- b1 0.5h) 1e-6)
(progn
(alert "在这个方向的梯形无解")
(setvar "cmdecho" oce)
(setq xxx 17)
(princ)
)
(progn
(setq b2 (distance do (midp p1 p2)))
(setq b3 (/ (* (sqrt (- (sqr b1) (sqr 0.5h))) b2) b1))
(setq long1 b3 short1 0.5h)
(if (= 1 xxx)
(progn
(setq long1 0.5h short1 b3)
(setq p1 (rot-90 p1) p2 (rot-90 p2) p3 (rot-90 p3) p4 (rot-90 p4) do (rot-90 do))
)
)
(if (or nil (< short1 1e-5) (< long1 1e-5) (< (/ short1 long1) 1e-5) (< (/ long1 short1) 1e-5))
(progn
(alert "这个方向的梯形无解")
(setvar "cmdecho" oce)
(setq xxx 18)
(princ)
)
(progn
(setvar "osmode" 0)
(command ".ucs" "O" pm)
(command ".line" p1 p2 p3 p4 "C")
(command ".ellipse" "C" do (polar do 0 long1) (polar do (/ pi 2) short1))
(command "ucs" "P")
(setvar "osmode" oldmode)
(setvar "cmdecho" oce)
(setq xxx 19)
(princ)
)
)
)
)
)
)
( (and (> (abs (- (ang (inters p1 p3 p2 p4 nil) p1 p2) (/ pi 2))) 1e-8)
(< (abs (sin (angle (inters p1 p3 p2 p4 nil) '(0 0)))) 1e-8))
(progn
(alert "斜筝形在这个方向无解")
(setvar "cmdecho" oce)
(setq xxx 19)
(princ)
)
);;对角线在X,Y轴方向的斜筝形无解
( (and (or nil (< (abs (cos (anglep2 p4))) 1e-8) (< (abs (cos (anglep1 p3))) 1e-8))
(< (abs (sin (angle (inters p1 p3 p2 p4 nil) '(0 0)))) 1e-8))
(progn
(if (< (abs (cos (anglep1 p3))) 1e-8)
(setq pch (list p1 p2 p3 p4) yyy 425
p1 (cadr pch) p2 (caddr pch)
p3 (cadddr pch) p4 (car pch))
(princ)
)
(setq pch (list p1 p2 p3 p4))
(setq intp (inters p1 p3 p2 p4))
(setq k1 (tank p1 p2))
(setq k2 (tank p3 p2))
(setq dm '(0 0))
(setq sx1 (inters dm (polar dm (/ Pi 2) 1) p1 p2 nil))
(setq sx2 (inters dm (polar dm (/ Pi 2) 1) p3 p2 nil))
(setq b1 (distance dm sx1))
(setq b2 (distance dm sx2))
(alert "正筝形在这个方向多解,请输入一段较小的距离,\n距离超出将给出一个指定的圆或椭圆")
(setq xx (getdist "\n请输入一段距离: "))
(setq b1 (+ (* k1 xx) b1)) (setq b2 (+ (* k2 xx) b2))
(if (or nil (< (/ (- (sqr b1) (sqr b2)) (- (sqr k1) (sqr k2)) ) 0)
(<(- (sqr b1) (* (sqr k1) (sqr (/ (- (sqr b1) (sqr b2)) (- (sqr k1) (sqr k2)) ) ) ) ) 0) )
(progn
(setq sy1 (- (/ (ang p2 p1 p3) 2) (ang p2 intp p1)) )
(setq sy2 (* (distance intp p2) (/ (sin sy1) (cos sy1)) ) )
(setq xx(abs (- (/ (distance (midp p1 p3) intp) 2) (abs sy2))) )
(setq b1 (+ (* k1 xx) (distance dm sx1)))
(setq b2 (+ (* k2 xx) (distance dm sx2)))
)
)
(setq long1 (sqrt (/ (- (sqr b1) (sqr b2)) (- (sqr k1) (sqr k2)))))
(setq short1 (sqrt (- (sqr b1) (* (sqr k1) (sqr long1)))))
(setq cen1 (list xx 0))
(if (or nil (and (< k1 0) (< (car p1) 0))
(and (> k1 0) (> (car p1) 0)))
(setq cen1 (list (- xx) 0)))
(if (= 1 xxx)
(progn
(setq cen1 (rot-90 cen1))
(setq long1 (sqrt (- (sqr b1) (* (sqr k1) (sqr long1)))))
(setq short1 (sqrt (/ (- (sqr b1) (sqr b2)) (- (sqr k1) (sqr k2)))))
(setq p1 (rot-90 (car pch)) p2 (rot-90 (cadr pch))
p3 (rot-90 (caddr pch)) p4 (rot-90 (cadddr pch)))
)
)
(if (or nil (< short1 1e-5) (< long1 1e-5) (< (/ short1 long1) 1e-5) (< (/ long1 short1) 1e-5))
(progn
(alert "你输入的距离不合适!")
(setvar "cmdecho" oce)
(setq xxx 18)
(princ)
)
(progn
(setvar "osmode" 0)
(command ".ucs" "O" pm)
(command ".line" p1 p2 p3 p4 "C")
(command ".ellipse" "C" cen1 (polar cen1 0 long1) (polar cen1 (/ Pi 2) short1))
(setvar "osmode" oldmode)
(setvar "cmdecho" oce)
(princ)
)
)
))
(t (progn
;;计算直线截距和斜率------------------
(setq b1 (/ (det2 p1 p2) (- (car p1) (car p2))))
(setq b2 (/ (det2 p2 p3) (- (car p2) (car p3))))
(setq b3 (/ (det2 p3 p4) (- (car p3) (car p4))))
(setq k1 (tank p1 p2) k2 (tank p2 p3) k3 (tank p3 p4) k (tank (midp p1 p3) (midp p2 p4)) )
;;定义求解椭圆长短轴线函数------------
(defun solvef (k1 k2 k3 k b1 b2 b3 / a b c g1 g2 s11 s12 s13 s21 s22 s23 sx1 sx2 sy1 sy2 kk1 kk2 kk3)
;;(defun solvef (k1 k2 k3 k b1 b2 b3)
(setq kk1 (- k1 k) kk2 (- k2 k) kk3 (- k3 k))
(setq a (+ (- (* (sqr k1) (sqr kk2))) (* (sqr k1) (sqr kk3))
(- (* (sqr k2) (sqr kk3))) (* (sqr k2) (sqr kk1))
(- (* (sqr k3) (sqr kk1))) (* (sqr k3) (sqr kk2))))
(if (< (abs a) 1e-8) (setq a 0) (princ))
(setq b (+ (* (sqr k1) (* 2 b2) (- kk2)) (* (sqr k1) (* 2 b3) (+ kk3))
(* (sqr k2) (* 2 b3) (- kk3)) (* (sqr k2) (* 2 b1) (+ kk1))
(* (sqr k3) (* 2 b1) (- kk1)) (* (sqr k3) (* 2 b2) (+ kk2))))
(if (< (abs b) 1e-8) (setq b 0) (princ))
(setq c (+ (- (* (sqr b1) (sqr k3))) (* (sqr b1) (sqr k2))
(- (* (sqr b2) (sqr k1))) (* (sqr b2) (sqr k3))
(- (* (sqr b3) (sqr k2))) (* (sqr b3) (sqr k1))))
(if (< (abs c) 1e-8) (setq c 0) (princ))
(setq g1 (roots a b c) g2 (cadr g1) g1 (car g1))
(setq s11 (sqr (+ (* kk1 g1) b1)) s12 (sqr (+ (* kk2 g1) b2)) s13 (sqr (+ (* kk3 g1) b3))
s21 (sqr (+ (* kk1 g2) b1)) s22 (sqr (+ (* kk2 g2) b2)) s23 (sqr (+ (* kk3 g2) b3)))
(defun solvex (k1 k2 k3 s1 s2 s3)
(cond ((= (sqr k2) (sqr k3)) (setq sss (sqrt (abs (/ (- s1 s2) (- (sqr k1) (sqr k2)))))) )
((= (sqr k3) (sqr k1)) (setq sss (sqrt (abs (/ (- s2 s3) (- (sqr k2) (sqr k3)))))) )
((= (sqr k1) (sqr k2)) (setq sss (sqrt (abs (/ (- s3 s1) (- (sqr k3) (sqr k1)))))) )
(t (setq sss (sqrt (abs (/ (- s1 s2) (- (sqr k1) (sqr k2)))))) )
)
)
(setq sx1 (solvex k1 k2 k3 s11 s12 s13))
(setq sy1 (sqrt (abs (- s11 (* k1 k1 sx1 sx1)))))
(setq sx2 (solvex k1 k2 k3 s21 s22 s23))
(setq sy2 (sqrt (abs (- s21 (* k1 k1 sx2 sx2)))))
(list (list (list g1 (* k g1)) sx1 sy1) (list (list g2 (* k g2)) sx2 sy2))
)
;;计算椭圆的长短轴和中心--
(setq so (solvef k1 k2 k3 k b1 b2 b3))
(setq cen1 (car (carso)) long1 (cadr (carso)) short1 (caddr (carso)))
(setq cen2 (car (cadr so)) long2 (cadr (cadr so)) short2 (caddr (cadr so)))
(if (= 1 xxx)
(progn
(setq cen1 (rot-90 cen1) long1(caddr (carso)) short1 (cadr (carso)))
(setq cen2 (rot-90 cen2) long2(caddr (cadr so)) short2 (cadr (cadr so)))
(setq p1 (rot-90 p1) p2 (rot-90 p2) p3 (rot-90 p3) p4 (rot-90 p4))
)
)
;;判断中心点是否在四边形内
;;并且判断所求是否满足要求
(if (and (and (> short2 1e-5) (> long2 1e-5) (> (/ short2 long2) 1e-5) (> (/ long2 short2) 1e-5))
(or nil (inner cen2 p1 p2 p3) (inner cen2 p2 p3 p4) (inner cen2 p3 p4 p1) (inner cen2 p4 p1 p2)))
(if (and (and (> short1 1e-5) (> long1 1e-5) (> (/ short1 long1) 1e-5) (> (/ long1 short1) 1e-5))
(or nil (inner cen1 p1 p2 p3) (inner cen1 p2 p3 p4) (inner cen1 p3 p4 p1) (inner cen1 p4 p1 p2)))
(setq xxx 2)
(setq cen1 cen2 long1 long2 short1 short2 xxx 3)
)
(if (and (and (> short1 1e-5) (> long1 1e-5) (> (/ short1 long1) 1e-5) (> (/ long1 short1) 1e-5))
(or nil (inner cen1 p1 p2 p3) (inner cen1 p2 p3 p4) (inner cen1 p3 p4 p1) (inner cen1 p4 p1 p2)))
(setq cen2 cen1 long2 long1 short2 short1 xxx 4)
(setq xxx 5)
)
)
;;画椭圆------------------
(setvar "osmode" 0)
(command ".ucs" "O" pm)
(cond ((= xxx 2)
(progn
(command ".line" p1 p2 p3 p4 "C")
(command ".ellipse" "C" cen1 (polar cen1 0 long1) (polar cen1 (/ pi 2) short1))
(command ".ellipse" "C" cen2 (polar cen2 0 long2) (polar cen2 (/ pi 2) short2))
))
((= xxx 3)
(progn
(command ".line" p1 p2 p3 p4 "C")
(command ".ellipse" "C" cen2 (polar cen2 0 long2) (polar cen2 (/ pi 2) short2))
))
((= xxx 4)
(progn
(command ".line" p1 p2 p3 p4 "C")
(command ".ellipse" "C" cen1 (polar cen1 0 long1) (polar cen1 (/ pi 2) short1))
))
((= xxx 5)
(progn
(alert "椭圆轴长或比率太少,无解")
(princ)
))
)
(command "ucs" "P")
(setvar "osmode" oldmode)
(setvar "cmdecho" oce)
(princ)
)
)
)
)
)
)
)
)
) 晕死看不懂:Q
页:
[1]