Trig functions missing?

Discussion in 'AutoCAD' started by Kent Cooper, AIA, Feb 4, 2005.

  1. I was pondering what would be needed to code up the
    divide-the-lot-into-two-equal-areas thing, the formula for which uses the
    tangents of two angles. I was surprised to find that AutoLisp doesn't have
    a tangent function!

    It can give you the SINE of an angle (sin) and the COSINE (cos), and the
    ARCtangent (atan), but those seem to be the only trigonometric functions
    it's heard of. Why it would have arctangent and not tangent is beyond me,
    as well as why it would have arctangent but not arcsine or arccosine. Help
    didn't lead me to anything, including in ActiveX/VBA.

    It's work-around-able, by getting the x- and y-differences of an angled line
    or between two points, and dividing the y by the x, and also through the
    fact that SIN / COS = TAN. But is some tangent function really available,
    hidden away somewhere? Am I missing something?
     
    Kent Cooper, AIA, Feb 4, 2005
    #1
  2. Kent Cooper, AIA

    Doug Broad Guest

    Kent,
    No tangent is not built-in, perhaps because some input values lead to crashes.
    Its relatively easy to create the missing functions. Modify as necessary
    to accomodate the problem inputs.

    (defun tan (x)(/ (sin x)(cos x))) ;watch out for pi/2 and 1.5 pi.

    (defun cot (x) (/ 1 (tan x))) ;watch out for 0 and pi

    (defun asin (x) (angle '(0 0) (list 0 x)));watch out for x>1

    etc...



    (defun
     
    Doug Broad, Feb 4, 2005
    #2
  3. Kent Cooper, AIA

    Doug Broad Guest

    Ignore the asin function. Its wrong.
     
    Doug Broad, Feb 4, 2005
    #3
  4. Kent Cooper, AIA

    BillZ Guest

    Here's what I use:

    Code:
    ;Trig functions in AutoLISP
    ;adapted from CADalyst tip 442, by John Howard
    ;
    ;CO-TANGENT
    (defun cot (x)
    (cond
    ((equal (sin x) 0.0 1.0e-16)
    (if (minusp x)
    -1.0e200
    1.0e200
    )
    )
    (T
    (/ (cos x) (sin x))
    )
    )
    )
    ;
    ;CO-SECANT
    (defun csc (x)
    (cond
    ((equal (sin x) 0.0 1.0e-16)
    (if (minusp x)
    -1.0e200
    1.0e200
    )
    )
    (T
    (/ 1.0 (sin x))
    )
    )
    )
    ;
    ;SECANT
    (defun sec (x)
    (cond
    ((equal (sin x) 0.0 1.0e-16)
    (if (minusp x)
    -1.0e200
    1.0e200
    )
    )
    (T
    (/ 1.0 (cos x))
    )
    )
    )
    ;
    ;TANGENT
    (defun tan (x)
    (cond
    ((equal (cos x) 0.0 1.0e-16)
    (if (minusp x)
    -1.0e200
    1.0e200
    )
    )
    (T
    (/ (sin x) (cos x))
    )
    )
    )
    ;
    ;TAND - degrees used for input.
    (defun tand (d)
    (/ (sin (* (/ d 180.0) pi))(cos (* (/ d 180.0) pi)))
    )
    ;
    ;ARC COSECANT
    (defun acsc (x)
    (cond
    ((equal x 1.0 1.0e-16)
    (* pi 0.5)
    )
    ((equal x -1.0 1.0e-16)
    (* pi -0.5)
    )
    ((> (abs x) 1.0)
    (atan (/ (/ 1.0 x) (sqrt (- 1.0 (/ 1.0 (* x x))))))
    )
    (T
    (prompt "\n*ERROR* (abs x) < 1.0 from ACSC function\n")
    )
    )
    )
    ;
    ;ARC COSINE
    (defun acos (x)
    (cond
    ((equal x 1.0 1.0e-16)
    0.0
    )
    ((equal x -1.0 1.0e-16)
    pi
    )
    ((< (abs x) 1.0)
    (- (* pi 0.5) (atan (/ x (sqrt (- 1.0 (* x x))))))
    )
    (T
    (prompt "\n*ERROR* (abs x)  > 1.0 from ACOS function\n")
    )
    )
    )
    ;
    ;ARC SECANT
    (defun asec (x)
    (cond
    ((equal x 1.0 1.0e-16)
    0.0
    )
    ((equal x -1.0 1.0e-16)
    pi
    )
    ((> (abs x) 1.0)
    (- (* pi 0.5) (atan (/ (/ 1.0 x) (sqrt (- 1.0 (/ 1.0 (* x x)))))))
    )
    (T
    (prompt "\n*ERROR* (abs x) < 1.0 from ASEC function\n")
    )
    )
    )
    ;
    ;ARC SINE
    (defun asin (x)
    (cond
    ((equal x 1.0 1.0e-16)
    (* pi 0.5)
    )
    ((equal x -1.0 1.0e-16)
    (* pi -0.5)
    )
    ((< (abs x) 1.0)
    (atan (/ x (sqrt (- 1.0 (* x x)))))
    )
    (T
    (prompt "\n*ERROR* (abs x) > 1.0 from ASIN function\n")
    )
    )
    )
    ;
    (princ "\nCOT, CSC, SEC, TAN, TAND, ACSC, ACOS, ASEC, ASIN \n")
    (prin1)
    Bill
     
    BillZ, Feb 4, 2005
    #4
  5. Probably because (as BillZ pointed out) all other trigonometric functions are at
    their root derived from sine and cosine.

    Matt

     
    Matt Stachoni, Feb 4, 2005
    #5
  6. Hi,

    "Probably because (as BillZ pointed out) all other trigonometric functions
    are at their root derived from sine and cosine."

    I think I'd say "can be" rather than "are"

    tan and the three inverses of tan, cos and sin are equally viable initial
    definitions of relationships between triangle sides.

    If you decided TAN was the prime definition you could have:

    sin = tan/(1 + tan^2) and
    cos = 1/(1 + tan^2)

    --


    Laurie Comerford
    CADApps
    www.cadapps.com.au
     
    Laurie Comerford, Feb 5, 2005
    #6
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.