绘制多边形网格的算法

时间:2014-11-06 21:03:08

标签: algorithm graphics grid polygons

是否有绘制n边多边形网格的算法。以下是六边形的示例:

enter image description here

1 个答案:

答案 0 :(得分:0)

这是我使用网格包的解决方案。

library(grid)
hexagon_layer <- function(x, y, r) {
    axis.x <- c(x, x + 3 * r, x + 6 * r, x + 9 * r, x + 12 * r, x + 15 * 
        r, x + 18 * r, x + 1.5 * r, x + 4.5 * r, x + 7.5 * r, x + 10.5 * 
        r, x + 13.5 * r, x + 16.5 * r, x + 1.5 * r, x + 4.5 * r, x + 7.5 * 
        r, x + 10.5 * r, x + 13.5 * r, x + 16.5 * r)
    axis.y <- c(rep(y, 7), rep(y + r * sqrt(3)/2, 6), rep(y - r * sqrt(3)/2, 
        6))
    hexagon <- function(x, y, r) {
        grid.path(x = c(x + 0.5 * r, x + r, x + 0.5 * r, x - 0.5 * r, x - 
            r, x - 0.5 * r), y = c(y + r * sqrt(3)/2, y, y - r * sqrt(3)/2, 
            y - r * sqrt(3)/2, y, y + r * sqrt(3)/2))
    }
    for (i in 1:length(axis.x)) {
        hexagon(axis.x[i], axis.y[i], r = 0.05)
        next
    }
}
for (i in seq(1, 21, by = 2)) {
    hexagon_layer(x = 0.05, y = i * 0.05 * sqrt(3)/2, r = 0.05)
    next
}