# N-dimensional sorting and color

**URL:** <https://discourse.processing.org/t/n-dimensional-sorting-and-color/19711>\
**Category:** Coding Questions\
**Created:** [April 12, 2020, 4:18pm UTC](https://discourse.processing.org/t/n-dimensional-sorting-and-color/19711 "2020-04-12T16:18:18Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 17, 2020, 12:35am UTC](https://discourse.processing.org/t/n-dimensional-sorting-and-color/19711/21 "2020-04-17T00:35:36Z")

</div>

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:

```auto
# 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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/42774a76e3b96a233351b452ae22b6e4039d53e6.jpeg)

---

<div class="post-metadata">

**Author:** ![Pisicandru](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@Pisicandru](https://discourse.processing.org/u/Pisicandru)\
**Post date:** [April 17, 2020, 8:03pm UTC](https://discourse.processing.org/t/n-dimensional-sorting-and-color/19711/22 "2020-04-17T20:03:25Z")

</div>

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:

 ![Majuro_wormColumn](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2f0185576225eac2f295d199cd49d45838204bff.jpeg)

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

 ![Majuro_wormRow_ofwormColumn](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/de5a953b0af592470805b87608d2fd57dac2a153.jpeg)

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

Thanks,  
Monica

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 17, 2020, 9:17pm UTC](https://discourse.processing.org/t/n-dimensional-sorting-and-color/19711/23 "2020-04-17T21:17:02Z")

</div>

Interesting!

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

> **[Boustrophedon](https://en.wikipedia.org/wiki/Boustrophedon)**
>
> Boustrophedon /ˌbuːstrəˈfiːdən/ (Ancient Greek: βουστροφηδόν, boustrophēdón "ox-turning" from βοῦς, bous, "ox", στροφή, strophē, "turn" and the adverbial suffix -δόν, "like, in the manner of"; that is, turning like oxen in ploughing) is a type of bi-directional text, mostly seen in ancient manuscripts and other inscriptions. Alternate lines of writing are flipped, or reversed, with reversed letters. Rather than going left-to-right as in modern European languages, or right-to-left as in A Many anc...

---

<div class="post-metadata">

**Author:** ![Pisicandru](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@Pisicandru](https://discourse.processing.org/u/Pisicandru)\
**Post date:** [April 19, 2020, 12:17am UTC](https://discourse.processing.org/t/n-dimensional-sorting-and-color/19711/24 "2020-04-19T00:17:42Z")

</div>

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 😉 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 😉

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

---

<div class="post-metadata">

**Author:** ![Pisicandru](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@Pisicandru](https://discourse.processing.org/u/Pisicandru)\
**Post date:** [April 19, 2020, 3:00pm UTC](https://discourse.processing.org/t/n-dimensional-sorting-and-color/19711/25 "2020-04-19T15:00:45Z")

</div>

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:

 ![Majuro_diag](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3cd7852bc9bcbb69cf5cb5ab445c3024e5e107c2.jpeg)  
And now the same diagonal sorting but on the image that first was sorted by luminosity:  
 ![Majuro_Lumdiag](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a3378fbc1d5260e23f8a03e874db7a63490e54ab.jpeg)

Thanks,  
Monica

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 19, 2020, 3:47pm UTC](https://discourse.processing.org/t/n-dimensional-sorting-and-color/19711/26 "2020-04-19T15:47:09Z")

</div>

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:

> **[jeffThompson/PixelSorting](https://github.com/jeffThompson/PixelSorting)**
>
> A set of experiments in pixel sorting using Processing - jeffThompson/PixelSorting

[Previous page](https://discourse.processing.org/t/n-dimensional-sorting-and-color/19711.md?page=1)
