I have written the following code for simple drawing program and I need to double the size of the partially filled array when it is full of data. Here is my code…
final int START_POINTS = 5;
int[] x = new int[START_POINTS];
int[] y = new int[START_POINTS];
int pointCount = 0;
void setup()
{
//pointCount=addPoints(mouseX,mouseY,x,y,pointCount);
//pointCount=addPoints(34,85,x,y,pointCount);
//pointCount=addPoints(90,123,x,y,pointCount);
//pointCount=addPoints(250,33,x,y,pointCount);
//pointCount=addPoints(500,33,x,y,pointCount);
//printAll(x,pointCount);
size(500, 500);
}
//This function prints the values of the data inside the arrays.
void printAll(int[] data, int size){
for(int i=0; i<size; i++)
print(data[i]+" ");
println();
}
void draw()
{
background(255);
drawLines(x, y, pointCount);
}
//This function adds the integer values to the corresponding arrays and returns the size of the partially filled array.
int addPoints(int x, int y, int[] pointsX, int[] pointsY, int curSize)
{
pointsX[curSize]=x;
pointsY[curSize]=y;
curSize++;
//println(curSize);
return curSize;
}
//This function draws lines from the data given in the previous array to the point in the next array.
void drawLines(int[] x,int[] y, int curSize)
{
stroke(0);
for(int i=1;i<curSize;i++)
{
line(x[i-1],y[i-1],x[i],y[i]);
}
}
//This function doubles the size of old array and all the data in the old array is copied to the new array.And then returns the new array.
int[] doubleArray(int[] oldArry)
{
int[] newArry=new int[oldArry.length*2];
for(int i=0;i<oldArry.length;i++)
{
newArry[i]=oldArry[i];
println(newArry);
}
return newArry;
}
//This function runs when the is mouse button is clicked inside the canvas. It adds the currunt location of the mouse to the global arrays. If the size of the global array is full then size is doubled.
void mousePressed()
{
pointCount=addPoints(mouseX,mouseY,x,y,pointCount);
//println(pointCount);
if(pointCount>START_POINTS)
{
doubleArray(x);
doubleArray(y);
}
}