Example:
Lets say i have a canvas of 512x512 resolution . I am trying to get it's coordinates as follows :
First step : Divide each canvas into Cartesian coordinate quadrants
_____ _____ | | | | 2 | 1 | |_____|_____| | | | | 3 | 4 | |_____|_____|
Second step : Divide each quadrant to another four quadrants and add new quadrant id after the old one , like this :
_____ _____ _____ _____ | | | | | | 22 | 21 | 12 | 11 | |_____|_____|_____|_____| | | | | | | 23 | 24 | 13 | 14 | |_____|_____|_____|_____| | | | | | | 32 | 31 | 42 | 41 | |_____|_____|_____|_____| | | | | | | 33 | 34 | 43 | 44 | |_____|_____|_____|_____|
And so on...
_____ _____ _____ _____ _____ _____ _____ _____ | | | | | | | | | | 222 | 221 | 212 | 211 | 122 | 121 | 112 | 111 | |_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 223 | 224 | 213 | 214 | 123 | 124 | 113 | 114 | |_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 232 | 231 | 242 | 241 | 132 | 131 | 142 | 141 | |_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 233 | 234 | 243 | 244 | 133 | 134 | 143 | 144 | |_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 322 | 321 | 312 | 311 | 422 | 421 | 412 | 411 | |_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 323 | 324 | 313 | 314 | 423 | 424 | 413 | 414 | |_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 332 | 331 | 342 | 341 | 432 | 431 | 442 | 441 | |_____|_____|_____|_____|_____|_____|_____|_____| | | | | | | | | | | 333 | 334 | 343 | 344 | 433 | 434 | 443 | 444 | |_____|_____|_____|_____|_____|_____|_____|_____|
Until number of quadrants is equal to the number of pixels
I am trying to wrap my head here, about how to implement this functionality.
First i thought of doing a function which will get an imageData
index and return it's quadrant id . But this way i'll have to do some heavy(?) computation each time the function is called .
Or To generate a new array from imageData
and access its elements by index when i need them .
I'm sure there are a couple of ways one could tackle this problem . A recursive approach is interesting , is it possible on a bigger canvas ?
Friend of mine pointed me to the following piece of code that does something very similar , but i am struggling to follow whats going on here :
for (var y = 0; y < 512; y++)
for (var x = 0; x < 512; x++){
var s = "";
for (var b = 1; b <= 256; b *= 2){
var yb = y & b;
var xb = x & b;
s = String.fromCharCode(49 + (xb != yb) + yb / b * 2) + s;
}
}
I don't get the binary math , or the magic 49
number ? which is a string of "1"
. is it a good idea to use it as starting point ?