Freaking spent the past two weeks trying to solve the problem of inserting vertices into a list in sorted order w/o duplicates. I'm so out of practice in programming. I went about it in a way that was vastly more complicated than it
needed to be. In the end, it was really, really simple.
Code:
int vertex_find ( VRL **vrl, FVERTEX *v, int eol )
{
int beg = 0, mid, end;
int v_idx = -1;
if ( eol > 0 )
{
end = eol - 1;
mid = end / 2;
while ( beg <= end )
{
if( v->x < vrl[mid]->v.x )
end = mid - 1;
else if ( v->x > vrl[mid]->v.x )
beg = mid + 1;
else // We have a match on the x.
{
// Try and match the y.
if ( v->y < vrl[mid]->v.y )
end = mid - 1;
else if (v->y > vrl[mid]->v.y)
beg = mid + 1;
else // Matched the y, now, try and match the z.
{
if ( v->z < vrl[mid]->v.z )
end = mid - 1;
else if (v->z > vrl[mid]->v.z)
beg = mid + 1;
else
{
// we have a complete match.
// Force the loop to terminate.
beg = end + 1;
// Record the position in the list.
v_idx = mid;
}
}
}
mid = beg + (end - beg) / 2;
}
}
return v_idx;
}
I need to stop maiking it more difficult than it needs to be. I also need to make two more sort routines now. One that sorts by Y coords and another that sorts by Z. For this, it may suffice to make it generic, as I tried with the list builder, but since I'll be keying off one value, it won't be as complicated.