diff --git a/NEWS.md b/NEWS.md index 1285b29..824afc6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -66,6 +66,8 @@ Bug fixes and minor improvements * Now allows alpha values to be specified within the `fill` (and `fill2`) colour strings for `gridpattern_plasma()` and `gridpattern_gradient()` (when `use_R4.1_gradients=FALSE`). +* In `pattern_square()` for the "horizontal" and "vertical" types the value `1L` is + now guaranteed to be in the center of the pattern (#46). gridpattern v0.2.1 ================== diff --git a/R/pattern_square.R b/R/pattern_square.R index fbdbe1f..1ae59ab 100644 --- a/R/pattern_square.R +++ b/R/pattern_square.R @@ -123,9 +123,15 @@ pattern_horizontal <- function(subtype = NULL, nrow = 5L, ncol = 5L) { if (is.null(subtype) || is.na(subtype)) subtype <- 3L stopifnot(is_integer(subtype)) n <- as.integer(subtype) - s <- seq.int(n) - v <- rep(s, length.out = ncol) - v <- rep(v, length.out = nrow) + if (nrow > 2L && n > 1L) { + v1 <- rev(rep(c(seq.int(n, 2L, -1L), 1L), length.out = nrow %/% 2)) + v2 <- rep(seq.int(n), length.out = (nrow %/% 2) + (nrow %% 2)) + v <- c(v1, v2) + } else { + s <- seq.int(n) + v <- rep(s, length.out = nrow) + } + v <- rep.int(v, ncol) matrix(v, nrow = nrow, ncol = ncol) } @@ -170,9 +176,15 @@ pattern_vertical <- function(subtype = NULL, nrow = 5L, ncol = 5L) { if (is.null(subtype) || is.na(subtype)) subtype <- 3L stopifnot(is_integer(subtype)) n <- as.integer(subtype) - s <- seq.int(n) - v <- rep(s, length.out = nrow) - v <- rep(v, length.out = ncol) + if (ncol > 2L && n > 1L) { + v1 <- rev(rep(c(seq.int(n, 2L, -1L), 1L), length.out = ncol %/% 2)) + v2 <- rep(seq.int(n), length.out = (ncol %/% 2) + (ncol %% 2)) + v <- c(v1, v2) + } else { + s <- seq.int(n) + v <- rep(s, length.out = ncol) + } + v <- rep.int(v, nrow) matrix(v, nrow = nrow, ncol = ncol, byrow = TRUE) } diff --git a/tests/text_diagrams/vertical.txt b/tests/text_diagrams/vertical.txt index b397d9b..a69f368 100644 --- a/tests/text_diagrams/vertical.txt +++ b/tests/text_diagrams/vertical.txt @@ -1,11 +1,11 @@ > pss("vertical", nrow = 7, ncol = 9) / - - - - - - - - - \ -| 1 2 3 1 2 3 1 1 2 | -| 1 2 3 1 2 3 1 1 2 | -| 1 2 3 1 2 3 1 1 2 | -| 1 2 3 1 2 3 1 1 2 | -| 1 2 3 1 2 3 1 1 2 | -| 1 2 3 1 2 3 1 1 2 | -| 1 2 3 1 2 3 1 1 2 | +| 3 1 2 3 1 2 3 1 2 | +| 3 1 2 3 1 2 3 1 2 | +| 3 1 2 3 1 2 3 1 2 | +| 3 1 2 3 1 2 3 1 2 | +| 3 1 2 3 1 2 3 1 2 | +| 3 1 2 3 1 2 3 1 2 | +| 3 1 2 3 1 2 3 1 2 | \ - - - - - - - - - /