Thread: Angle from X,Y
View Single Post
  #12   Spotlight this post!  
Unread 13-04-2004, 07:34
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: Angle from X,Y

My current code is like this:
Code:
#define abs(Num) ( ((Num) < 0) ? (0 - (Num)) : (Num) )
#define IsNegetive(Num) ( ((Num) < 0) ? 255 : 0 )
#define IsPositive(Num) ( ((Num) > 0) ? 255 : 0 )



/* 360 degrees = 256
  0 =   0 (0x00)
 45 =  32 (0x20)
 90 =  64 (0x40)
135 =  96 (0x60)
180 = 128 (0x80)
225 = 160 (0xA0)
270 = 192 (0xC0)
315 = 224 (0xE0) */

unsigned char GetAngle(signed char X, signed char Y)
{
   signed int  Angle = 0;
 unsigned char OtherSide = (X < 0) ? 255 : 0;
 unsigned char Value = 0;
 unsigned char Simple = (X == 0) | (Y == 0) | (X == Y);
 
 //Perform Simple tests
 if (Simple)
 {
  if (X == 0)
   if (Y < 0)
    Angle = 192; // Down
   else if (Y > 0)
    Angle = 64; //Up
   else // Y == 0
    Angle = 0; //None, default
  
  if (Y == 0)
   if (X < 0)
    Angle = 128; // Left
   else if (X > 0)
    Angle = 0; //Right
   else // X == 0
    Angle = 0; //None, default
  
  if (X == Y)
  {
   switch( (IsNegetive(X) & 2) | (IsNegetive(Y) & 1) )
   {
    case 0: //++
     Angle = 32;
     break;
    case 2: //-+
     Angle = 96;
     break;
    case 3: //--
     Angle = 160;
     break;
    case 1: //+-
     Angle = 224;
     break;
   }
  }
 }

 //Again for goto, no braces
 if (Simple) goto Done; //I hope that's right
 
 if (X > Y) //Preserve accuracy
  Angle = 64 - atan(abs(X)/Y);
 else
  Angle = atan(abs(X)/Y);
 
 Angle = OtherSide ? (127 - Angle) : Angle;
 Value = (unsigned char)((Angle + 255) % 255);
 
Done:
 return Value;
}
Nobody's answered the last question!