What is the x of the hexagon each point falls in?
Tableau
HEXBINX([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 ax
ELSE bx
END AS result
FROM h
ORDER BY idRow 8 (16, -0.8) is nearer the offset centre (16.5, -0.866) than the grid centre (15, 0), so its x is 16.5.
| Value | Ratio | Result |
|---|---|---|
| 2.5 | 0.5 | 3 |
| −2.5 | −0.5 | −3 |
| 0.13 | 1 | 0 |
| −0.4 | −1 | 0 |
| 3.75 | 0 | 3 |
| null | ||
| 0 | 1.5 | 0 |
| 16 | −0.8 | 16.5 |
The Tableau formula and the SQL return the same 8 rows.