-
Notifications
You must be signed in to change notification settings - Fork 39
/
CSVDataSource.java
46 lines (38 loc) · 1.13 KB
/
CSVDataSource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.*;
import processing.core.*;
/**
* CSVDataSource
* Read data from a CSV file.
* Each Layer corresponds to one line, with the first entry in the line the name of the layer.
* Assumes every line is the same length.
* Ignores the parameters given to make.
*
* @author Albert Sun
* @author Lee Byron
* @author Martin Wattenberg
*/
public class CSVDataSource implements DataSource {
public String[] data;
public CSVDataSource(PApplet parent, String filename) {
data = parent.loadStrings(filename);
}
public Layer[] make(int a, int b) {
int numLayers = data.length;
Layer[] layers = new Layer[numLayers];
for (int i = 0; i < numLayers; i ) {
String[] fields = data[i].split(",");
int sizeArrayLength = fields.length - 1;
String name = fields[0];
size = makeDataArray(fields);
layers[i] = new Layer(name, size);
}
return layers;
}
protected float[] makeDataArray(String[] a) {
float[] x = new float[a.length - 1];
for (int i = 1; i < a.length; i ) {
x[i-1] = Float.valueOf(a[i].trim()).floatValue();
}
return x;
}
}