I was working on an animation on processing. Then, I have a question about the loop. Normally, my code is more long. However, I made a simple code which can usefull also for the beginners. My sample code:
void setup()
{
size(500, 500);
coordinates = loadStrings("coordinates.txt");
beginShape(); // It combines the all of vertexes
}
void draw()
{
point(initialX, initialY);
println(initialX, initialY, p);
}
How to I make it?
It is very likely that you need to fix your setup
method to get the points data from the line and then modify draw
method to use these points in a loop:
int[][] points;
int curr = 0;
void setup() {
size(500, 500);
strokeWeight(4);
frameRate(5);
coordinates = loadStrings("coordinates.txt");
beginShape(); // It combines the all of vertexes
points = new int[coordinates.length][2];
int row = 0;
for (String line : coordinates) {
String[] pair = line.split(" ");
points[row] = new int[] { Integer.parseInt(pair[0]), Integer.parseInt(pair[1])};
println(points[row][0]); // print x
println(points[row][1]); // print y
row++;
}
fixLineCoords();
endShape(CLOSE);
}
void fixLineCoords() {
int indexStart = curr % points.length;
int indexEnd = (curr + 1) % points.length;
initialX = points[indexStart][0];
initialY = points[indexStart][1];
finalX = points[indexEnd][0];
finalY = points[indexEnd][1];
deltaX = abs(finalX - initialX);
deltaY = abs(finalY - initialY);
p = 2 * deltaY - deltaX;
println("Line between points " + curr + " and " + (curr+1));
counter = 0; // reset counter;
}
void draw() {
point(initialX, initialY);
println(initialX, initialY, p);
if (finalX > initialX )
initialX++;
else
initialX--;
if (p < 0) {
p = p + 2 * deltaY;
} else {
if (initialY > finalY)
initialY--;
else
initialY++;
p = p + 2 * deltaY - 2 * deltaX;
}
counter++;
if (counter > deltaX) {
if (curr == points.length) {
noLoop(); // all points processed
} else {
curr++;
fixLineCoords();
}
}
}
Result: