Skip to content

Commit

Permalink
fix: 1 now guaranteed to be center of 'horizontal' and 'vertical' squ…
Browse files Browse the repository at this point in the history
…are patterns

* In `pattern_square()` for the "horizontal" and "vertical" types the value `1L` is
  now guaranteed to be in the center of the pattern (#46).
  • Loading branch information
trevorld committed Nov 29, 2021
1 parent bf8d342 commit d25835d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
==================
Expand Down
24 changes: 18 additions & 6 deletions R/pattern_square.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
14 changes: 7 additions & 7 deletions tests/text_diagrams/vertical.txt
Original file line number Diff line number Diff line change
@@ -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 |
\ - - - - - - - - - /

0 comments on commit d25835d

Please sign in to comment.