我正在寻找一种算法来计算落在矩形区域内的六边形,无论是否裁剪。
我知道以下内容:
rectWidth = 1280;
rectHeight = 720;
hexRadius = 50; // middle to corner
hexHeight = hexRadius * 2;
hexShortSpan = hexRadius * 0.5;
hexLongSpan = cos(radians(30)) * hexRadius;
hexWidth = hexLongSpan * 2;
hexSide = hexRadius + hexShortSpan; // this is not a side but side + shortSpan for horizontal rows
无法找出mod op以获得正确的结果。
float A = rectWidth / hexWidth;
float B = rectHeight / hexSide;
float hexCount = A * B +????;
// etc. etc. not sure about the rest...
罗布
这是一张图片......
答案 0 :(得分:0)
计算六边形的面积,并将矩形的面积除以六边形的面积。
六边形的区域是
a * a *(3 * sqrt(3))/ 2
其中a是六边形边长。
答案 1 :(得分:0)
提示:计算一侧十六角的数量。例如。计算所有十六进制右角(X'):...
左上角位于第一个六边形的中间,情况不太普遍。它可以用计数来解决:
这是代码的python版本:
from math import ceil, cos, sqrt
rectWidth = 1280
rectHeight = 720
hexRadius = 50
hexHeight = hexRadius * 2
hexHalfHeight = hexRadius/2
hexWidth = hexRadius * sqrt(3)
# First one is corner, ceil() calculates others in row
num_in_first_row = 1 + ceil( ( rectWidth - hexWidth ) /hexWidth )
num_in_second_row = ceil( rectWidth / hexWidth )
# First one is corner, ceil() starts from upper point of next hex on left border
num_of_first_rows = 1 + ceil( ( rectHeight - 2 * hexRadius ) / ( hexHeight + hexRadius ) )
# Starts from upper point of first hex in second row. Coords (hexWidth/2., hexHalfHeight)
num_of_second_rows = ceil( ( rectHeight - hexHalfHeight ) / ( hexHeight + hexRadius ) )
num = num_in_first_row * num_of_first_rows + num_in_second_row * num_of_second_rows
print num