What is the y of the hexagon each point falls in?
Tableau
HEXBINY([Value], [Ratio])SQL (DuckDB)
WITH c AS (
SELECT id, Value AS x, Ratio AS y
FROM input
), h AS (
SELECT
id,
x,
y,
3 * floor(x / 3 + 0.5) AS ax,
sqrt(3) * floor(y / sqrt(3) + 0.5) AS ay,
3 * floor((x - 1.5) / 3 + 0.5) + 1.5 AS bx,
sqrt(3)
* floor((y - sqrt(3) / 2) / sqrt(3) + 0.5)
+ sqrt(3)
/ 2 AS b_y
FROM c
)
SELECT
CASE
WHEN pow(x - ax, 2) + pow(y - ay, 2) <= pow(x - bx, 2) + pow(y - b_y, 2) THEN round(ay, 3)
ELSE round(b_y, 3)
END AS result
FROM h
ORDER BY idRow 3 (0.125, 1) is nearest the centre (0, 1.732); row 8 (16, -0.8) is nearest (16.5, -0.866).
| Value | Ratio | Result |
|---|---|---|
| 2.5 | 0.5 | 0 |
| −2.5 | −0.5 | 0 |
| 0.13 | 1 | 1.73 |
| −0.4 | −1 | −1.73 |
| 3.75 | 0 | 0 |
| null | ||
| 0 | 1.5 | 1.73 |
| 16 | −0.8 | −0.87 |
The Tableau formula and the SQL return the same 8 rows.