Skip to content

Instantly share code, notes, and snippets.

@Introscopia
Created March 4, 2023 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Introscopia/496c8a597b7f80d13458f320c53da1e3 to your computer and use it in GitHub Desktop.
Save Introscopia/496c8a597b7f80d13458f320c53da1e3 to your computer and use it in GitHub Desktop.
Calculate the approximate heading of a 2D vector at 3 different levels of precision using binary trees
typedef struct vec2d_struct{
double x, y;
} vec2d;
double v2d_rough_heading8(vec2d v){
if( v.x > 0 ){
if( v.y > 0 ){
if( v.x < v.y ) return 1.1780972450961724;// 3/8 PI
else return 0.3926990816987241;// 1/8 PI;
}
else{//v.y < 0
if( v.x < -v.y ) return -1.1780972450961724;// -3/8 PI
else return -0.3926990816987241;// -1/8 PI
}
}
else{//v.x < 0
if( v.y > 0 ){
if( -v.x < v.y ) return 1.9634954084936207;// 5/8 PI
else return 2.7488935718910690;// 7/8 PI
}
else{//v.y < 0
if( v.x > v.y ) return -1.9634954084936207;// 5/8 PI
else return -2.7488935718910690;// 7/8 PI
}
}
}
double v2d_rough_heading16(vec2d v){
if( v.x > 0 ){
if( v.y > 0 ){
if( v.x < v.y ){
// tan(3/8 PI)
if( 2.414213 * v.x < v.y ) return 1.3744467859455345;// 7/16 PI
else return 0.9817477042468103;// 5/16 PI
}
else{// tan(1/8 PI)
if( 0.414213 * v.x < v.y ) return 0.5890486225480862;// 3/16 PI
else return 0.1963495408493620;// 1/16 PI
}
}
else{//v.y < 0
if( v.x < -v.y ){
// tan(3/8 PI)
if( 2.414213 * v.x < -v.y ) return -1.3744467859455345;// -7/16 PI
else return -0.9817477042468103;// -5/16 PI
}
else{// tan(1/8 PI)
if( 0.414213 * v.x < -v.y ) return -0.5890486225480862;// -3/16 PI
else return -0.1963495408493620;// -1/16 PI
}
}
}
else{//v.x < 0
if( v.y > 0 ){
if( -v.x < v.y ){
// -tan(3/8 PI)
if( -2.414213 * v.x < v.y ) return 1.7671458676442586;// 9/16 PI
else return 2.1598449493429828;// 11/16 PI
}
else{// -tan(1/8 PI)
if( -0.414213 * v.x < v.y ) return 2.5525440310417070;// 13/16 PI
else return 2.9452431127404311;// 15/16 PI
}
}
else{//v.y < 0
if( v.x > v.y ){
// tan(3/8 PI)
if( 2.414213 * v.x > v.y ) return -1.7671458676442586;// -9/16 PI
else return -2.1598449493429828;// -11/16 PI
}
else{// tan(1/8 PI)
if( 0.414213 * v.x > v.y ) return -2.5525440310417070;// -13/16 PI
else return -2.9452431127404311;// -15/16 PI
}
}
}
}
double v2d_rough_heading32(vec2d v){
if( v.x > 0 ){
if( v.y > 0 ){
if( v.x < v.y ){
// tan(3/8 PI)
if( 2.414213 * v.x < v.y ){
// tan(7/16 PI)
if( 5.027339 * v.x < v.y ) return 1.4726215563702154;//15/32 PI
else return 1.2762720155208536;//13/32 PI
}
else{
// tan(5/16 PI)
if( 1.496605 * v.x < v.y ) return 1.0799224746714913;//11/32 PI
else return 0.8835729338221293;// 9/32 PI
}
}
else{// tan(1/8 PI)
if( 0.414213 * v.x < v.y ){
// tan(3/16 PI)
if( 0.668178 * v.x < v.y ) return 0.6872233929727672;// 7/32 PI
else return 0.4908738521234052;// 5/32 PI
}
else{
// tan(1/16 PI)
if( 0.198912 * v.x < v.y ) return 0.2945243112740431;// 3/32 PI
else return 0.0981747704246810;// 1/32 PI
}
}
}
else{//v.y < 0
if( v.x < -v.y ){
// tan(3/8 PI)
if( 2.414213 * v.x < -v.y ){
// tan(7/16 PI)
if( 5.027339 * v.x < -v.y ) return -1.4726215563702154;//-15/32 PI
else return -1.2762720155208536;//-13/32 PI
}
else{
// tan(5/16 PI)
if( 1.496605 * v.x < -v.y ) return -1.0799224746714913;//-11/32 PI
else return -0.8835729338221293;// -9/32 PI
}
}
else{// tan(1/8 PI)
if( 0.414213 * v.x < -v.y ){
// tan(3/16 PI)
if( 0.668178 * v.x < -v.y ) return -0.6872233929727672;// -7/32 PI
else return -0.4908738521234052;// -5/32 PI
}
else{
// tan(1/16 PI)
if( 0.198912 * v.x < -v.y ) return -0.2945243112740431;// -3/32 PI
else return -0.0981747704246810;// -1/32 PI
}
}
}
}
else{//v.x < 0
if( v.y > 0 ){
if( -v.x < v.y ){
// -tan(3/8 PI)
if( -2.414213 * v.x < v.y ){
// -tan(7/16 PI)
if( -5.027339 * v.x < v.y ) return 1.6689710972195777;//17/32 PI
else return 1.8653206380689396;//19/32 PI
}
else{
// -tan(5/16 PI)
if( -1.496605 * v.x < v.y ) return 2.0616701789183018;//21/32 PI
else return 2.2580197197676637;//23/32 PI
}
}
else{// -tan(1/8 PI)
if( -0.414213 * v.x < v.y ){
// -tan(3/16 PI)
if( -0.668178 * v.x < v.y ) return 2.4543692606170260;//25/32 PI
else return 2.6507188014663878;//27/32 PI
}
else{
// -tan(1/16 PI)
if( -0.198912 * v.x < v.y ) return 2.8470683423157501;//29/32 PI
else return 3.0434178831651120;//31/32 PI
}
}
}
else{//v.y < 0
if( v.x > v.y ){
// tan(3/8 PI)
if( 2.414213 * v.x > v.y ){
// tan(7/16 PI)
if( 5.027339 * v.x > v.y ) return -1.6689710972195777;//-17/32 PI
else return -1.8653206380689396;//-19/32 PI
}
else{
// tan(5/16 PI)
if( 1.496605 * v.x > v.y ) return -2.0616701789183018;//-21/32 PI
else return -2.2580197197676637;//-23/32 PI
}
}
else{// tan(1/8 PI)
if( 0.414213 * v.x > v.y ){
// tan(3/16 PI)
if( 0.668178 * v.x > v.y ) return -2.4543692606170260;//-25/32 PI
else return -2.6507188014663878;//-27/32 PI
}
else{
// tan(1/16 PI)
if( 0.198912 * v.x > v.y ) return -2.8470683423157501;//-29/32 PI
else return -3.0434178831651120;//-31/32 PI
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment