Line Int with Circle

Discussion in 'AutoCAD' started by jbryant4, Oct 7, 2004.

  1. jbryant4

    jbryant4 Guest

    Looking for a built-in reoutine (maybe vl vlx) that calcualtes an intersection between a line and a circle. The line is designated by 2 points and the circle is designated by centerpoint and rad. The line and circle are not entities in the drawing.....Something like this...(cint p1 p2 (list 2.0 3.0) 3.5)).....p1 is first point of line p2 is secong point of line (list 2.0 3.0) is center or circle and 3.5 is circle rad......Seems like I remember seeing something like this?
     
    jbryant4, Oct 7, 2004
    #1
  2. jbryant4

    David Bethel Guest

    not built in but a very simple (inters) call. -David

    (inters p10 p11 cen (polar cen (+ (angle p10 p11) (* pi 0.5)) rad))
     
    David Bethel, Oct 7, 2004
    #2
  3. I don't think this will work, except maybe under extraordinary-coincidence
    circumstances. I am assuming "cen" means the center point of the circle and
    "rad" is its radius. I am also assuming that jbrant4 wants to find where
    the hypothetical line would intersect the CURVE of the hypothetical circle
    (like INT osnap).

    (inters) takes four points and finds where the "first" hypothetical line
    (between the 1st & 2nd points) would intersect the "second" line (between
    the 3rd and 4th). [Of course no actual lines need to be involved, just as
    jbryant4's line and circle are not entities in the drawing.] David's
    routine makes that "second" line a radius of the circle, whose direction is
    perpendicular to the "first" line. But that radius line won't intersect the
    "first" line where the "first" line intersects the circle itself, unless it
    happens to hit at exactly the right spot at the outside end of that radius.

    Simple example: p1 is at 0,0, p2 is at 4,0, the center of the circle is at
    1,0, and its radius is 2. The line is horizontal, and happens to pass
    through the center of the circle for simplicity of visualization. The line
    and circle intersect at 3,0. This routine will look for the intersection
    point between four points: 0,0 4,0 1,0 1,2 (the end of the radius at 90
    degrees from the direction of the line). That will return 1,0, which
    happens to be the center of the circle, and one end of that "second" line,
    but not where the line and circle intersect.

    If the line happened to be horizontal along the 2 level in the y direction,
    it would happen to give the right answer.

    I don't see a way to do what I think jbryant4 wants, except maybe to convert
    the line and circle into equations representing them, and solve the
    simultaneous equations for an intersection. I don't know if you can do that
    in any of the languages available for AutoCAD customization, but I assume
    something like that is what (inters) does, so maybe someone out there has an
    idea. It's just a lot more complicated with the circle involved, but I
    guess INT and APPINT osnap must do it somehow.
     
    Kent Cooper, AIA, Oct 7, 2004
    #3
  4. jbryant4

    David Bethel Guest

    ;;;Intersection Point Of LINE & CIRCLE
    ;;;ARG -> LINE ename CIRCLE ename
    ;;;RETURNS 2D point list or nil
    (defun inters_line_circle (le ce / p10 p11 cen rad ppt pds osd ipt p2d)
    (setq p2d (lambda (p) (list (car p) (cadr p))))
    (and (= (type le) 'ENAME)
    (= (type ce) 'ENAME)
    (= "LINE" (cdr (assoc 0 (entget le))))
    (= "CIRCLE" (cdr (assoc 0 (entget ce))))
    (setq p10 (p2d (cdr (assoc 10 (entget le))))
    p11 (p2d (cdr (assoc 11 (entget le))))
    cen (p2d (cdr (assoc 10 (entget ce))))
    rad (cdr (assoc 40 (entget ce))))
    (cond ((equal p10 p11 1e-14))
    ((equal cen p10 1e-11)
    (setq ipt (polar cen (angle cen p11) rad)))
    ((equal cen p11 1e-11)
    (setq ipt (polar cen (angle cen p10) rad)))
    ((setq ppt (inters p10 p11 cen
    (polar cen (+ (angle p10 p11) (* pi
    0.5)) rad) nil))
    (and (setq pds (distance cen ppt))
    (<= pds rad)
    (setq osd (sqrt (- (* rad rad) (* pds pds)))
    ipt (polar ppt (angle ppt p10) osd))))))
    ipt)


    then something like could work. It would have to be modified to accept
    the point directly. -David
     
    David Bethel, Oct 7, 2004
    #4
  5. I think I get where you and Pythagoras are going, and yes, it would have to
    be modified for hypothetical-point input (this appears to require line and
    circle entities to exist already, and it looks like they would have to be
    pre-selected). One thing I don't understand -- could you explain the
    "le-14" and "le-11" fuzz factors in the (equal) functions?
     
    Kent Cooper, AIA, Oct 8, 2004
    #5
  6. jbryant4

    David Bethel Guest

    As to why they are, I really don't remember. This snippet is probably 8
    years old or so. (inters) might have been a little more tolerant than
    short line lengths. -David
     
    David Bethel, Oct 8, 2004
    #6
  7. jbryant4

    jbryant4 Guest

    This is actually what I have been using(watch word wrap). I had a college coop here at work figure out the formulas, and I converted it to lisp. I have been using this for 7-8 years, however there seems to be times (rarely) where it doesn't quite work right....I thought a while back I saw a VLISP funtion that did the same...Maybe I was wrong.

    (defun cint (cen r sp ep / h k x1 x2 y1 y2 rot_flag ang_sp ang_ep
    dist_sp dist_ep m yint a b c x11 x22 x y int_pt int_pt_ang
    int_pt_dist new_pt
    )
    (setq
    h (nth 0 cen)
    k (nth 1 cen)
    x1 (nth 0 sp)
    x2 (nth 0 ep)
    y1 (nth 1 sp)
    y2 (nth 1 ep)
    )
    (if (= x2 x1)
    (progn
    (setq
    rot_flag 1
    ang_sp (angle cen sp)
    ang_ep (angle cen ep)
    dist_sp (distance cen sp)
    dist_ep (distance cen ep)
    sp (polar cen (+ (dtr 90) ang_sp) dist_sp)
    ep (polar cen (+ (dtr 90) ang_ep) dist_ep)
    x1 (nth 0 sp)
    x2 (nth 0 ep)
    y1 (nth 1 sp)
    y2 (nth 1 ep)
    )
    )
    (setq rot_flag 0)
    )
    (setq
    m (/ (- y2 y1) (- x2 x1))
    yint (- y1 (* m x1))
    a (+ (* m m) 1.0)
    b (- (* 2.0 m yint) (* 2.0 h) (* 2.0 k m))
    c
    (+
    (* h h)
    (* k k)
    (* yint yint)
    (- (* 2.0 k yint))
    (- (* r r))
    )
    )
    (if (> (- (* b b) (* 4.0 a c)) 0.0)
    (progn
    (setq
    x11 (/
    (+
    (- b)
    (sqrt
    (- (* b b) (* 4.0 a c))
    )
    )
    (* 2.0 a)
    )
    x22 (/
    (-
    (- b)
    (sqrt
    (- (* b b) (* 4.0 a c))
    )
    )
    (* 2.0 a)
    )
    )
    (if (<= (abs (- x1 x11)) (abs (- x1 x22)))
    (setq x x11)
    (setq x x22)
    )
    (setq y (+ (* m x) yint))
    )
    )
    (setq int_pt (list x y))
    (if (= rot_flag 1)
    (progn
    (setq
    int_pt_ang (angle cen int_pt)
    int_pt_dist (distance cen int_pt)
    new_pt (polar cen (- int_pt_ang (dtr 90.0)) int_pt_dist)
    )
    )
    (list x y)
    )
    )
     
    jbryant4, Oct 8, 2004
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.