在处理中形成循环 [英] Making a loop in the processing

查看:45
本文介绍了在处理中形成循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作有关处理的动画.然后,我有一个关于循环的问题.通常,我的代码比较长.但是,我制作了一个简单的代码,对初学者也很有用.我的示例代码:

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);

}

我该怎么做?

推荐答案

很可能你需要修复你的 setup 方法来从线中获取点数据,然后修改 draw 方法在循环中使用这些点:

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();
        }
    }
}

结果:

这篇关于在处理中形成循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆