Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# XerxesZorgon/mondrianRects.r

Created Jul 6, 2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 # Generate Mondrian dimensional rectangles from ASCII characters of input string # # Parameters # ---------- # inStr: Input string used to generate rectangles. Each character is converted # to ASCII, length, width of rectangle determined by log-normal draw # such that rectangle area approximates ASCII code # nIter: Number of log-normal draws. Gives user ability to select dimensions # meanlog, sdlog: mean and std dev of log-normal distribution # # Returns # ------- # Displays letter, possible rectangle dimensions and remainder # # Example # ------- # inStr <- "Welcome friends!" # nIter <- 5 # meanlog <- 0.8492164 # sdlog <- 0.7437608 # # printRects(inStr,nIter,meanlog,sdlog) # [1] "W" # [1] "(x,y) = [13, 6], rmdr = 9" # [1] "(x,y) = [9, 8], rmdr = 15" # [1] "(x,y) = [17, 4], rmdr = 19" # [1] "(x,y) = [14, 5], rmdr = 17" # [1] "(x,y) = [15, 5], rmdr = 12" printRects <- function(inStr,nIter,meanlog,sdlog) { # Convert input string to ASCII inAsc <- CharToAsc(inStr) nChar <- length(inAsc) # For each character, n, take nIter random draws of a log-normal distribution # Rectangle dimension is x * (x/r) = n => x = sqrt(r*n). Reduce by 90%. for(k in 1:nChar) { # Print letter print(c(substr(inStr,k,k))) # ASCII representation of kth letter n <- inAsc[k] # Get nIter random draws r <- rlnorm(nIter,meanlog,sdlog) # Generate rectangle dimensions and remainder for(j in 1:nIter) { # If r[j] < 1, invert to make greater than 1 if(r[j] < 1) r[j] <- 1/r[j] # x,y dimensions of rectangle x <- round(0.9 * sqrt(n*r[j])) y <- round(x / r[j]) # Remainder rmdr <- n - (x*y) # Display results print( sprintf("(x,y) = [%d, %d], rmdr = %d", x,y,rmdr) ) } } }
to join this conversation on GitHub. Already have an account? Sign in to comment