Value from atan wrong

Discussion in 'AutoCAD' started by BillZ, Jul 2, 2004.

  1. BillZ

    BillZ Guest

    I have a vector:
    Command: !vect_1
    (-2.22045e-015 8.88178e-016 4.24264)

    When I feed these numbers to atan I get:
    Command: (rtd (atan (cadr vect_1)(car vect_1)))
    158.199
    Which is wrong.

    When I use:
    Command: (rtd (atan (atoi (rtos (cadr vect_1)))(atoi (rtos (car vect_1)))))
    0.0
    It returns the correct angle.
    My question: Is this the correct/accurate way to handle this problem?

    TIA

    Bill
     
    BillZ, Jul 2, 2004
    #1
  2. BillZ

    Devin Guest

    Hi Bill,

    I think you may have an extra argument in the atan function. What is the
    code for your atan function?

    Devin
     
    Devin, Jul 2, 2004
    #2
  3. BillZ

    Jürg Menzi Guest

    Hi Bill

    Use this function:
    ;
    ; == Function Vec2Ang
    ; Converts a vector to an angle.
    ; Arguments [Type]:
    ; Vec = Vector value

    • ; Return [Type]:
      ; > Angle value [REAL]
      ; Notes:
      ; Credits to Luis Esquivel
      ;
      (defun Vec2Ang (Vec / XvcVal YvcVal)
      (setq XvcVal (car Vec)
      YvcVal (cadr Vec)
      )
      (cond
      ((equal XvcVal 0 1E-4) (if (minusp YvcVal) (* pi 1.5) (* pi 0.5)))
      ((equal XvcVal -1 1E-4) pi)
      ((equal YvcVal 0 1E-4) (if (minusp XvcVal) pi 0))
      ((atan YvcVal XvcVal))
      )
      )

      Cheers
     
    Jürg Menzi, Jul 2, 2004
    #3
  4. BillZ

    Jürg Menzi Guest

    Hi Devin
    'atan' is an internal LISP function.

    Cheers
     
    Jürg Menzi, Jul 2, 2004
    #4
  5. It may be that your values are too close to zero for the computer to calculate properly.
    Multiply your values times 1E16 so that you are using reasonable numbers and see what you get.
    Then again, I just ran the function below and came up with 158.199, so you might want to verify your
    values.

    (rtd (atan 8.88 -22.2))

    (atan 8.88 -22.2) is equivalent to (atan -0.40) which is -21.8014 deg
    Note that 180.00 - 21.8104 = 158.1986

    It also may be the negative term on the second argument is confusing the function.

    Your second function, since you effectively converting the values to integers, is equivalent to
    (atan 0 0), which is not correct.
     
    Allen Johnson, Jul 2, 2004
    #5
  6. vec2ang doesn't work for his arguments, probably because his numbers are too small:

    Command: (rtd (vec2ang (list (cadr vect_1)(car vect_1))))
    270.0
    Should be -21.8014

    Not correct either:
    (rtd (vec2ang (list 8.88 -22.2)))
    -68.1986
     
    Allen Johnson, Jul 2, 2004
    #6
  7. Wait, I'm wrong. It is correct for the arguments given below. Sorry!
    Just not correct if you are substituting vec2ang for atan(x/y).
     
    Allen Johnson, Jul 2, 2004
    #7
  8. BillZ

    BillZ Guest

    Thanks,
    But I don't think I can use this function because I am looking for all 3d angles (from xy plane, xz plane, yz plane).
    Ang2Vec only returns 90d from my vector even when the angle from the xz plane I know is 53d..

    Bill
     
    BillZ, Jul 2, 2004
    #8
  9. It will work if you do something like:
    (vec2ang (list x y))
    (vec2ang (list x z))
    (vec2ang (list y z))
     
    Allen Johnson, Jul 2, 2004
    #9
  10. BillZ

    BillZ Guest

    That's it!

    Thanks.

    I'm kinda "learn as you go" to the extreme. :)

    Bill
     
    BillZ, Jul 2, 2004
    #10
  11. BillZ

    Devin Guest

    Jürg,

    OK I'm laughing trying not to feel stupid right now!

    Thanks :)
     
    Devin, Jul 2, 2004
    #11
  12. BillZ

    Doug Broad Guest

    Hi Juerg,
    I think that function has a logic flaw for the cases (x<1E-4 y=0.0)
    The built in function returns 0.0 for (atan 0.0 0.0). A
    substitute function should do the same(even though the
    case x=0, y=0 is illogical.)
    The function also has a logic flaw for the case (x=0.0 and y<0)
    Atan returns -pi/2. The substitute returns 1.5pi.

    In fact, any atan substitute function is likely to be more error
    prone than the atan function itself because it needs to make
    assumptions that are not justifiable. The simple fact is that
    as both x and y distances grow smaller in absolute value,
    the probable angular error grows. Who can predict the true
    direction. Rounding values off or substituting standard quadrant
    values makes unreasonable assumptions.

    Here are a few vector to angle functions that don't make any
    assumptions other than the vector is a list of three numbers.

    ;;Vector to Angle Functions
    (defun XYAng (vec)(atan (cadr vec) (car vec)))
    (defun YZAng (vec)(atan (caddr vec) (cadr vec)))
    (defun XZAng (vec)(atan (caddr vec) (car vec)))


    Regards,
    Doug
     
    Doug Broad, Jul 2, 2004
    #12
  13. Doug, your methods aren't precisely correct either. See my other post:

    "(atan 8.88 -22.2) is equivalent to (atan -0.40) which is -21.8014 deg
    Note that 180.00 - 21.8104 = 158.1986"

    But if you actually plot that vector, the correct angle should be -68.1986 deg

    The problem with the two argument atan function is that it doesn't appear to consider which value is
    negative when the result of x/y is negative.
     
    Allen Johnson, Jul 2, 2004
    #13
  14. Never mind.... if the vector is (8.88 -22.2), the correct formula is (atan -22.2 8.88) =
    (atan -2.5) = -68.1986
    Sorry.
     
    Allen Johnson, Jul 2, 2004
    #14
  15. BillZ

    Doug Broad Guest

    Hi Allen,
    The problem is that the precision in the original numbers is unknown.
    Multiplying very small numbers by very large numbers does not
    improve the precision. For angular accuracy to be meaningful,
    the length of the vectors must be larger than the measurement
    precision. The XY angle was probably thus indeterminate.

    Even the XZ angle and YZ angle could not be determined because
    the X and Y values were too small to make a conclusion about
    what their true sign should have been.

    Bill's original numbers: (-2.22045e-015 8.88178e-016 4.24264)
    look precise but looks can be deceiving. True precision comes
    by knowing the repeatability of the measurement. Since the
    X and Y numbers are so small, there should be a reasonable
    doubt about their precision. Numbers that close to zero could
    either be positive or negative or exactly 0. That greatly affects
    direction. Only Bill can tell us how the numbers were generated
    and what precision is reasonable. If an error in distance measurement
    is +-1/4", then some rounding is reasonable before applying
    the atan function. The sign of a number should not be assumed
    if the absolute value of a number is smaller than its precisiion.

    Bill made some incorrect assumptions in the original post about what
    the angle "should be" with the given atan input. Somewhere prior to
    the atan call, the numbers should be processed in some way. A general
    function, such as that posted by Juerg/Luis makes generalized assumptions
    that aren't necessarily reasonable in every case.

    The same thing happens when surveying students and scientists want
    to make the results look more accurate (or more reliable) by "fudging".

    Remember: "GIGO"
    Garbage In Garbage Out

    Regards,
    Doug
     
    Doug Broad, Jul 2, 2004
    #15
  16. BillZ

    BillZ Guest

    Just to shed more light on the situation:

    (setq p1 (getpoint "\nPick Base point: <> ")
    p2 (getpoint "\nPick vector direction point: <> "))

    (setq vect_1 (mapcar '- p2 p1)
    xy_ang (atan (cadr vect_1)(car vect_1)) ;angle in xy plane (z axis).
    xz_ang (atan (caddr vect_1)(cadr vect_1)) ;angle from xz plane (x axis).
    yz_ang (atan (caddr vect_1)(car vect_1)) ;angle from yz plane (y axis).
    )
    There were 2 points in 3d space (a rectangle that stands in the xz plane in the z direction).
    I'm new at this vector stuff so I might be all wrong on this.
    But I'm trying to know what angle I need to rotate (by way of rotation matrix) the rectangle to lay it down to the xy plane no matter how it's aligned to the WCS. In other words I'm trying to "read" the rectangle to know what angles I need to change.
    I've been lurking here to see what develops.

    Thanks

    Bill
     
    BillZ, Jul 2, 2004
    #16
  17. See my first port:

    "It may be that your values are too close to zero for the computer to calculate properly."
     
    Allen Johnson, Jul 2, 2004
    #17
  18. BillZ

    Doug Broad Guest

    Hi Bill,
    I'm not sure what you want to do (can't visualize).
    You've got your xz_ang and yz_ang mixed up in
    your last post:
    x = (car vec)
    y = (cadr vec)
    z = (caddr vec)

    If you can establish the UCS in the plane of the
    2dpolyline, you should be able to use my
    UCS2WCS routine (posted previously in another thread)
    to create a translation matrix and then use the transformby
    method.

    When I'm lazy though, I just set the UCS to align
    with the object and cutclip. Then I set WCS
    current and pasteclip.

    Good luck



    matter how it's aligned to the WCS. In other words I'm trying to "read" the rectangle to know what angles I need to change.
     
    Doug Broad, Jul 2, 2004
    #18
  19. BillZ

    BillZ Guest

    Thanks,
    We were looking at the angle differently. Where I was seeing the angle form the xz plane not the xz axis angle.

    I been looking at the ucs's also and know what you mean.
    I'm off for a week now so I'll think about it....


    Bill
     
    BillZ, Jul 3, 2004
    #19
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.