Programming Challenge 11/22

Programming Challenge 11/22

john.uhden
Mentor Mentor
2,430 Views
42 Replies
Message 1 of 43

Programming Challenge 11/22

john.uhden
Mentor
Mentor

I haven't tried this myself, but our challenge is to write code that draws a line tangent to two (2) non-touching non-overlapping non-concentric circles without using object snaps.  If there is a shorter line vs. a longer line, we want the shorter one.  Yes, both circles are at elevation 0 with normal normals and that have radii greater than 0.

I'm guessing it's nothing but trigonometry, but I dunno yet.

The function should not use any ssget but instead take two (2) arguments, one for each of the two circles as enames.  Yes, you may use supporting functions of your own making.

The winner will be the one that gets the most likes.  No, you can't "like" your own.

Any ties will be broken by the decision of @dbroad , with or without bribes.

John F. Uhden

2,431 Views
42 Replies
Replies (42)
Message 41 of 43

_gile
Consultant
Consultant

@calderg1000  added a kind of challenge inside the challenge that I wanted to do for fun.
Here's solution that uses only vector calculus, no trigonometric function, no vlax-curve* function.

 

[Edit: refactored code]

 

(defun circleTangents (circle1 circle2 flags / e1 e2 c1 r1 c2 r2 a u v)
  (setq e1 (entget circle1))
  (mapcar 'set '(c1 r1) (list (cdr (assoc 10 e1)) (cdr (assoc 40 e1))))
  (setq e2 (entget circle2))
  (mapcar 'set '(c2 r2) (list (cdr (assoc 10 e2)) (cdr (assoc 40 e2))))
  (setq	a (distance c1 c2)
	u (mapcar '(lambda (x y) (/ (- y x) a)) c1 c2)
	v (list (- (cadr u)) (car u))
  )
  (mapcar
    '(lambda (x) (entmakex (mapcar 'cons '(0 10 11) x)))
    (apply
      'append
      (mapcar
	'(lambda (i f / b d h k1 k2 p1 p2 v1 v2)
	   (if
	     (and
	       (< 0 (logand i flags))
	       (setq b (f r1 r2))
	       (< b a)
	       (setq d	(/ (* b b) a)
		     h	(sqrt (- (* b b) (* d d)))
		     k1	(/ r1 b)
		     k2	(- k1 1.)
		     p1	(mapcar '(lambda (x y) (+ x (* y (* k1 d)))) c1 u)
		     p2	(mapcar '(lambda (x y) (+ x (* y (* k2 d)))) c2 u)
		     v1	(mapcar '(lambda (x) (* x (* k1 h))) v)
		     v2	(mapcar '(lambda (x) (* x (* k2 h))) v)
	       )
	     )
	      (list
		(list "line" (mapcar '+ p1 v1) (mapcar '+ p2 v2))
		(list "line" (mapcar '- p1 v1) (mapcar '- p2 v2))
	      )
	   )
	 )
	'(1 2)
	(list - +)
      )
    )
  )
)

 

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 42 of 43

dbroad
Mentor
Mentor

Seems to work fine. Major (possibly false) assumption is that the circles are drawn in model space and not in a block or paper layout.  Look at my code to find the owner for the circles.  The lines should be drawn in the same owner (block) as the circles. Try executing your program in paper space or in the block editor.

 

Otherwise, very interesting use of parameters and vlax-curve functions. Kudos.

 

I agree that the trig functions will return the same level of accuracy as the vlax-curve functions or any of the point generating functions.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 43 of 43

john.uhden
Mentor
Mentor

@Kent1Cooper 

I guess I never thought about it that way.  MANY THANKS!!

John F. Uhden

0 Likes