Is this code correct?

Dennis dkorpel at gmail.com
Thu Mar 30 12:59:31 UTC 2023


On Thursday, 30 March 2023 at 10:29:25 UTC, z wrote:
> Is this code correct or logically sound?

You need to be exact on what 'correct' is. The comment above 
`triangleFacesCamera` says:

> Indicates wether a triangle faces an imaginary view point.

There's no view point / camera position defined anywhere in your 
code.
Instead, all it's doing is comparing the angle of one triangle 
side (A->B) with another (B->C) in the XZ plane. This suggests 
you want to know the triangle winding order: clockwise or counter 
clockwise.

If you search for "triangle winding order" you'll find simple and 
correct ways to do that. Your code needlessly computes angles, 
only considers the XZ plane, and doesn't compare the angles 
correctly.

> r2 -= r1;
>
> return r2 > 0;

You need to wrap (r2 - r1) into [-τ/2, τ/2] range, then you can 
compare with 0.



More information about the Digitalmars-d-learn mailing list