Hey, friar. I stumbled across this old post of yours, and found a way to shorten it WAY up. alot of redundant code in there. check out the shortened code:
Code:
/*******************************
here is the new, cleaned up code
*******************************/
float find_quad(vector orig, vector dest, float view)
{
float axis_1_a = 0;
float axis_1_b = 0;
float axis_2_a = 0;
float axis_2_b = 0;
if (view == 1)
{
axis_1_a = orig_y;
axis_1_b = orig_z;
axis_2_a = dest_y;
axis_2_b = dest_z;
}
if (view == 2)
{
axis_1_a = orig_x;
axis_1_b = orig_z;
axis_2_a = dest_x;
axis_2_b = dest_z;
}
if (view == 3)
{
axis_1_a = orig_x;
axis_1_b = orig_y;
axis_2_a = dest_x;
axis_2_b = dest_y;
}
if ((axis_2_a < axis_1_a) && (axis_2_b < axis_1_b))
{
return 1;
}
if ((axis_2_a < axis_1_a) && (axis_2_b > axis_1_b))
{
return 2;
}
if ((axis_2_a > axis_1_a) && (axis_2_b > axis_1_b))
{
return 3;
}
if ((axis_2_a > axis_1_a) && (axis_2_b < axis_1_b))
{
return 4;
}
}
much shorter (43 vs 80 lines), and easier to read
Here it is, but for your 3rd function.
originally 79 lines long, now only 56 lines long, excluding comments and spaces.
Code:
/*
axis_1_a = orig_orig_y; //A
axis_1_b = orig_orig_z; //B
axis_2_a = dest_y; //C
axis_2_b = dest_z; //D
/*
Pattern:
Denominators (per quad calc) are always the same, regardless of view
views 2 & 3 have identical calculation per quadrant. only difference is what A thru D get set to.
view 1: quads 2 & 4 are identical calcs to view 2 & 3.
view 1: quads 1 & 3 are opposite calcs to view 2 & 3.
structure:
if view == x (1 2 or 3)
{
assign axis_X_Y
} //end view assignments
if quadrant == 1 or 3
if view == 1
{ reversed numerator compared to view 2 & 3 }
else
{ assign temp }
if quadrant == 2 or 4
{ assign temp }
//quad1 (A-C) / sqrt( (C-A)^2 + (D-B)^2 ) //exception: view(1), numerator is flipped
//quad2 (D-B) / sqrt( (C-A)^2 + (B-D)^2 )
//quad3 (C-A) / sqrt( (A-C)^2 + (D-B)^2 ) //exception: view(1), numerator is flipped
//quad4 (B-D) / sqrt( (A-C)^2 + (B-D)^2 )
*/
float find_sin_num(entity orig, vector dest, float quadrant, float view)
{
vector orig_orig = orig.getWorldOrigin();
float A, B, C, D;
float numerator, denominator;
if (view == 1)
{
A = orig_orig_y; //A
B = orig_orig_z; //B
C = dest_y; //C
D = dest_z; //D
}
if (view == 2)
{
A = orig_orig_x;
B = orig_orig_z;
C = dest_x;
D = dest_z;
}
if (view == 3)
{
A = orig_orig_x;
B = orig_orig_y;
C = dest_x;
D = dest_y;
}
if ( quadrant == 1)
{
if ( view == 1)
{
return (C - A) / sys.sqrt (((C - A)*(C - A)) + ((D - B)*(D - B)) ) ;
}
else
{
return (A - C) / sys.sqrt (((C - A)*(C - A)) + ((D - B)*(D - B)) ) ;
}
}
if (quadrant == 2)
{
return (D - B) / sys.sqrt (((C - A)*(C - A)) + ((B - D)* (B - D)) );
}
if (quadrant == 3)
{
if ( view == 1)
{
return (A - C) / sys.sqrt ( ((A-C)*(A-C)) + ((B-D)*(B-D)) );
}
else
{
return (C - A) / sys.sqrt ( ((A-C)*(A-C)) + ((B-D)*(B-D)) );
}
}
if (quadrant == 4)
{
return (B - D) / sys.sqrt (((A-C)*(A-C)) + ((B-D)*(B-D)) );
}
}
IMHO, much easier to read, because you can follow the algorithm process visually much easier.
Last but not least, is the point at point function: it's a short cleanup, but an efficient one:
the following:
Code:
angle = arcsin(angle, 0.25);
if (quad == 2)
{
angle = angle + 90;
}
if (quad == 3)
{
angle = angle + 180;
}
if (quad == 4)
{
angle = angle + 270;
}
can become:
Code:
angle = arcsin(angle, 0.25);
angle = angle + (( quad - 1 ) * 90); // angle = angle + 90*0 or 1, 2 3, essentially: angle + 90, 180, 270.
effective and cuts ~36 lines out because you use those ~12 lines thrice in your function.
easier to read, too.
Anyway. I know it's ~2 years after the fact, but maybe some new coder will find it interesting to see how to optimize their code a bit, and make it easier to read.