Your skill will accomplishwhat the force of many cannot PerlMonks

### JPEG Zig-Zag

 on Oct 29, 2012 at 17:07 UTC Need Help??

For the last week I've been trying to figure out an algorithm for JPEG's zig-zag spacefilling curve. Specifically, I wanted an algorithm that maps all elements in an NxN matrix to a N*N array given their x and y values. I finally figured it out with the help of my analysis 101 TA. He's not a computer scientist, but he knows what pairing functions are. Anyway, after a few hours messing with perl, I made this subroutine:

```sub getPos{
(\$x,\$y,\$s)=@_;(\$d=\$x+\$y)*(\$d++)/2
+(\$d%2?\$y:\$x)-(\$d>\$s)*(\$d-\$s)**2
}

To use it, you do something like this:

```for(my \$x = 0; \$x < \$size; \$x++){
for(my \$y = 0; \$y < \$size; \$y++){
print getPos(\$x, \$y, \$size)."  ";
}
print "\n";
}

For \$size = 10 the loop will yield:

```0  1  5  6  14  15  27  28  44  45
2  4  7  13  16  26  29  43  46  63
3  8  12  17  25  30  42  47  62  64
9  11  18  24  31  41  48  61  65  78
10  19  23  32  40  49  60  66  77  79
20  22  33  39  50  59  67  76  80  89
21  34  38  51  58  68  75  81  88  90
35  37  52  57  69  74  82  87  91  96
36  53  56  70  73  83  86  92  95  97
54  55  71  72  84  85  93  94  98  99

The body of getPos has 65 (non-whitespace) characters, any ideas on how to get it even smaller?

UPDATE: This prints a JPEG zig-zag when \$ARGV[0] is the size of the matrix:
```\$==pop;print map{\$y=\$_;map((\$d=\$y+\$_)*\$d++/2-(\$d>\$=)*(\$d-\$=)**2+(\$d&1?
+\$_:\$y).\$",@m),\$/}@m=0..\$=-1
97 characters! I started at 125!

Create A New User
Node Status?
node history
Node Type: obfuscated [id://1001394]
Front-paged by Arunbear
help
Chatterbox?
 [Corion]: marioroy: Oh, that's always cool, having API-compatible modules. This makes testing and comparing things much easier [marioroy]: IPC in MCE::Shared can handle 400k (sends) per second. That's seems a lot for being a pure-Perl module. After making the release, will come back and post a solution for a node by a fellow wanting faster logging. [Corion]: While working on WWW::Mechanize:: Chrome, I had the suspicion that AnyEvent was doing something wrong, but I was able to swap it out for Mojolicious and the error persisted. [Corion]: Of course, the error was in my own code ;) [marioroy]: Corion, start and start_child in MCE::Hobo::Manager return a MCE::Hobo object, whereas P::FM returns the PID. I can have it return the PID though. I tried Hobo::Manager with several P::FM modules, just changed P::FM to MCE::Hobo::Manager and it works. [marioroy]: I also have a Hobo driver for Forklift allowing folks to use in multiple classes, no conflicts with one another. That's not possible for P::FM. [Discipulus]: congrats marioroy! [marioroy]: CORE::wait works well eventhough multiple instances or classes using Hobo::Manager. [Corion]: marioroy: I'm not sure what the normal use for the PID is in P:FM, but I guess that most programs just ignore or log it

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2017-05-26 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (189 votes). Check out past polls.