|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
How do you find the angle from (X, Y)?
![]() And how do you optimize this for realtime on the FRC? ![]() And One Of Astronouth's Rare Contensts: Why would I care? ![]() |
|
#2
|
|||||
|
|||||
|
Re: Angle from X,Y
Angle from (x,y) (I'm assuming on a normal Cartesian Plane) to what in relation to what as a vertex?
ie. angle between (x,y) and x-axis with origin as vertex. |
|
#3
|
|||||
|
|||||
|
Re: Angle from X,Y
The angle between (0,0) and (x,y) in relation to the x axis (A horizantal line, y=0)
|
|
#4
|
||||
|
||||
|
Re: Angle from X,Y
The inverse tangent function (arctan) will give you the angle from the origin to a point. For example: given (x,y), arctan(y/x) = the angle from the origin to the point (x,y).
If you search CD for something like "arctan" or "trig" or "trigonometry", you should get a fair amount of results already covering implementation of trigonometric functions on the FRC controller. I don't have any experience using these, myself, but I've heard that a few teams have had success with using an arbitrary degree Taylor series approximation of trig functions. |
|
#5
|
|||||
|
|||||
|
Re: Angle from X,Y
Doesn't arc tangent return an angle for a given slope? As in: -90 to 90.
I'm talking about 360 degrees. Like a Rectangular to Polar converter thing. |
|
#6
|
||||||
|
||||||
|
Re: Angle from X,Y
Quote:
|
|
#7
|
|||||
|
|||||
|
:WHAP: Duh
Code:
unsigned int GetAngle(signed int X, signed int Y)
{
signed int Angle = arctan(Y/abs(X));
unsigned char OtherSide = IsNegetive(X);
unsigned int Value = 0;
if (OtherSide)
Value = 180 - Angle + 360;
else
Value = Angle + 360;
Value %= 360;
return Value;
}
[Changed that tan to arctan] Last edited by Astronouth7303 : 04-10-2004 at 09:01 PM. Reason: Big boo-boo |
|
#8
|
||||
|
||||
|
Re: Angle from X,Y
I don't know much about programming, but I screwed around with winamp AVS for a while, and on that there was a function known as "atan2(x,y)" which was the same as atan but it automatically corrected for quadrants... isn't there anything simular in C? I mean, C is much more powerful than that AVS junk from what I understand, it should at least have the same capabilities.
|
|
#9
|
|||||
|
|||||
|
Re: Angle from X,Y
Quote:
Quote:
Last edited by The Lucas : 04-10-2004 at 11:05 PM. |
|
#10
|
|||||
|
|||||
|
Re: Angle from X,Y
This is the formula that LeoM posted a while back to get the angle from rectangular coordinates. Worked extremely well back in 2002
Temp1 was the positive distance in X direction Temp2 was the positive distance in Y direction temp3 = (temp2*127) / SQR((temp1*temp1)+(temp2*temp2)) 'Make values on a 127 unit circle ss_degrees = ((1-(temp3/118))*(((temp3+181)*temp3+744)/769))+((temp3/118)*(((((3*temp3-707)*temp3+42611)/20))+((temp3-115)/3))) ss_degrees = (ss_degrees*180)/128 'Convert to Degrees |
|
#11
|
|||||
|
|||||
|
Re: Angle from X,Y
Ay. The problem is I don't get what it's doing.
|
|
#12
|
|||||
|
|||||
|
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;
}
|
|
#13
|
|||||
|
|||||
|
Re: Angle from X,Y
Quote:
The best advice I've ever heard about optimization is "Don't -- until you need to." You generally want to get your code working, and only then worry about optimizing -- if necessary. |
|
#14
|
|||||
|
|||||
|
Re: Angle from X,Y
I was refeing to post 1:
Quote:
|
|
#15
|
|||||
|
|||||
|
Re: Angle from X,Y
Quote:
(I originally thought that line was part of your signature, so I just ignored it.)Last edited by Greg Ross : 04-13-2004 at 10:28 PM. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PBASIC ARCTAN function | Greg Ross | Programming | 14 | 05-11-2003 12:28 PM |
| The angle of going up the Ramp | David Bryan | Programming | 2 | 01-21-2003 08:56 PM |
| Slotted Angle Aluminum | hinge87 | OCCRA | 1 | 10-09-2002 06:15 PM |
| Is an angle bracket allowed? | archiver | 2001 | 0 | 06-24-2002 12:58 AM |
| 45 degree PVC angle fittings: which? | archiver | 2001 | 2 | 06-23-2002 10:53 PM |