Skip to content

Instantly share code, notes, and snippets.

@XerxesZorgon
Created July 6, 2022 00:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
# 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) )
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment