N-dimensional sorting and color

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)
}

SpiralPixels--screenshot

1 Like

Hi,

Wow, this is great. I love it, thanks so much.

Of course now i am way off the base from what i started with since now we are in the realm of “pure” image manipulation and has nothing or almost nothing with the initial question i was interested in. But now that you opened a can of worms with the spiral decomposition, i was thinking to use other functions that traverse all the pixels in a matrix.

The easiest one is the “worm” - in the sense that you “walk” down the 1st column and up the second column and so on (or you can do left to right and right to left on rows).

So for same Majuro image, if you do the “worm” on columns you get this:

And now if you use the above image as input but do the “worm” on rows you get this rendering:

My next trick would be to do a “worm” that walks on diagonal.

Thanks,
Monica

1 Like

Interesting!

You might be interested in term “boustrophedon” – for reading and writing systems that switch back and forth “as the ox plows a field.”

HI,

I didn’t know there is such a term as boustrophedon, or that it was an ancient way to write. My husband who is an art historian actually knew the term :wink: And i should have realized that nowadays we call the same pattern “lawn mowing” pattern - i guess we don’t have too many oxes (?) in the fields anymore :wink:

My diagonal “lawn mowing” pattern function is done but the results are not very exciting in my opinion. I guess i need to think a little bit more about it.

Have a nice weekend,
Monica

Hi again,

I guess boredom at home does it - i decided to use the new diagonal decomposition function as before, meaning doing sorting on it and write the pixels in that order instead of moving pixels on the diagonal.
Now i got some better results. First the usual Majuro image sorted on diagonal:


And now the same diagonal sorting but on the image that first was sorted by luminosity:

Thanks,
Monica

Thanks for sharing this! Are you still working in straight R, or in Processing.R, or in something else?

If I haven’t already shared it – you might be interested in some of the approaches explored here: