Making a loop in the processing

孤街醉人 提交于 2020-06-28 03:54:10

问题


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?


回答1:


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:




回答2:


Mostly I use array and fetch all line of my text file into array and then access them from index. sample code is here. further if you have problem in drawing of code you can text me.

/*Get Configration File*/
File fileSoucrce = new File (System.getenv("APPDATA")+"\\sapphire\\xmc.txt");
Scanner myReader;

this.console("Configration file exists.");

try {
        String[] fileText = new String[10];
        int i =0;
        myReader = new Scanner(fileSoucrce);
        while (myReader.hasNextLine()) {
        fileText[i++]=myReader.nextLine();
        }
myReader.close();

ConnectString =fileText[0];
ConnectUSER       =fileText[1];
ConnectPassword       =fileText[2];
} catch (FileNotFoundException ex) {
     JOptionPane.showMessageDialog(new JFrame(), "Configration file not found at "+fileSoucrce.getAbsolutePath());
     Logger.getLogger(OracleCon.class.getName()).log(Level.SEVERE, null, ex);
    return;
}

Code is available in This InventoryMainPage.java https://github.com/MuhammadFaisal1521/Java-Application-Inventory-Management-System-with-Oracle-Database/tree/master/src/Inventory



来源:https://stackoverflow.com/questions/62376588/making-a-loop-in-the-processing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!