We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I have 2 questions regarding how to slice images, and randomly move the tiles around on a grid. I've looked at some posts, which helped me (notably ), and looking at 2 dimensional arrays, but I'm stuck and need help.
Thank you kindly for your help!!
PImage img;
PImage[] pieces = new PImage[16];
void setup() {
size(800, 800);
img = loadImage("http://3.bp.blogspot.com/-fGkhXK9efpM/TmaRMG2mQpI/AAAAAAAAB5w/zEjlbty8zGM/s1600/searichter.jpg");
pieces[0] = img.get(0, 0, img.width/4, img.height/4);
pieces[1] = img.get(img.width/4, 0, img.width/4, img.height/4);
pieces[2] = img.get(img.width/4*2, 0, img.width/4, img.height/4);
pieces[3] = img.get(img.width/4*3, 0, img.width/4, img.height/4);
pieces[4] = img.get(0, img.height/4, img.width/4, img.height/4);
pieces[5] = img.get(img.width/4, img.height/4, img.width/4, img.height/4);
pieces[6] = img.get(img.width/4*2, img.height/4, img.width/4, img.height/4);
pieces[7] = img.get(img.width/4*3, img.height/4, img.width/4, img.height/4);
pieces[8] = img.get(0, img.height/4*2, img.width/4, img.height/4);
pieces[9] = img.get(img.width/4, img.height/4*2, img.width/4, img.height/4);
pieces[10] = img.get(img.width/4*2, img.height/4*2, img.width/4, img.height/4);
pieces[11] = img.get(img.width/4*3, img.height/4*2, img.width/4, img.height/4);
pieces[12] = img.get(0, img.height/4*3, img.width/4, img.height/4);
pieces[13] = img.get(img.width/4, img.height/4*3, img.width/4, img.height/4);
pieces[14] = img.get(img.width/4*2, img.height/4*3, img.width/4, img.height/4);
pieces[15] = img.get(img.width/4*3, img.height/4*3, img.width/4, img.height/4);
frameRate(0.5);
}
void draw() {
for (int i=0; i < pieces.length; i++) {
image(pieces[i], (round(random(0, 3))*img.width/4), (round(random(0, 3))*img.height/4));
}
}
Answers
oops, forgot the posts that I looked at:
http://forum.processing.org/one/topic/slicing-and-tiling-images.html http://forum.processing.org/one/topic/how-to-make-a-jigsaw.html
Simplified the code a little. Gonna try a loop for the slicing creation next time! =:)
Finally I was able to make a loop to slice the image! \m/
Also correcting division for JS Mode and code accepts any GRID value for jig-saw slicing now! =D>
BtW, check it online at the link below:
studio.processingtogether.com/sp/pad/export/ro.9$MTikWt80-9w/latest
GoToLoop, thank you so much for your help! It works like a charm, and I'm learning quite a bit from your code, which is indeed much more flexible than my first attempt. Just for clarification, am I understanding this correctly:
Also, why do you use ' += ' instead of just ' + ' in the above section? Lastly, and this should really be my last question, is there a way to slow the shuffling down without affecting the global frameRate() ? Thanks a million!!!!
B/c it's shorter than
jy = jy + jigY
! :-jhttp://processing.org/reference/addassign.html
Pay attention that it's ->
jy += jigY
! It means jy points to the next row below after it!Constant DIFF isn't a cell/piece size. It's a poor correction for the integer division between img.width & GRID! :(
You see, with
jigX = img.width/GRID
,jixX*GRID
isn't exactly img.width! It's <= img.width!!! @-)That it so b/c the fraction part is discarded in the division part! And your source image is very irregular -> 806x809!
jy = 0
means back to the top row. Andjx += jigX
means it's now at next column.jy += jigY
after GRID times should be near or equal to img.height!So it's reached the bottom. and needs to get back to top row!!! :D
Version 2.5 is following the same order established at version 2.3:
Top to bottom row, and then next column! Look at there to figure out the pattern better! ;-)
P.S.: Version 2.6 -> To be more flexible w/ any GRID & img.height, variable diff is derived from jigY now: *-:)
final int diff = img.height - (jigY>>2);
if ((jy += jigY) > diff) {
Thanks again for being patient and thorough GoToLoop, that does indeed make sense. :-B
Taking a closer look at the code agin, I've come to realize that this display portion: :D
could be adapted to replace this whole complicated slicing creation portion: X_X
So here's new simplified v2.70: ^:)^
slick! Any thoughts on slowing down the shuffling speed (i.e. about once every 2 seconds), without affecting frameRate() ? =D>
How about this ->
if (frameCount % FREQ != 0) return;
:bzJust adjust FREQ in relation to FPS. The greater FREQ compared to FPS, the slowest shuffle gets! :D
So for example,
FREQ = 4*FPS
gets ya 1 shuffle each 4 seconds:Almost there... First, thanks again (big time), and that makes sense. I probably should have been a little clearer in regards to shuffling speed. I achieved the same thing with a counter, whereas your solution is much more elegant. But, lastly, I plan on having the grid move in space, for which I would need to put background() inside void draw(). In the current situation, the grid flashes up for one frame every few seconds. What is the best approach for displaying the tiles constantly, but only changing their position every few seconds. Again, thank you already for the obvious time and effort you put into this!!! I shall be eternally grateful ^:)^
You gotta take a snapshot of current canvas every time you change the tiles.
Then store it in a PImage variable, and use that as background()! :-\"
Cool, sorry to keep bugging, but the problem is partially existent in the following way. Let's say I have a global variable r , which I decrease to move the entire grid by translate( 0, 0, r); Now it only updates that screen along with the grid according to FREQ. void draw() {
Is there a way to move/transform the whole thing (which now works beautifully, btw) independently of FREQ ? Or am I missing something?
Maybe this is getting a little complex, but what if I created a class for each tile, have it move (rather than shuffle) around the grid in certain time increments and load each instance with a different tile from pieces[i] ?