July 19th, 2023: Carpenters Tricks and Edge Cases

Recently doing a spate of coding challenges I came upon a question I rather liked:

Given the coordinates of four points in 2D space p1p2p3 and p4, return true if the four points construct a square.

Simple enough, right? In particular, with my background in Carpentry I thought I immediately had the solution. There is a great construction trick when building rectangular frame to check if they are true; Do not fiddle with a carpenters square, trying to check each corner. For anything larger than a small frame you may not be able to see the difference. Being just half a degree off of 90 over the course of 16 feet and you’d be off by more than an inch and a half. Instead, simply measure the length of the two diagonals: If they are equal then you know you have a rectangle*. Or so I thought.

The two diagonals above give different measurements, so you’d know you have a parallelogram instead of a rectangle.

Of course you also want to check that the other 4 sides are the same length right? Actually this may not be necessary if none of the points are coincidental. But, with just 4 arbitrary points we don’t have a way of measuring just the diagonals anyhow since we do not yet know which is which. “Easy enough”, I thought, “Ill simply compute the lengths for all 6 lines and add them to a set. If there are exactly two length measurements it must be a square right? Easy Peasy”

We start by making a function (d) to calculate the distance between two points using the quadratic formula. Here we can use a little shortcut by omitting taking the square root: we actually don’t care about the lengths, just a “class of lengths”. Then, simply check if there are two different lengths out of all six possible lines between the points.

Oh right. Those pesky coincidental points. Imagine you have 3 points that form an equilateral triangle, and a 4th point that coincides with one of the above. The distance between the three points is the same, and the distance from the null point to its partner is a second length (0). Ok, well we should just check for coincidental points before bothering to compute distance anyhow:

That should be all the edge cases right? “The 4 points are distinct and we know how to check if we have a rectangle using our diagonal trick and that the remaining sides are equal. I think that’s it.”

Aaaaand wrong. There is exactly one edge case that breaks the above method:

Two adjoined equilateral triangles clearly have 2 sets of measurements: five equal lines and one distinct longer diagonal. It was not enough to just count that we had 2 differing lengths. The fix is trivial, instead of checking we have 2 total lengths we instead actually bother to count them. We should have 4 equal sides and 2 equal, but distinct, diagonals to ensure we have a square.

A good reminder to consider all the possible edge cases and to not assume a simple solution necessarily covers all of them.

Corrected, and all together:

(input points are passed as lists of floats: [float, float])

Leave a comment