Finding a Midpoint

KEPelton

Usenet Archive
Joined
Aug 1, 2003
Messages
6
Reaction score
0
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
 
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:

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
 
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.
 
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
 
(Setq pnt (Polar sptd (Angle sptd eptd) (/ (Distance sptd eptd) 2.0))
 
That's the ticket, forgot about that one.  Good call!

"mhr" <> wrote:

(Setq pnt (Polar sptd (Angle sptd eptd) (/ (Distance sptd eptd) 2.0))
 
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:

(Setq pnt (Polar sptd (Angle sptd eptd) (/ (Distance sptd eptd) 2.0))
 
(defun midpnt (pt1 pt2)

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

"KEPelton" <> wrote:

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
 
One more...

 

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

"KEPelton" <> wrote:

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
 
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. :]
 
Hi John,

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

Regards,
Doug
 
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

John Uhden said:
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. :]

Doug Broad said:
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))

(Setq pnt (Polar sptd (Angle sptd eptd) (/ (Distance sptd eptd) 2.0))
 
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

Jamie Duncan said:
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

John Uhden said:
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. :]

Doug Broad said:
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))

(Setq pnt (Polar sptd (Angle sptd eptd) (/ (Distance sptd eptd) 2.0))
 
thanks for the clarification.

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

Doug Broad said:
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

Jamie Duncan said:
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

John Uhden said:
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. :]

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))

(Setq pnt (Polar sptd (Angle sptd eptd) (/ (Distance sptd eptd) 2.0))
 
Back
Top