Results 1 to 4 of 4

Thread: List Manipulation

  1. #1
    Member
    Join Date
    2012-07
    Posts
    4
    Login to Give a bone
    0

    Default List Manipulation

    Hi, I have a strange problem.
    I have a list of point that I read to find angle direction.
    The list is:
    ((1715.26 4377.56) (1715.26 5237.19) (1715.26 6096.82) (2828.1 6096.82) (3940.94 6096.82) (5053.78 6096.82) (5053.78 5237.19) (5053.78 4377.56))


    The function:
    (while (cadr lt)
    (setq a(rtod(angle (car lt)(cadr lt))))
    (setq la(append la(list a)))
    (setq lt(cdr lt))
    )


    **RTOD is a Radian to Degre function


    the function read correctly points:
    The result is:
    (90.0 90.0 0.0 0.0 -5.08889e-014 -90.0 -90.0)


    Everything is looking good, but the 5th angle is weird


    If you look at the list you can see that the 3rd, 4th, 5th and 6th elements in the list are horizontals points.
    But if you try this to find angle:
    (angle(nth 2 ls)(nth 3 ls)) Result: 0.0
    (angle(nth 3 ls)(nth 4 ls)) Result: 0.0
    (angle(nth 4 ls)(nth 5 ls)) Result: 6.28319


    the last result has a bad impact on the rest of the program that I wrote (insert block and draw balcony railing)


    Someone know why the 3rd result is different than 1st and 2nd?


    And Can you help to return 0.0 like 1st and 2nd?

    Thanks

    SD

  2. #2
    100 Club
    Join Date
    2000-11
    Location
    Ontario, Canada
    Posts
    116
    Login to Give a bone
    0

    Default Re: List Manipulation

    I assume that your point list is being obtained from objects - such as contiguous line segments - rather than being typed in explicitly. Using the following functions...

    Code:
    (defun angs (lst / A LA LT)
      (setq lt lst)
      (while (cadr lt)
        (setq a(rtod(angle (car lt)(cadr lt))))
        (setq la(append la(list a)))
        (setq lt(cdr lt))
        )
      la)
    
    (defun rtod (num-rads)
      (* 180.0 (/ num-rads pi))
    )
    ... I fed your list of points to (angs), and got the following list:

    Code:
    (90.0 90.0 0.0 0.0 0.0 270.0 270.0)
    (I assume that your RTOD function reduces 'pseudo-parallel' angles, such as 90 and 270 degrees, to the lower representation)

    Points read from objects are saved with higher precision than what is represented in your list, and arbitrarily high precision can lead to uncertainty in the calculation. If this is indeed what is happening, I suggest that you round your points to a pre-set number of decimals before running them through your function.

  3. #3
    Active Member
    Join Date
    2009-03
    Posts
    63
    Login to Give a bone
    0

    Default Re: List Manipulation

    I get this from time to time when writing angle functions too, particularly if the points are being picked off the screen rather than typed in. What GHarvey said above is exactly the culprit. The 6.28319 return your getting is the radian equivalent of 360°, but if you go out farr enough in the decimals a rounding issue somewhere is making it not quite tip over from slightly less than 360° back to 0°. My usual solution is to add some kind of "fuzz factor" to the function. If 2 decimal precision is good enough for you then something like this will do it...

    Code:
    (setq pt (getpoint) pt (list (atof (rtos (car pt) 2 2)) (atof (rtos (cadr pt) 2 2)) (atof (rtos (caddr pt) 2 2))))
    If you need higher precision then set the decimal precision in the rtos functions higher.

    If your getting your points list from an object then that list can be run through a quick loop and run that rtos then atof to round off all your atoms to something that plays nicer with the calculator.

    I could probably help more if you posted the whole code, but thats what I got off the top of my

  4. #4
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: List Manipulation

    I like descriptive variable and function names names so that it is easy to read the code.

    Saving a few characters in lisp doesn't really save that much memory but makes it harder to read.

    A three letter prefix describing the variable type (like the Reddick naming convention) and Camel capitalization names make each variable meaning clear.

    I also like functions to return nil for error.
    I like to test all of the variables to make sure they are what I think they are.

    LISP has a little trouble with very small numbers that should be zero.

    so instead of (rtod pi)

    (radianstodegrees pi 8 ) ;<- 8 Decimal places.

    Or something like that.

    P=



    Code:
    (defun RadiansToDegrees (sngRadians intDecimalPlaces / sngDegrees strDegrees)
     (if (and (= (type sngRadians) 'REAL)
                (= (type intDecimalPlaces) 'INT)
                (setq sngDegrees (* 180 (/ sngRadians pi)))
                (setq strDegrees  (rtos sngDegrees 2 intDecimalPlaces))
        )
      (atof strDegrees)
     )
    )
    
    (defun DegreesToRadians (sngDegrees intDecimalPlaces / sngRadians strRadians)
     (if (and (= (type sngDegrees) 'REAL)
                (= (type intDecimalPlaces) 'INT)
                (setq sngRadians (* pi (/ sngDegrees 180.)))
                (setq strRadians  (rtos sngRadians 2 intDecimalPlaces))
        )
      (atof strRadians)
     )
    )
    AutomateCAD

Similar Threads

  1. bi-ped manipulation
    By and36rw969741 in forum 3ds Max - General
    Replies: 0
    Last Post: 2011-05-18, 12:39 AM
  2. 3D Viewport Manipulation
    By kevin.collins2 in forum AutoCAD General
    Replies: 0
    Last Post: 2009-04-10, 02:18 PM
  3. Data Manipulation?
    By TroyGates in forum NavisWorks - General
    Replies: 3
    Last Post: 2008-06-11, 11:08 PM
  4. Help With String Manipulation
    By CADdancer in forum AutoLISP
    Replies: 3
    Last Post: 2006-12-05, 03:37 PM
  5. Better Void Manipulation
    By narlee in forum Revit Architecture - Wish List
    Replies: 7
    Last Post: 2005-04-04, 02:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •