SQL Spatial Puzzle #2: The Hunter

(Click here for Puzzle #1 – the disappearing square)

There seem to be several variations on this puzzle, but the version I know is as follows:

“A hunter starts at a location. He walks one mile south, one mile east, and then one mile north. He ends up back where he started, and sees a bear. What colour is the bear?”

The Solution

Before worrying about the colour of the bear, we need to resolve the apparent paradox in the positioning of the hunter. He has moved one mile south, one mile east, and one mile north, and ended up back where he started. Where could he have been in order for this to be true?

The most commonly-known answer is that the hunter started at the North Pole, the bear is therefore a polar bear and is white (don’t let it worry you that there are no polar bears at the north pole!). Let’s test whether this answer fits the criteria using SQL Server’s geography datatype.

Firsty, we define the hunter’s start point, @p0, at the North Pole. The latitude of the North Pole is 90 degrees. All lines of longitude converge at the North Pole, so you can use any longitude value. I’ll use an arbitrary longitude value of 0:

DECLARE @p0 geography = geography::Point(90, 0, 4326);

We now need to declare the first point the hunter travels to, @p1, which is one mile due South of the start.

  • Travelling south reduces latitude, so @p1.Lat < @p0.Lat.
  • Travelling due south involves maintaining a constant longitude, so @p1.Long = @p0.Long.
  • The coordinates in this example use SRID 4326, so the results of any linear calculations are stated in metres. 1 mile = 1,609 metres (approx.), so we need @p0.STDistance(@p1) = 1609.
  • Solving these criteria leads to the following point:
DECLARE @p1 geography = geography::Point(89.98559, 0, 4326);

The next point that the hunter travels to lies one mile due east of @p1. Therefore, it must lie on the same latitude as @p1, but have a greater longitude (assuming that the antimeridian is not crossed). Using the same logic for the previous point, we can deduce that this point lies at:

DECLARE @p2 geography = geography::Point(89.98559, 60, 4326);

Finally, we need to establish the final point, @pf, which must lie one mile due north of the last point, as follows:

DECLARE @pf geography = geography::Point(90, 60, 4326);

To meet the final criteria of the puzzle, the final point must be the same as the point at which the hunter stated, so @pf = @p0.

We can prove that the points described above meet the criteria for the puzzle as follows:

DECLARE @p0 geography = geography::Point(90, 0, 4326);
DECLARE @p1 geography = geography::Point(89.98559, 0, 4326);
DECLARE @p2 geography = geography::Point(89.98559, 60, 4326);
DECLARE @pf geography = geography::Point(90, 60, 4326);

IF @p1.Lat  @p1.Long
AND CAST(@p2.STDistance(@p1) AS INT) = 1609
SELECT '@p2 lies one mile due east of @p1';

IF @pf.Lat > @p2.Lat
AND @pf.Long = @p2.Long
AND CAST(@pf.STDistance(@p2) AS INT) = 1609
SELECT '@pf lies one mile due north of @p2';

IF @pf.STEquals(@p0) = 1
SELECT '@pf is the same as @p0';

This proves that the route p0, p1, p2, pf meets all the criteria described in the riddle and, using the methods of the geography datatype, we have proven that the North Pole is a valid solution to this puzzle. However, it is not the only solution - there are, in fact, an infinite number of locations on the earth's surface that would meet the criteria of the puzzle...

...consider the second step of the hunter's journey - walking due east along a parallel of the earth - a line of constant latitude. As you approach the poles, the circumference of the parallels of the earth become increasingly smaller. Starting at a point just north of the south pole, it is possible to walk east right around the earth along a circle that is one mile in circumference. This occurs at a latitude of about -89.9977, as can be shown by the following:

DECLARE @linestring geography = geography::STLineFromText('
LINESTRING(0 -89.9977, 15 -89.9977, 30 -89.9977, 45 -89.9977, 60 -89.9977, 75 -89.9977, 90 -89.9977, 105 -89.9977, 115 -89.9977, 120 -89.9977, 135 -89.9977, 150 -89.9977, 165 -89.9977, 180 -89.9977, 195 -89.9977, 200 -89.9977, 210 -89.9977, 225 -89.9977, 240 -89.9977, 255 -89.9977, 270 -89.9977, 285 -89.9977, 300 -89.9977, 315 -89.9977, 330 -89.9977, 345 -89.9977, 360 -89.9977)
', 4326);

SELECT 
@linestring,
@linestring.STLength();

This linestring is 1,609 metres long - one mile, but follows a line of constant latitude right around the earth. So, if the hunter starts at any point that is one mile north of this line, they would first walk one mile south onto the line, then one mile east along the line (taking them once around the earth), and finally one mile north again to return to their original starting point.

The following code illustrates an example of just one such point that lies one mile north of the line:

DECLARE @q0 geography = geography::Point(-89.98329, 0, 4326);
SELECT @q0.STDistance(@linestring);

Therefore, an alternative solution to the puzzle could be as follows:

DECLARE @q0 geography = geography::Point(-89.98329, 0, 4326);
DECLARE @q1 geography = geography::Point(-89.9977, 0, 4326);
DECLARE @q2 geography = geography::Point(-89.9977, 360, 4326);
DECLARE @qf geography = geography::Point(-89.98329, 360, 4326);

It’s harder to prove this solution with SQL Server because, since walking east along the line between @q1 and @q2 takes you on an entire revolution of the earth, the result reported by STDistance() for the distance between  the points will always be zero (rather than the 1,609 metres it actually took you to walk there).

In fact, not only are there an infinite number of points that lie one mile north of the line at constant latitude –89.9977 that satisfy the puzzle, but there are also an infinite number of lines along which it can be done: Consider the line that goes around the South pole that is 1/2 mile in length, for example, or 1/3 mile. Walking due east for one mile along any of these lines will take you on some number of rotations of the earth but always return you to the starting point. In all these cases, the middle step of "walking due east for one mile" effectively becomes nullified, leaving the puzzle as walking due south for one mile and then due north for one mile, which clearly returns you to the start location.

About these ads
This entry was posted in Spatial, SQL Server and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s