我想在我的代码中实现一个简单的函数来获取像素坐标列表,这些像素坐标是围绕中心点(x,y或线性RGBA字节数组,但我可以稍后转换)的特定大小的(假设)六边形。
也许有一个我没有考虑过的简单的解决方案。你能想出一种巧妙的方式来实现它吗?
发布于 2014-04-14 12:14:05
你真正需要的只是一个六边形象限中的像素列表。然后,您可以简单地“反射”x和y坐标,以获得完整的六边形(当然,受屏幕边界的限制)。
首先,我会断言我想让我的六边形的一条边水平对齐。我还假设“我的六边形的大小”是指从我的六边形的中心到底边(水平对齐)的垂直线的长度(我们称之为L)。然后,假设原点(0,0)是我的中心点,我会对这个大小为L的六边形进行代数和三角运算。
我知道,边界[0,0,L/sqrt(3),L]内的所有点(即x-偏移,y-偏移,宽,高)肯定在我的六边形内。因此,将所有这些要点添加到我的列表中。
List<Point> pointsInHexagonQuadrant = new List<Point>();
for (int i = 0; i < L/Math.Sqrt(3); i++) //I'm ignoring any casting, you may have to fix.
{
for (int j = 0; j <= L; j++)
{
pointsInHexagonQuadrant.Add(new Point(i,j));
}
}我通过trig和代数知道我的六边形的最右边的点在( 2*L/sqrt(3) ,0),从L/sqrt(3)到2*L/sqrt(3)六边形的斜边的方程是y=sqrt(3)*x-2*L。我想要所有y坐标小于那个值的点。
for(int i = L/Math.Sqrt(3); i <= 2*L/Math.Sqrt(3); i++)
{
for (int j = 0; j <= Math.Sqrt(3)*i-2*L; j++)
{
pointsInHexagonQuadrant.Add(new Point(i,j));
}
}加上这个点,你就有一个六边形的象限,如下所示:
(0,0) (2L/sqrt(3),0)
---------------
| /
| /
| /
| /
| /
|-------/
(0,L) (L/sqrt(3),L) 为了得到完整的六边形,你可以在x轴和y轴上“反射”...
List<Point> pointsInMyHexagon = new List<Point>();
foreach (Point p in pointsInHexagonQuadrant)
{
pointsInMyHexagon.Add(new Point(p.X,p.Y));
pointsInMyHexagon.Add(new Point(-p.X,p.Y));
pointsInMyHexagon.Add(new Point(p.X,-p.Y));
pointsInMyHexagon.Add(new Point(-p.X,-p.Y));
}现在偏移六边形,将中心放回(x,y)点上。
foreach (Point p in pointsInMyHexagon)
{
p.Offset(myCenterPoint.X, myCenterPoint.Y);
}这可能是粗糙的,但这个概念应该是可行的。
https://stackoverflow.com/questions/23050853
复制相似问题