Professional Documents
Culture Documents
Da 6
Da 6
(Processing)
Data
State data is in X(-200 to 200 px) and Y(-233 to 233 px) which has to be
converted into X(0 to 400 px) and Y(0 to 466 px) format
We will use map function
Code of processing
class Table {
String[][] data;
int rowCount;
int getRowCount() {
return rowCount;
}
//code starts
Table() {
data = new String[30][30];
}
Table(String filename) {
String[] rows = loadStrings(filename);
data = new String[rows.length][];
PImage mapImage;
Table locationTable;
int rowCount;
void setup( ) {
size(400, 467);
mapImage = loadImage("India-state.png");
// Make a data table from a file that contains
// the coordinates of each state.
locationTable = new Table("states_data.tsv");
// The row count will be used a lot, so store it globally.
rowCount = locationTable.getRowCount( );
}
void draw( ) {
background(255);
image(mapImage, 0, 0);
// Drawing attributes for the ellipses.
smooth( );
fill(192, 0, 0);
noStroke( );
// Loop through the rows of the locations file and draw the points.
for (int row = 0; row < rowCount; row++) {
float x = locationTable.getInt(row, 1); // column 1
float xcord =map(x,-200,200,0,400);
float y = locationTable.getInt(row, 2); // column 2
float ycord =map(y,-232,232,0,467);
ycord=467-ycord;
System.out.println(ycord);
ellipse(xcord, ycord, 6, 6);
}
}
Output
Q1 Population interactive map
Data
Code of processing
class Table {
String[][] data;
int rowCount;
int getRowCount() {
return rowCount;
}
//code starts
Table() {
data = new String[30][30];
}
Table(String filename) {
String[] rows = loadStrings(filename);
data = new String[rows.length][];
PImage mapImage;
Table locationTable;
int rowCount;
Table dataTable;
// Global variables set in drawData( ) and read in draw( )
float closestDist;
String closestText;
float closestTextX;
float closestTextY;
void setup( ) {
size(450, 467);
mapImage = loadImage("India-state.png");
// Make a data table from a file that contains
// the coordinates of each state.
locationTable = new Table("states_data.tsv");
dataTable = new Table("states_population.tsv");
// The row count will be used a lot, so store it globally.
rowCount = locationTable.getRowCount( );
}
void draw( ) {
background(255);
image(mapImage, 0, 0);
// Drawing attributes for the ellipses.
closestDist = MAX_FLOAT;
// Loop through the rows of the locations file and draw the points.
for (int row = 0; row < rowCount; row++) {
String abbrev = dataTable.getRowName(row);
float x = locationTable.getInt(row, 1); // column 1
float xcord =map(x,-200,200,0,400);
float y = locationTable.getInt(row, 2); // column 2
float ycord =map(y,-232,232,0,467);
ycord=467-ycord;
System.out.println(ycord);
drawData(xcord, ycord, abbrev);
}
if (closestDist != MAX_FLOAT) {
fill(0);
textAlign(CENTER);
textSize(15);
text(closestText, closestTextX, closestTextY);
}
}
ellipse(x, y, 6, 6);
float d = dist(x, y, mouseX, mouseY);
// Because the following check is done each time a new
// circle is drawn, we end up with the values of the
// circle closest to the mouse.
if ((d < 6 + 2) && (d < closestDist)) {
closestDist = d;
String name = locationTable.getString(abbrev, 0);
closestText = name + " " + value;
closestTextX = x;
closestTextY = y-6-4;
}
}
Output