Skip to content

Code stash

Erik Haliewicz edited this page Jul 24, 2023 · 5 revisions

fully 32-bit dot product line test (determine which side of a line a point is on)

s8 point_sign_fix32(f32 x, f32 y, f32 v1_x_f16, f32 v1_y_f16, f32 v2_x_f16, f32 v2_y_f16) {

   f32 ndx = v2_x - v1_x;
   if(ndx == 0) {
     f32 ndy = v2_y - v1_y;
     // special case 1
     if(x <= v1_x) {
       return ndy > 0;
     }
     return ndy < 0;
   }

   f32 ndy = v2_y - v1_y;
   if(ndy == 0) {
     // special case 2
     if (y <= v1_y) {
       return ndx < 0;
     }
     return ndx > 0;
   }

   f32 dx = (x - v1_x);
   f32 dy = (y - v1_y);

   if ( (ndy ^ ndx ^ dx ^ dy)&0x80000000 ) {
        if  ( (ndy ^ dx) & 0x80000000 ) {
            // (left is negative)
            return 1;
        }
        return 0;
    }
    

    f32 left = ndy*dx;
    f32 right = dy*ndx;

    if(right < left) {
        return 0;
    }

    return 1;
   
}

Clone this wiki locally