Hi all,
I am new to Processing. I would like to implement an animation, in which an image is built from the bottom tile by tile.
I have a 1920x1080 pixels input image and I divided it in tiles (20 tiles per row and 20 tiles per column). Now, I would like to implement an animation in which, starting from most-bottom row, each tile with (x,y) coordinates in input image is shown moving from (x,0) to (x,y).
I wrote a Tile class in which i provide start and destination coordinates, the speed and the function to update the coordinates. Then, I created a Tile matrix in which every element is an instance of Tile and I tried to draw the animation. However, things did not go as I expected. Firstly, because tiles are shown from the first row to the last one; moreover, the output image is not built tile by tile, but row by row. Could anyone help me?
Thanks so much.
Here is the code.
class Tile {
int xPos, yPos, xD, yD;
int pW, pH;
float speed;
Tile (int startX, int startY, int dstX, int dstY, float s, int width, int height) {
xPos = startX;
yPos = startY;
xD = dstX;
yD = dstY;
pW = width;
pH = height;
speed = s;
}
void update() {
yPos += speed;
if (yPos > yD) {
yPos = yD;
}
}
void display(){
//print("xD=",xD, " yD=",yD,"\n");
PImage onePiece = img.get(xD,yD, pieceW, pieceH);
image(onePiece, xPos, yPos);
}
}
int w=1920, h=1080;
int nRows = 20, nCols = 20;
int currentPixelRow = h, currentPixelCol = 0;
int thisRow=0, thisCol=0;
int pieceW = 96, pieceH = 54;
int previousDisplayTime, deltaTime;
int drawX=0,drawY=h-pieceH;
PImage img,back;
PImage[][] segments = new PImage[nRows][nCols];
Tile[][] pieces;
void setup(){
pieces = getPieces();
size(1920, 1080);
frameRate(30);
previousDisplayTime = 0;
deltaTime = 1000;
}
void draw() {
background(0);
for(int i=0; i<nRows-1; i++){
for(int j=0; j<nCols; j++){
//print("i=",i," j=",j,"\n");
pieces[i][j].update();
pieces[i][j].display();
}
}
}
Tile[][] getPieces(){
int lastRow=0, lastCol=0;
int flag = 0;
int i=0,j=0;
pieces = new Tile[nRows][nCols];
img = loadImage("/home/dmc/Desktop/SMCV/res/models/anfi.png");
for(i=0; i<nRows; i++){
currentPixelRow = currentPixelRow-pieceH;
currentPixelCol = 0;
if(flag == 1){
flag = 0;
j=0;
}
while(j<nCols && flag==0){
lastCol = currentPixelCol+pieceW;
if(lastCol >= w){
lastCol = w;
flag = 1;
}
if(currentPixelRow <= 0){
currentPixelRow = 0;
flag = 2;
}
print(" i=", i);
print(" j=", j);
print(" currentPixelCol=", currentPixelCol);
print(" currentPixelRow=", currentPixelRow);
print("\n");
pieces[i][j] = new Tile(currentPixelCol, 0, currentPixelCol, currentPixelRow, 2.5, pieceW, pieceH);
currentPixelCol = lastCol;
j++;
}
print("\n");
}
return pieces;
}