Author Topic: Angle to Direction Vector; a better way?  (Read 2258 times)

0 Members and 1 Guest are viewing this topic.

zoltan

  • Guest
Angle to Direction Vector; a better way?
« on: March 29, 2006, 11:18:09 AM »
The XDATA code 1013 stores a World Direction expressed as a unti vector.  The vector can be resolved to get an angle within the XY plane and an angle from the XY plane, or three angle frome each of the axes.  I needed one of these a while ago and I wrote a really fast, probably wrong, pair of functions to convert back and forth between the two.  Mine only handles 2D vector in the XY plane.

Is this the right idea? Does someone have a pair of functions in their library that will do this better?

Code: [Select]
;;returns a Direction Vector given and angle in XY plane
(Defun AngtoDir ( ANG )
 (Cond
  ((= ANG 0.0) ;0 angle
   '(1.0 0.0 0.0)
  )
  ((< ANG (/ PI 2.0)) ;1st quad
   (List (Cos ANG) (Sin ANG) 0.0 )
  )
  ((= ANG (/ PI 2.0)) ;90 angle
   '(0.0 1.0 0.0)
  )
  ((< (/ PI 2.0) ANG PI) ;2nd quad
   (List (- (Cos (- ANG (/ PI 2.0)))) (Sin (- ANG (/ PI 2.0))) 0.0 )
  )
  ((= ANG PI) ;180 angle
   '(-1.0 0.0 0.0)
  )
  ((< PI ANG (* PI 1.5)) ;3rd quad
   (List (- (Cos (- ANG PI))) (- (Sin (- ANG PI))) 0.0 )
  )
  ((= ANG (* PI 1.5)) ;270 angle
   '(0.0 -1.0 0.0)
  )
  ((< (* PI 1.5) ANG (* PI 2.0)) ;4th quad
   (List (Cos (- ANG (* PI 1.5))) (- (Sin (- ANG (* PI 1.5)))) 0.0 )
  )
  ((= ANG (* PI 2.0)) ;360 angle
   '(1.0 0.0 0.0)
  )
 )
)

;;returns an angle in XY plane given a 2D Direction Vector
(Defun DirtoAng ( VEC )
 (Cond
  ((And (Not (MinusP (Car VEC))) (ZeroP (Cadr VEC))) ;0 angle
   0.0
  )
  ((And (Not (MinusP (Car VEC))) (Not (MinusP (Cadr VEC)))) ;1st quad
   (ATan (/ (Cadr VEC) (Car VEC)) )
  )
  ((And (ZeroP (Car VEC)) (Not (MinusP (Cadr VEC)))) ;90 angle
   (/ PI 2.0 )
  )
  ((And (MinusP (Car VEC)) (Not (MinusP (Cadr VEC)))) ;2nd quad
   (+ (ATan (/ (Cadr VEC) (- (Car VEC)))) (/ PI 2.0) )
  )
  ((And (MinusP (Car VEC)) (ZeroP (Cadr VEC))) ;180 angle
   PI
  )
  ((And (MinusP (Car VEC)) (MinusP (Cadr VEC))) ;3rd quad
   (+ (ATan (/ (- (Cadr VEC)) (- (Car VEC)))) PI )
  )
  ((And (ZeroP (Car VEC)) (MinusP (Cadr VEC))) ;270 angle
   (* PI 1.5 )
  )
  ((And (Not (MinusP (Car VEC))) (MinusP (Cadr VEC))) ;4th quad
   (+ (ATan (/ (- (Cadr VEC)) (Car VEC))) (* PI 1.5) )
  )
 )
)

sinc

  • Guest
Re: Angle to Direction Vector; a better way?
« Reply #1 on: April 03, 2006, 09:17:25 PM »
Pardon me if I misunderstand, but is this what you're looking for?

Code: [Select]
;;returns a Direction Vector given and angle in XY plane (ANG)

(polar '(0 0) ANG 1)


;;returns an angle in XY plane given a 2D Direction Vector (VEC)

(angle '(1 0) VEC)


Beware floating-point errors (in other words, use the EQUAL function with fuzz factor to check for equalilty).

zoltan

  • Guest
Re: Angle to Direction Vector; a better way?
« Reply #2 on: April 04, 2006, 10:17:56 AM »
Well, now that is a really nice simple one I overlooked.