Find max of array input from text file?
in
Programming Questions
•
1 years ago
Hi I'm very new to processing and for a project in college I need to write a program to plot 'x' and 'y' values inputted from a text file. The values are located in two columns in the text file separated by a tab.This I have achieved but I would like if the plot would automatically adjust its size depending on the max value in the array but i'm having trouble finding the max value in the array. Tried setting up a 2D array and using the max function but did not work. Any help would be greatly appreciated. My code is a s follows.
- int i = 0;
- int a = 0;
- String[] lines;
- int index = 0;
- void setup()
- {
- size(1000, 600);
- //sets background white
- background(255, 255, 255);
- frameRate(150);
- lines = loadStrings("sinewave.txt");
- // writes text to display
- fill(0, 0, 0);
- textSize(14);
- String s = "TIME / (seconds)";
- text(s, 500, 510, 150, 50);
- String s_1 = "VOLTAGE / (volts)";
- text(s_1, 15, 250, 100, 50);
- }
- void draw()
- {
- if (index+1 < lines.length)
- {
- String[] pieces = split(lines[index], '\t');
- String[] pieces_1 = split(lines[index+1], '\t');
- if (pieces.length == 2)
- {
- int x = int(pieces[0]);
- int y = int(pieces[1]);
- int a = int(pieces_1[0]);
- int b = int(pieces_1[1]);
- stroke(255, 0, 0);
- line(x+120, 500-y, a+120, 500-b);
- stroke(0, 0, 255);
- ellipse(x+120, 500-y, 5, 5);
- stroke(0, 0, 0);
- String[][] s = (lines);
- int q = max(s);
- strokeWeight(1);
- beginShape(POINTS);
- // Horizontal gridlines dark
- for (i = 120; i<x+140; i+=2)
- for ( a = 80; a<500; a+=40)
- {
- strokeWeight(2);
- vertex(i, 20+a);
- strokeWeight(1);
- }
- // Vertical gridlines dark
- for (i = 100; i<500; i+=2)
- for ( a = 100; a<520-y; a+=40)
- {
- strokeWeight(2);
- vertex(20+a, i);
- strokeWeight(1);
- }
- stroke(204, 204, 204);
- // Horizontal gridlines
- for (i = 120; i<x+140; i+=2)
- for (a = 80; a<480; a+=20)
- {
- vertex(i, 20+a);
- }
- // Vertical gridlines
- for (i = 100; i<500; i+=2)
- for ( a = 120; a<500-y; a+=20)
- {
- vertex(20+a, i);
- }
- stroke(0, 0, 0);
- endShape();
- //sets thickness of lines
- strokeWeight(5);
- line(120, 100, 120, 500);
- line(120, 500, 520-y, 500);
- strokeWeight(1);
- }
- // Go to the next line for the next run through draw()
- index = index + 1;
- }
- }
1