Hengefinder: The Geometry and Algorithms of Urban Solar Alignment

Hengefinder: The Geometry and Algorithms of Urban Solar Alignment

Twice a year, thousands of people gather in New York City for "Manhattanhenge," the brief moment when the setting sun aligns perfectly with the east-west grid of the city's streets. While the phenomenon is a local celebrity, the underlying geometry is universal. If the sun can align with a street in Manhattan, it can align with a street anywhere in the world—provided the street's bearing matches the sun's azimuth at sunset.

Building a tool to predict these moments, known as Hengefinder, requires bridging the gap between simple assumptions and the complex realities of spherical trigonometry and orbital mechanics. Here is a deep dive into the technical challenges involved in calculating urban solar alignments.

The Challenge of Road Bearing: Earth is Not Flat

To find a "henge" moment, the first step is determining the bearing of a street—its angle relative to true north. A common instinct is to take two points on a street (latitude and longitude), calculate the difference in coordinates, and use atan2 to find the angle. However, this assumes a Cartesian plane, which does not exist on a sphere.

The Longitude Convergence Problem

Latitude lines are evenly spaced, but longitude lines converge as they move toward the poles. For example, one degree of longitude spans roughly 52 miles in New York City but only 33 miles in Anchorage. If you treat these degrees as equal units in a trigonometric function, your bearing calculation will be skewed.

To solve this, longitude must be scaled by the cosine of the latitude. This puts both latitude and longitude into comparable units before calculating the angle:

# Corrected approach for short street segments
delta_y = lat_2 - lat_1
mean_lat = math.radians((lat_1 + lat_2) / 2)
delta_x = (lon_2 - lon_1) * math.cos(mean_lat)

bearing_rad = math.atan2(delta_x, delta_y)
bearing_deg = math.degrees(bearing_rad)

Redefining "Sunset" via Binary Search

Standard astronomical libraries, such as Python's Astral, define sunset as the moment the sun has fully dipped below the horizon. For a henge, this is too late; the goal is to find the moment the sun's disk is just touching the horizon.

Boundary Search vs. Value Search

Because the sun's altitude changes monotonically as it approaches the horizon, the problem is a perfect candidate for binary search. However, a standard binary search looks for a specific value. In this case, the developer needed a "last-true" binary search—finding the final minute where the sun's altitude is still above a specific target threshold.

Instead of searching for altitude == target, the algorithm evaluates a boolean: "Is the sun still above the target altitude?"

while left < right:
    mid = (left + right + 1) // 2  # Upper-biased midpoint
    if altitude > target:
        left = mid # Valid, could be the last one
    else:
        right = mid - 1 # Invalid, look earlier

Finding Alignment: The Two-Phase Search

Once the road bearing and the sun's azimuth (the angle of the sun relative to north) are known, the final step is finding the date when they match. This is non-trivial because the sun's azimuth over a year is non-monotonic—it moves in a smooth curve that reverses direction at the solstices.

Phase 1: Coarse Sampling

To avoid calculating the azimuth for every single day of the year (which would be computationally expensive and require excessive API calls), a coarse search is used, sampling every 30 days. The algorithm flags a window for further investigation if:

  1. The Intermediate Value Theorem applies: The sun's azimuth crosses the road bearing between two samples.
  2. A direction reversal occurs: The azimuth reverses direction, meaning a peak or valley was hit, which might have coincided with the road bearing.

Phase 2: Fine-Grained Search

Once a potential window is identified, the tool switches to a day-by-day search to pin down the exact date and time of the alignment.

Broader Applications and Limitations

While Hengefinder focuses on sunsets, the logic extends to other celestial bodies and alignments. A mobile version of the app has expanded this to include the moon and "Sauron Henges," where the sun or moon aligns perfectly with the top of a specific building.

Community Insights and Counterpoints

The project has sparked discussions regarding the practicalities of solar tracking. Some users pointed out that while binary search is an elegant programming exercise, the sun's position can be calculated directly using solar equations from sources like NOAA. Others noted the limitations of the current model:

"It doesn't work with curved streets."

Furthermore, the utility of such calculations extends beyond aesthetics. One commenter suggested an "inverse Hengefinder"—an app that predicts shadow zones in cities to help parents navigate heat-sensitive children through the coolest possible routes.

Conclusion

Urban henges are geometrically rare but happen constantly across the globe. From the straight canals of Amsterdam to the skyscrapers of New York, these moments are a result of the intersection between human urban planning and celestial mechanics. By treating the problem as a series of "closed boxes"—spherical geometry, boundary searches, and coarse-to-fine sampling—Hengefinder turns a complex astronomical problem into a reachable tool for urban exploration.

Sources