Create PShape objects using names from an array?
in
Programming Questions
•
2 years ago
I'm completely new to processing, and have set myself a project to help me learn. I'm making an interactive map of the counties of England.
I've opted for using an SVG with each county as a shape within the SVG. I have all the names of the counties, along with some data, stored in a 2D array. The names in the array correspond to the names of the county shapes in the SVG.
I would like to create all the county shapes using a loop, and then I can change their colours etc. to represent data. I've tried to do this by creating a PShape array and making each shape in the SVG an object using a loop with getChild(), but it doesn't work...
- PShape UKmap;
- PShape[] counties = new PShape[49];
- int x = 40;
- String lines[];
- String [][] csv;
- int csvWidth=0;
- int mapX = width/2+250;
- int mapY = height/2+50;
- int mapSize = 600;
- void setup() {
- size(1280, 800);
- UKmap = loadShape("UK_counties.svg");
- stroke(0);
- fill(0, 150, 0);
- smooth();
- shapeMode(CENTER);
- //for importing csv files into a 2d array
- //by che-wei wang
- lines = loadStrings("UK counties.csv");
- //calculate max width of csv file
- for (int i=0; i < lines.length; i++) {
- String [] chars=split(lines[i], ',');
- if (chars.length>csvWidth) {
- csvWidth=chars.length;
- }
- }
- //create csv array based on # of rows and columns in csv file
- csv = new String [lines.length][csvWidth];
- //parse values into 2d array
- for (int i=0; i < lines.length; i++) {
- String [] temp = new String [lines.length];
- temp= split(lines[i], ',');
- for (int j=0; j < temp.length; j++) {
- csv[i][j]=temp[j];
- }
- }
- //draw the country map
- shape(UKmap, mapX, mapY, mapSize, mapSize);
- //create each county
- for (int i=1; i < lines.length+1; i++) {
- // counties[i] = new PShape();
- counties[i] = UKmap.getChild(csv[i][0]);
- //colour each county according to pop. density
- fill(map(int(csv[i][2]), 0, 9000, 255, 0), 0, 0);
- //draw the counties
- shape(counties[i], mapX, mapY, mapSize, mapSize);
- }
- }
- void draw() {
- }
1