[Note: Today’s article is courtesy of Eric Sink, graciously covering for Alex while he is on vacation this week]

I have always found more benefit in making fun of myself than in making fun of others. So when Alex asked me to be Guest Editor on The Daily WTF for a day, I figured it wouldn't be hard to come up with something of my own that deserves public ridicule.

I have mentioned on several occasions that I am building a piece of woodworking software as a hobby project. Part of this effort required me to dig into the world of computational geometry and write a solid modeling library. I've written some really cool pieces of code.

And I've also written the snippet of code below.

public bool SegmentInside(Solid s, xyz p1, xyz p2)
{
  if (!PointInside(s, p1))
  {
    return false;
  }

  if (!PointInside(s, p2))
  {
    return false;
  }

  xyz v = p2 - p1;
  for (double t = 0.1; t <= 0.9; t += 0.1)
  {
    xyz p = p1 + t * v;
    if (!PointInside(s, p))
    {
      return false;
    }
  }
  return true;
}

Unless you work on CAD or game engine design, the grievousness of this piece of code may not be as obvious as some of the things that show up here on this website, so let me explain.

This function has a simple purpose: Given a 3D solid and 3D line segment, determine whether the segment lies entirely inside the solid.

If we knew the solid to be convex, the solution would be quite simple. In the convex case, if both end points are inside the solid, then the entire segment would be inside the solid as well. Since I already have a robust implementation of checking to see if a single point is inside a solid, SegmentInside would be trivial.

However, if the solid is concave, this doesn't work. Suppose the solid is a dinner fork and the line segment goes from one tine to another. Or suppose the solid is a donut with the segment going through the hole in the middle. In these examples, the segment obviously crosses through some space which is not inside the solid, even though both endpoints are.

A robust solution to this problem would have taken a lot of time, and I was in a hurry. So I wrote the code you see above, which simply checks 10 points along the interior of the line segment. As soon as it finds any point on the segment which is outside the solid, it returns false.

I was ashamed of myself when I wrote this, but the fact is that it allowed me to move forward and all of my unit tests pass.

Granted, it wouldn't be hard to create a unit test which causes this code to fail. In that case I would have to retaliate against myself by simply checking a much higher number of interior points on the line segment. Instead of 10 evenly spaced points, I would check 100 or a 1000. Then I would need another unit test.

Eventually I would end up with an implementation of SegmentInside which is robust for any practical situation even though it would require essentially infinite time for any case which returns true.

Anyway, from the perspective of someone who knows computational geometry, this snippet of code is shameful in the extreme, an obvious WTF. I've logged a bug to remind myself to fix it.