Finding a Midpoint

Discussion in 'AutoCAD' started by KEPelton, Aug 1, 2003.

  1. KEPelton

    KEPelton Guest

    Hello. I am fairly new to LISP so I apologize in advance if this has already been answered.

    I am trying to write a rountine that will ask the user for two points, determine the midpoint between these points and insert a block at that midpoint. Is there a way to do that? I have:

    (setq sptd (getpoint "\nFirst Point?"))
      (setq eptd (getpoint "\nSecond Point?"))

    How can I determine the midpoint between sptd and eptd? Thank you very much.

    K.E. Pelton
    SDG
     
    KEPelton, Aug 1, 2003
    #1
  2. KEPelton

    Jason Wilder Guest

    Well, I hope someone could provide you a better answer, but two methods come to mind.



     



    One - draw a temporary line, get the midpoint of the line, place the block, erase the line.



     



    Two - mathematically calculate the midpoint.



    "KEPelton" <> wrote in message news:...

    Hello. I am fairly new to LISP so I apologize in advance if this has already been answered.

    I am trying to write a rountine that will ask the user for two points, determine the midpoint between these points and insert a block at that midpoint. Is there a way to do that? I have:

    (setq sptd (getpoint "\nFirst Point?"))
      (setq eptd (getpoint "\nSecond Point?"))

    How can I determine the midpoint between sptd and eptd? Thank you very much.

    K.E. Pelton
    SDG
     
    Jason Wilder, Aug 1, 2003
    #2
  3. KEPelton

    ffejgreb Guest

    Rather than write a routine, why not use acad's internal calculator?  When prompted for an insertion point, type 'cal - mee.  Works perfectly and been using for years.
     
    ffejgreb, Aug 1, 2003
    #3
  4. KEPelton

    kkosbau Guest

    You do a little math to get the mid point.
    (setq x1 (car eptd))
    (setq x2 (car sptd))
    (setq y1 (cadr eptd))
    (setq y2 (cadr sptd))

    then

    (setq midx (/ (+ x1 x2) 2))
    (setq midy (/ (+ y1 y2) 2))
    (setq midpt (list midx midy))

    I think that will work
     
    kkosbau, Aug 1, 2003
    #4
  5. KEPelton

    mhr Guest

    (Setq pnt (Polar sptd (Angle sptd eptd) (/ (Distance sptd eptd) 2.0))
     
    mhr, Aug 1, 2003
    #5
  6. KEPelton

    Jason Wilder Guest

    That's the ticket, forgot about that one.  Good call!



    "mhr" <> wrote in message news:...

    (Setq pnt (Polar sptd (Angle sptd eptd) (/ (Distance sptd eptd) 2.0))
     
    Jason Wilder, Aug 1, 2003
    #6
  7. KEPelton

    Doug Broad Guest

    Caveat:



    POLAR is the wrong function to use unless you are doing 2D math.  It should



    almost never be used for such a function.



     



    If you're not going to use the built in calculator, then:



     



    (defun mp (p1 p2) (mapcar '(lambda(a b)(/(+ a b 0.0) 2.0)) p1 p2))



     



     



    "mhr" <> wrote in message news:...

    (Setq pnt (Polar sptd (Angle sptd eptd) (/ (Distance sptd eptd) 2.0))
     
    Doug Broad, Aug 1, 2003
    #7
  8. KEPelton

    Josh Guest

    (defun midpnt (pt1 pt2)



     (mapcar '+  pt1 (mapcar '* (mapcar '- pt2 pt1) '(0.5 0.5 0.5)))
    )




    "KEPelton" <> wrote in message news:...

    Hello. I am fairly new to LISP so I apologize in advance if this has already been answered.

    I am trying to write a rountine that will ask the user for two points, determine the midpoint between these points and insert a block at that midpoint. Is there a way to do that? I have:

    (setq sptd (getpoint "\nFirst Point?"))
      (setq eptd (getpoint "\nSecond Point?"))

    How can I determine the midpoint between sptd and eptd? Thank you very much.

    K.E. Pelton
    SDG
     
    Josh, Aug 1, 2003
    #8
  9. KEPelton

    KEPelton Guest

    That works! Thank you very much.

    -K
     
    KEPelton, Aug 1, 2003
    #9
  10. KEPelton

    KEPelton Guest

    Thank you everyone! It works!

    -K.E.
     
    KEPelton, Aug 1, 2003
    #10
  11. KEPelton

    Joe Burke Guest

    One more...



     



    (defun MidPoint (p1 p2)
     (mapcar '* (mapcar '+ p1 p2) '(0.5 0.5 0.5))
    )



    "KEPelton" <> wrote in message news:...

    Hello. I am fairly new to LISP so I apologize in advance if this has already been answered.

    I am trying to write a rountine that will ask the user for two points, determine the midpoint between these points and insert a block at that midpoint. Is there a way to do that? I have:

    (setq sptd (getpoint "\nFirst Point?"))
      (setq eptd (getpoint "\nSecond Point?"))

    How can I determine the midpoint between sptd and eptd? Thank you very much.

    K.E. Pelton
    SDG
     
    Joe Burke, Aug 1, 2003
    #11
  12. KEPelton

    John Uhden Guest

    That IS the best, BTW. Actually, it's just shy of the best. Use * instead of
    /. But you knew that and was just checking to see if anyone was paying
    attention. :]
     
    John Uhden, Aug 2, 2003
    #12
  13. KEPelton

    Doug Broad Guest

    Hi John,

    Catching up on your NG reading after a nice vacation?
    Ya got me.

    Regards,
    Doug
     
    Doug Broad, Aug 2, 2003
    #13
  14. KEPelton

    Jamie Duncan Guest

    Why is that John/Doug? - to avoid possible divide by zero error if user
    picks same point twice?

    also, you use lambda and mapcar 'cause points are lists? why the 0.0?

    Jamie


     
    Jamie Duncan, Aug 2, 2003
    #14
  15. KEPelton

    Doug Broad Guest

    0.0 probably isn't necessary. The concepts are a holdover from
    my experiences from earlier versions of LISP where adding two
    large integers had unpredictable results or overflow. The 0.0
    ensures that the result of the addition is a real rather than an integer.
    The type conversion is automatic and does not require float.

    If there had been a function called "average", then it could
    have been written:
    (defun midpt (p1 p2) (mapcar 'average p1 p2))

    But since there is no buit in function "average", the choice is
    define one or use lambda to create a nameless version.

    Multiplication by 0.5 has been demonstrated by some in
    the newsgroup to be faster than dividing by 2.

    So this is probably enough:
    (defun midpt (p1 p2)(mapcar '(lambda (a b) (* 0.5 (+ a b))) p1 p2))

    Does that help?

    The main reason I posted was to warn folks not to use
    polar unless the z-coordinate value was unimportant.

    Regards,
    Doug

     
    Doug Broad, Aug 2, 2003
    #15
  16. KEPelton

    Jamie Duncan Guest

    thanks for the clarification.

    the 0.5 verus /2 seems insignificant unless one was performing this on
    thousands of points.


    --
    Jamie Duncan

    "Don't walk in front of me, I may not follow. Don't walk behind me, I may
    not lead. Walk beside me and be my friend. "


     
    Jamie Duncan, Aug 2, 2003
    #16
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.