六角网格坐标到像素坐标
我正在使用六角形网格。我选择使用这个坐标系是因为它非常优雅。
这个问题讨论了生成坐标本身,并且非常有用。我现在的问题是将这些坐标与实际像素坐标相互转换。我正在寻找一种简单的方法来找到坐标为 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了清楚起见,让“六边形”坐标为
(r,g,b)
,其中r
、g
和b
code> 分别是红色、绿色和蓝色坐标。坐标(r,g,b)
和(x,y)
之间的关系如下:推导:
我首先注意到任何水平行的六边形(应具有恒定的
y
坐标)都有一个恒定的b
坐标,因此y
仅取决于b
。每个六边形可以分成六个边长为s
的等边三角形;一排六边形的中心比下一行的中心高/低一个半边长(或者,也许更容易看出,一排的中心比两行中心高/低 3 个边长) ),因此对于b
中1
的每次更改,y
都会更改3/2 * s
,给出第一个公式。根据y
求解b
给出第二个公式。具有给定 r 坐标的六边形的中心都位于垂直于 r 轴的线上,且位于 r 轴上的点
3/2 处* s
从原点(类似于上面y
在b
方面的推导)。r
轴的斜率为-sqrt(3)/3
,因此垂直于它的线具有斜率sqrt(3)
;r
轴上和线上的点的坐标为(3sqrt(3)/4 * s * r, -3/4 * s * r)
;因此,对于包含具有r
坐标r
的六边形中心的线,在x
和y
中的方程为y + 3/4 * s * r = sqrt(3) * (x - 3sqrt(3)/4 * s * r)
。使用第一个公式替换y
并求解x
得出第二个公式。 (这不是我实际推导这个的方式,但我的推导是图形化的,经过大量的试验和错误,并且这种代数方法更简洁。)具有给定
r
坐标的六边形集合是具有该 g 坐标的六边形集合的水平反射,因此无论x
坐标以r
和b
表示的公式是什么,用g
代替r
的该公式的x
坐标将相反。这给出了第三个公式。第四个和第五个公式是将第二个公式替换为
b
并根据x 求解
和r
或g
y
。最终的公式来自观察,并通过代数与早期公式进行验证。
For clarity, let the "hexagonal" coordinates be
(r,g,b)
wherer
,g
, andb
are the red, green, and blue coordinates, respectively. The coordinates(r,g,b)
and(x,y)
are related by the following:Derivation:
I first noticed that any horizontal row of hexagons (which should have a constant
y
-coordinate) had a constantb
coordinate, soy
depended only onb
. Each hexagon can be broken into six equilateral triangles with sides of lengths
; 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 of1
inb
,y
changes3/2 * s
, giving the first formula. Solving forb
in terms ofy
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 ther
axis that is3/2 * s
from the origin (similar to the above derivation ofy
in terms ofb
). Ther
axis has slope-sqrt(3)/3
, so a line perpendicular to it has slopesqrt(3)
; the point on ther
axis and on the line has coordinates(3sqrt(3)/4 * s * r, -3/4 * s * r)
; so an equation inx
andy
for the line containing the centers of the hexagons withr
-coordinater
isy + 3/4 * s * r = sqrt(3) * (x - 3sqrt(3)/4 * s * r)
. Substituting fory
using the first formula and solving forx
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 thex
coordinate in terms ofr
andb
, thex
coordinate for that formula withg
in place ofr
will be the opposite. This gives the third formula.The fourth and fifth formulas come from substituting the second formula for
b
and solving forr
org
in terms ofx
andy
.The final formula came from observation, verified by algebra with the earlier formulas.