六角网格坐标到像素坐标

发布于 2024-08-25 05:33:35 字数 456 浏览 7 评论 0原文

我正在使用六角形网格。我选择使用这个坐标系是因为它非常优雅。

网格

这个问题讨论了生成坐标本身,并且非常有用。我现在的问题是将这些坐标与实际像素坐标相互转换。我正在寻找一种简单的方法来找到坐标为 x,y,z 的六边形的中心。假设像素坐标中的 (0,0) 位于十六进制坐标中的 (0,0,0) 处,并且每个六边形都有长度为 s 的边。在我看来,x、y 和 z 应该分别沿着轴将坐标移动一定距离,但它们以一种奇怪的方式相互关联,我无法完全理解它。

如果您可以朝另一个方向并将像素坐标中的任何 (x,y) 点转换为该点所属的十六进制,则可获得奖励积分。

I am working with a hexagonal grid. I have chosen to use this coordinate system because it is quite elegant.

grid

This question talks about generating the coordinates themselves, and is quite useful. My issue now is in converting these coordinates to and from actual pixel coordinates. I am looking for a simple way to find the center of a hexagon with coordinates x,y,z. Assume (0,0) in pixel coordinates is at (0,0,0) in hex coords, and that each hexagon has an edge of length s. It seems to me like x,y, and z should each move my coordinate a certain distance along an axis, but they are interrelated in an odd way I can't quite wrap my head around it.

Bonus points if you can go the other direction and convert any (x,y) point in pixel coordinates to the hex that point belongs in.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2024-09-01 05:33:35

为了清楚起见,让“六边形”坐标为 (r,g,b),其中 rgb code> 分别是红色绿色蓝色坐标。坐标 (r,g,b)(x,y) 之间的关系如下:

y = 3/2 * s * b
b = 2/3 * y / s
x = sqrt(3) * s * ( b/2 + r)
x = - sqrt(3) * s * ( b/2 + g )
r = (sqrt(3)/3 * x - y/3 ) / s
g = -(sqrt(3)/3 * x + y/3 ) / s

r + b + g = 0

推导:

  • 我首先注意到任何水平行的六边形(应具有恒定的 y 坐标)都有一个恒定的 b 坐标,因此 y 仅取决于 b。每个六边形可以分成六个边长为s的等边三角形;一排六边形的中心比下一行的中心高/低一个半边长(或者,也许更容易看出,一排的中心比两行中心高/低 3 个边长) ),因此对于 b1 的每次更改,y 都会更改 3/2 * s,给出第一个公式。根据 y 求解 b 给出第二个公式。

  • 具有给定 r 坐标的六边形的中心都位于垂直于 r 轴的线上,且位于 r 轴上的点 3/2 处* s 从原点(类似于上面 yb 方面的推导)。 r 轴的斜率为 -sqrt(3)/3,因此垂直于它的线具有斜率 sqrt(3)r 轴上和线上的点的坐标为 (3sqrt(3)/4 * s * r, -3/4 * s * r);因此,对于包含具有 r 坐标 r 的六边形中心的线,在 xy 中的方程为y + 3/4 * s * r = sqrt(3) * (x - 3sqrt(3)/4 * s * r)。使用第一个公式替换y并求解x得出第二个公式。 (这不是我实际推导这个的方式,但我的推导是图形化的,经过大量的试验和错误,并且这种代数方法更简洁。)

  • 具有给定 r 坐标的六边形集合是具有该 g 坐标的六边形集合的水平反射,因此无论 x 坐标以 rb 表示的公式是什么,用 g 代替 r 的该公式的 x 坐标将相反。这给出了第三个公式。

  • 第四个和第五个公式是将第二个公式替换为 b 并根据 x 求解 rgy

  • 最终的公式来自观察,并通过代数与早期公式进行验证。

For clarity, let the "hexagonal" coordinates be (r,g,b) where r, g, and b are the red, green, and blue coordinates, respectively. The coordinates (r,g,b) and (x,y) are related by the following:

y = 3/2 * s * b
b = 2/3 * y / s
x = sqrt(3) * s * ( b/2 + r)
x = - sqrt(3) * s * ( b/2 + g )
r = (sqrt(3)/3 * x - y/3 ) / s
g = -(sqrt(3)/3 * x + y/3 ) / s

r + b + g = 0

Derivation:

  • I first noticed that any horizontal row of hexagons (which should have a constant y-coordinate) had a constant b coordinate, so y depended only on b. Each hexagon can be broken into six equilateral triangles with sides of length s; the centers of the hexagons in one row are one and a half side-lengths above/below the centers in the next row (or, perhaps easier to see, the centers in one row are 3 side lengths above/below the centers two rows away), so for each change of 1 in b, y changes 3/2 * s, giving the first formula. Solving for b in terms of y gives the second formula.

  • The hexagons with a given r coordinate all have centers on a line perpendicular to the r axis at the point on the r axis that is 3/2 * s from the origin (similar to the above derivation of y in terms of b). The r axis has slope -sqrt(3)/3, so a line perpendicular to it has slope sqrt(3); the point on the r axis and on the line has coordinates (3sqrt(3)/4 * s * r, -3/4 * s * r); so an equation in x and y for the line containing the centers of the hexagons with r-coordinate r is y + 3/4 * s * r = sqrt(3) * (x - 3sqrt(3)/4 * s * r). Substituting for y using the first formula and solving for x gives the second formula. (This is not how I actually derived this one, but my derivation was graphical with lots of trial and error and this algebraic method is more concise.)

  • The set of hexagons with a given r coordinate is the horizontal reflection of the set of hexagons with that g coordinate, so whatever the formula is for the x coordinate in terms of r and b, the x coordinate for that formula with g in place of r will be the opposite. This gives the third formula.

  • The fourth and fifth formulas come from substituting the second formula for b and solving for r or g in terms of x and y.

  • The final formula came from observation, verified by algebra with the earlier formulas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文