Is this code correct or logically sound?
import std;
enum {////side/depth/height and side/height
x,//0
y,//1
z //2
}
/**
Indicates wether a triangle faces an imaginary view point.
*/
bool triangleFacesCamera(float[3] tA, float[3] tB, float[3] tC) {
float r1, r2;
r1 = atan2(tB[x] - tA[x], tB[z] - tA[z]);//tried swapping parameters without success
r2 = atan2(tC[x] - tB[x], tC[z] - tB[z]);//also tried with tA as substraction 2nd operand, with same/similar results.
//trying with both is still broken, but appears to change the breakage pattern.
r2 -= r1;
return r2 > 0;
}
For context, it is trying to reproduce an old algorithm that does the same thing :
//in D pseudo code
extern short sus(byte A, byte B){
r0 = sus(tB[y] - tA[y], tB[x] - tA[x]);
r1 = sus(tC[y] - tA[y], tC[x] - tA[x]);
r1 -= r0;
return r1 > 0;
}
sus is named so because it makes use of atan and a premade table that looks like tan(a*(a*5.09...)) but is impossible to exactly reproduce for me.(on a graph it looks like tan but crammed to loop only four/sixteen/sixtyfour times at τ/4,π,τ)
Trying to parse both atan2 and sus with (sin(angle), cos(angle)) as arguments[1] shows that atan2 outputs it as an angle in -π~π angle representation but sus's output differs in that it looks like sin(x*2).
I'd add images to better illustrate it but do not know how...
Big thanks
[1] https://run.dlang.io/is/xR8TcE
ps: is it normal that sometimes posting has a very long delay? when it happens it is as if the post has gone nowhere but then it suddenly appears much later, which can cause confusion and duplicate posting, oof.
Permalink
Reply