Passing arguments to functios fails

Passing arguments to functios fails

T2ioTD
Enthusiast Enthusiast
870 Views
3 Replies
Message 1 of 4

Passing arguments to functios fails

T2ioTD
Enthusiast
Enthusiast

Hi All

 

I am writing a fx in Autolisp to calculate  and return the distances (D1,D2,...Dn) of a point defined by xo,yo to a list of points defined by a list (x1,y1,x2,y2,...xn,yn) But in the Vlisp editor, adding the watch, i keep seeing that the function have its arguments always nil:

 

I hope I get much needed advice on this board.

 

; A helper function to calculate the Pythagorian distance. Tested.
(defun DisPythagor(x1,y1,x2,y2/)
(setq xx (- x1 x2))
(setq xx (* xx xx))
(setq yy (- x1 x2))
(setq yy (* yy yy))
(setq Pythagore (sqrt (+ xx yy)))
); function


; the function part that is making the error of always having xo, yo and xiyiList as nil:
(defun manyDist( xo,yo,xiyiLst)
 (setq Dislist nil)
 (setq i 0)
 (setq test (length xiyiLst))
 (alert (rtos test))
 (while (< i (/ (length xiyiLst) 2))
  ; works each two numbers together:
  (setq xi (nth i numlist))
  (setq yi (nth (+ i 1) numlist))
  (setq Di (DisPythagor xo,yo,xi,yi))
  ; now aSdds this Di the assembled list:
  (setq Dislist (append Dislist (list Di)))
  (+ i 2) ; advances the counter
 ); while
(setq Dislist Dislist) ;  to make it the last  thing happening
); function


; the main calling the function above:
(setq PtList (list 3 3 1 1 2 2 3 3))
(setq x 0)
(setq y 0)
(setq Disliste (manyDist x,y,PtList))
(princ Disliste)

0 Likes
Accepted solutions (1)
871 Views
3 Replies
Replies (3)
Message 2 of 4

ВeekeeCZ
Consultant
Consultant
Accepted solution
Arguments must be separated by space, not a comma.

(defun DisPythagor(x1 y1 x2 y2 /)
(setq Disliste (manyDist x y PtList))
0 Likes
Message 3 of 4

T2ioTD
Enthusiast
Enthusiast

So the corrected version (I had also to update some variable names):

 

; the function part:
(defun DisPythagore(x1 y1 x2 y2/)
(setq xx (- x1 x2))
(setq xx (* xx xx))
(setq yy (- x1 x2))
(setq yy (* yy yy))
(setq Pythagore (sqrt (+ xx yy)))
); function


; the function part:
(defun manyDist(xo yo xiyiLst)
 (setq Dislist nil)
 (setq i 0)
 (setq test (length xiyiLst))
 (alert (rtos test))
 (while (< i (length xiyiLst) ) ;jump 2 by 2
  ; works each two numbers together:
  (setq xi (nth i xiyiLst))
  (setq yi (nth (+ i 1) xiyiLst))
  (setq Di (DisPythagore xo yo xi yi))
  ; now aSdds this Di the assembled list:
  (setq Dislist (append Dislist (list Di)))
  (setq i (+ i 2)) ; advances the counter
 ); while
(setq Dislist Dislist) ;  make it the last thing happening
); function


; the main calling the function:
(setq PtList (list 3 3 1 1 2 2 3 3))
(setq x 0)
(setq y 0)
(setq Disliste (manyDist x y PtList))
(princ Disliste)

0 Likes
Message 4 of 4

Kent1Cooper
Consultant
Consultant

In case you're interested in some comments/critique, in the interest of simplification/consolidation:

You don't need the DisPythagore function at all.  There's a (distance) function that will do that calculation for you between two points.

 

The (alert) ought to use (itoa) rather than (rtos).  In my setup with Architectural Units, what your original reports is 8".  And it can be reported directly, without the middle-man of the 'test' variable.

 

Since presumably you know the list will always have an even number of coordinates, a (repeat) function instead of a (while) function spares it the need to make an evaluation every time it goes through, in order to determine whether to go through again.  It can just do it the known number of times.

 

You can "report" the contents of a variable at the end just by stating the variable name, without having to (setq) it again to achieve that.  And if you localize that one, it doesn't need to be set to nil at the beginning, because it will always start out that way.

 

You can (setq) a bunch of things within one (setq) function -- they don't each need to have their own.

 

This is equivalent to the combination of your DisPythagore and manyDist functions together:

 

(defun manyDist (xo yo xiyiLst / i xi yi Di DisList)
  (setq i 0)
  (alert (itoa (length xiyiLst)))
  (repeat (/ (length xiyiLst) 2); jump 2 by 2
  ; works each two numbers together:
    (setq
      xi (nth i xiyiLst)
      yi (nth (1+ i) xiyiLst)
      Di (distance (list xo yo) (list xi yi))
      ; now aSdds this Di the assembled list:
      Dislist (append Dislist (list Di))
      i (+ i 2) ; advances the counter
    ); setq
  ); repeat
  Dislist ; report result
); defun
Kent Cooper, AIA
0 Likes