Congratulations! It looks great.
Now that you have your R method – you can install Processing.R mode into Processing PDE, cut and paste your method into the sketch, and use that to execute.
Apologies for my rusty R:
# SpiralPixels
# 2020-04-16 Processing.R
# spiraldecomp by Pisicandru
# sketch wrapper jeremydouglass
# see discussion:
# https://discourse.processing.org/t/n-dimensional-sorting-and-color/19711
iw <- 320
ih <- 210
settings <- function() {
size(iw*2, ih)
}
setup <- function() {
background(255)
img <- loadImage("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Sunset_-_Majuro.jpg/320px-Sunset_-_Majuro.jpg")
image(img, 0, 0)
path <- spiraldecomp(iw,ih)
stdout$println(paste("len:", length(path[["yc"]])))
loadPixels()
w2 <- as.integer(width/2)
for(i in 1:length(path[["xr"]])){
x <- as.integer(path[["xr"]][i])
y <- as.integer(path[["yc"]][i])
x2 = as.integer(i %% iw)
y2 = as.integer(i/iw)
set(w2 + x, y, get(x2, y2))
}
saveFrame("SpiralPixels--screenshot.png")
}
spiraldecomp <- function(r1, c1){
require(pracma)
# for any matrix, with no. rows r1 > 3, and no.cols c1 >3
new <- data.frame()
if(r1<=c1) n1 <- ceil(r1/2) else n1 <- ceil(c1/2)
if(r1<= c1 & mod(r1, 2)==1 | r1 > c1 & mod(c1,2)==1){
for (i in 1:(n1-1)){
yc1 <- seq(i, (c1-i))
xr1 <- rep(i, length(yc1))
xr2 <- seq(i, (r1-i))
yc2 <- rep((c1-i+1), length(xr2))
yc3<- seq((c1-i+1),(i+1))
xr3 <- rep((r1-i+1), length(yc3))
xr4 <- seq((r1-i+1), (i+1))
yc4 <- rep(i, length(xr4))
m1 <- data.frame(xr=c(xr1, xr2, xr3, xr4), yc=c(yc1, yc2, yc3, yc4))
new <- rbind(new, m1)
}
i<- n1
if(r1<=c1){
if (c1-n1!=1) yc1 <- seq(i, c1-1) else yc1 <- i
xr1 <- rep(i, length(yc1))
} else {
if(r1-n1!=1) xr1 <- seq(i, r1-1) else xr1 <- i
yc1 <- rep(i, length(xr1))
}
m1 <- data.frame(xr=xr1, yc=yc1)
new <- rbind(new, m1)
} else {
for (i in 1:n1){
yc1 <- seq(i, (c1-i))
xr1 <- rep(i, length(yc1))
xr2 <- seq(i, (r1-i))
yc2 <- rep((c1-i+1), length(xr2))
yc3<- seq((c1-i+1),(i+1))
xr3 <- rep((r1-i+1), length(yc3))
xr4 <- seq((r1-i+1), (i+1))
yc4 <- rep(i, length(xr4))
m1 <- data.frame(xr=c(xr1, xr2, xr3, xr4), yc=c(yc1, yc2, yc3, yc4))
new <- rbind(new, m1)
}
}
return(new)
}