hi , all
Highfly's method is Efficient , until now , I still can not understand .
Following code is using Asymptotic method .
Just for test , when the point is close the line (cen1 cen2) , it return the wrong result .
(defun c:test (/ foo cir1 cir2 pnt dxf1 dxf2 rad1 rad2 cen1 cen2 cr1)
(defun foo (mode / is_go pl1 pl2 mp1 mp2)
;; Using Asymptotic method
;; mode 1 -- outside 2 circles ; 2 -- include 2 circles
;; 3 -- include circle-1 ; 4 -- include circle-2
;;
(if (setq cr1 (3PCirCle pnt cen1 cen2)) (setq is_go T))
(while is_go
(setq pl1 (C_INT_C (car cr1) (cadr cr1) cen1 rad1)
pl2 (C_INT_C (car cr1) (cadr cr1) cen2 rad2)
)
(cond ((and pl1 (cadr pl1))
(setq mp1 (midpt (car pl1) (cadr pl1)))
)
(pl1 (setq mp1 (car pl1)))
(t)
)
(cond ((and pl2 (cadr pl2))
(setq mp2 (midpt (car pl2) (cadr pl2)))
)
(pl1 (setq mp2 (car pl2)))
(t)
)
(if (or (and (not pl1) (not pl2)) (and (not (cadr pl1)) (not (cadr pl2))) (and (equal (car pl1) (cadr pl1) 1e-3) (equal (car pl2) (cadr pl2) 1e-3 )))
(setq is_go nil)
(setq cr1 (3PCirCle pnt (polar cen1 (cond ((or (= mode 1) (= mode 4))(angle cen1 mp1))
((or (= mode 2) (= mode 3)) (if (ss-ostp mp1 pnt cen1 cen2) (angle cen1 mp1) (angle mp1 cen1)))
)
rad1) (polar cen2
(cond ((or (= mode 1) (= mode 3))(angle cen2 mp2))
((or (= mode 2) (= mode 4)) (if (ss-ostp mp2 pnt cen1 cen2) (angle cen2 mp2) (angle mp2 cen2)))
) rad2)))
)
)
(if cr1 (entmake (list (cons 0 "CIRCLE") (cons 10 (car cr1)) (cons 40 (cadr cr1)) (cons 62 mode))))
)
(setq cir1 (car (entsel "\nPick a circle:")))
(setq cir2 (car (entsel "\nPick another circle:")))
(setq pnt (getpoint "\nPick a point:"))
(if (not (and cir1 cir2 pnt))
(exit)
)
(setq dxf1 (entget cir1))
(setq dxf2 (entget cir2))
(setq rad1 (cdr (assoc 40 dxf1)))
(setq rad2 (cdr (assoc 40 dxf2)))
(setq cen1 (cdr (assoc 10 dxf1)))
(setq cen2 (cdr (assoc 10 dxf2)))
;;
(foo 1)
;;;
(foo 2)
;;;include circle-1 , outside circle-2
(foo 3)
;;;include circle-2 , outside circle-1
(foo 4)
(princ)
)
(defun midpt (pta ptb)
(mapcar (function (lambda (x y)
(/ (+ x y) 2.0)
)
)
pta
ptb
)
)
;;;is outside of triangle
(defun ss-ostp (p p1 p2 p3)
;;Gile & Lee Mac
((lambda (a b c)
(not
(or
(and (< 1e-6 a) (< 1e-6 b) (< 1e-6 c))
(and (< a -1e-6) (< b -1e-6) (< c -1e-6))
) ;_if at triangle edge, so add tolerance
)
)
(sin (- (angle p1 p) (angle p1 p2)))
(sin (- (angle p2 p) (angle p2 p3)))
(sin (- (angle p3 p) (angle p3 p1)))
)
)
;;;circle intersect with circle
(defun C_Int_C (ce1 r1 ce2 r2 / dis a b)
;;by w_kai
(setq dis (distance ce1 ce2))
(cond
((equal dis (+ r1 r2))
(list (polar ce1 (angle ce1 ce2) r1))
)
((equal dis (abs (- r1 r2)))
(if (minusp (- r1 r2))
(list (polar ce2 (angle ce2 ce1) r2))
(list (polar ce1 (angle ce1 ce2) r1))
)
)
((and (> dis (abs (- r1 r2)))
(< dis (+ r1 r2))
)
(setq a (/ (- (+ (* dis dis) (* r1 r1))
(* r2 r2)
)
(* 2. dis)
)
)
(setq b (sqrt (- (* r1 r1) (* a a))))
(list (polar (polar ce1 (angle ce1 ce2) a)
(+ (angle ce1 ce2) (/ pi 2))
b
)
(polar (polar ce1 (angle ce1 ce2) a)
(- (angle ce1 ce2) (/ pi 2))
b
)
)
)
)
)
;;;
;;; 三点圆函数
(defun 3PCirCle
(P0 P1 P2 / X0 Y0 X1 Y1 X2 Y2 DX1 DY1 DX2 DY2 D 2D C1 C2 CE)
;;by highflybird
(setq X0 (car P0)
Y0 (cadr P0)
X1 (car P1)
Y1 (cadr P1)
X2 (car P2)
Y2 (cadr P2)
DX1 (- X1 X0)
DY1 (- Y1 Y0)
DX2 (- X2 X0)
DY2 (- Y2 Y0)
)
(setq D (- (* DX1 DY2) (* DX2 DY1)))
(if (/= D 0.0)
(progn
(setq 2D (+ D D)
C1 (+ (* DX1 (+ X0 X1)) (* DY1 (+ Y0 Y1)))
C2 (+ (* DX2 (+ X0 X2)) (* DY2 (+ Y0 Y2)))
CE (List (/ (- (* C1 DY2) (* C2 DY1)) 2D)
(/ (- (* C2 DX1) (* C1 DX2)) 2D)
)
)
(list CE (distance CE P0) (list p0 p1 p2))
)
)
)