我正在制作动画处理。然后,我对循环有疑问。通常,我的代码更长。但是,我编写了一个简单的代码,对初学者也很有用。因此,我的文本文件名为“ coordinates
”,如下所示:
378 206
366 190
341 168
307 148
我的示例代码:
String[] coordinates; // Creating an array for my text file.
String[] xy; // Creating an array for split the numbers, then I can use x coordinate and y coordinate
int deltaX, deltaY, p;
int initialX, initialY; // Initial point of first coodinate
int finalX, finalY; // Final point of first coodinate
int counter = 0;
void setup()
{
size(500, 500);
strokeWeight(5);
frameRate(2);
coordinates = loadStrings("coordinates.txt");
beginShape(); // It combines the all of vertexes
String[] xy1 = coordinates[0].split(" "); // Split the text file for finding the 378 and 206
int x1 = Integer.parseInt(xy1[0]); // Number of 378
int y1 = Integer.parseInt(xy1[1]); // Number of 206
println(x1);
println(y1);
String[] xy2 = coordinates[1].split(" "); // Split the text file for finding the 366 and 190
int x2 = Integer.parseInt(xy2[0]); // Number of 366
int y2 = Integer.parseInt(xy2[1]); // Number of 190
println(x2);
println(y2);
initialX = x1;
initialY = y1;
finalX = x2;
finalY = y2;
deltaX = abs(finalX - initialX);
deltaY = abs(finalY - initialY);
p = 2 * deltaY - deltaX;
endShape(CLOSE);
}
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)
noLoop();
}
因此,就像您看到的那样,我使用“ Bresenhams算法”绘制线,但是我只能在第一个点的坐标和第二个点的坐标(378 206
和366 190
)之间进行绘制。但是,线应该从366 190
到341 168
。然后,线341 168
到307 148
。通常我的文本文件很长,我想循环绘制这样的线条。我该怎么做?
您很可能需要修复setup
方法以从行中获取点数据,然后修改draw
方法以在循环中使用这些点:
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();
}
}
}
结果:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句