Finding a normalized index, in a 2D array... to display the contents of the 2D array upon clicking. HALP!
in
Programming Questions
•
1 year ago
I have been working on this program for WEEKS now, and I have gotten nowhere... I just can't seem to figure it out.
So the basic idea is to plot a dataset of car information (kind of like 'objects', each has many attributes like MPG cylinders, etc.). The information is stored in an excel file.
This information is read into a 2D array, and the subsequent attributes (like MPG) are read into their own 1D arrays.
There are a good many 'cars' in the data set... the idea is that they are displayed using pixels (etc) in something that looks like a 2D array of scatterplots. (see image below)
-------
WHAT I AM STRUGGLING WITH:
I have to allow the user to select a data point with the RIGHT-MOUSE BUTTON - which I have done successfully. I can get the program to print out the coordinates.
However, the requirement is to print out the actual value stored in the original 2D array (pretty sure) - to basically output the MPG information for that particular point.
What I don't understand is how to go from a normalized array... back to the 2D array in the beginning.
I would SINCERELY appreciate any help that anybody could offer, as I am at a loss... and have much more programming to do that is being severely neglected.
I will post the code below, however, it's pretty lengthy...
thanks for your time!
PFont f;
Cell[][] grid;
int cols = 10;
int rows = 10;
String[][] carData = new String[407][10]; //2D array to store the dataset.
float[] MPG = new float[407]; //1D array for MPG.
float[] Cylinder = new float[407]; //1D array for cylinder.
float[] Horsepower = new float[407]; //1D array for Horsepower.
float[] Weight = new float[407]; //1D array for Weight.
float[] Acceleration = new float[407]; //1D array for Acceleration.
float[] carYear = new float[407]; //1D array for carYear.
float[] Origin = new float[407]; //1D array for Origin. (please convert to int then stick in it, and change String[] to int[].)
//Normalized values to plot.
float[] MPG_norm = new float[407]; //1D array for MPG.
float[] Cylinder_norm = new float[407]; //1D array for cylinder.
float[] Horsepower_norm = new float[407]; //1D array for Horsepower.
float[] Weight_norm = new float[407]; //1D array for Weight.
float[] Acceleration_norm = new float[407]; //1D array for Acceleration.
float[] carYear_norm = new float[407]; //1D array for carYear.
float[] Origin_norm = new float[407]; //1D array for Origin.
//Min and Max values for each of the attributes.
float minMPG = 0;
float maxMPG = 0;
float minCylinder = 0;
float maxCylinder = 0;
float minHorsepower = 0;
float maxHorsepower = 0;
float minWeight = 0;
float maxWeight = 0;
float minAcceleration = 0;
float maxAcceleration = 0;
float mincarYear = 0;
float maxcarYear = 0;
float minOrigin = 0;
float maxOrigin = 0;
void setup()
{
size(900, 900);
f = loadFont("TimesNewRomanPSMT-16.vlw");
grid = new Cell[cols][rows];
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
grid[i][j] = new Cell(i*20, j*20, 100, 80);
}
}
importData();
}
void importData()
{
String[] data = loadStrings("a1-cars.csv");
String[] temp = new String[500];
int index = 0;
//Will load the data files into the 2D array.
for (int i = 1; i < data.length; i++)
{
temp = split(data[i],",");
for (int j = 0; j < 10; j++)
{
carData[i][j] = temp[j];
//print(carData[i][j] + " "); //Just to see if it works.
}
//System.out.println(); //Just to make the formatting look nice.
}
//Will loop through the 2D array and then store the values of each individual attribute into it's respective array.
//The temporary variable origin will store a String, then the string would be converted into an int, to be able to store into
//the origin array (which is a int).
String originTemp = "";
for (int i = 1; i < data.length; i++)
{
MPG[i] = float(carData[i][2]); //Read the values from 2D array, then convert the input to an int.
MPG[i-1] = MPG[i]; //Shift all the values to index 0.
//print(MPG[i] + " "); //Just to see if it works.
Cylinder[i] = float(carData[i][3]); //Read the values from 2D array, then convert the input to an int.
Cylinder[i-1] = Cylinder[i]; //Shift all the values to index 0.
//print(Cylinder[i-1] + " ");
Horsepower[i] = float(carData[i][5]); //Read the values from 2D array, then convert the input to an int.
Horsepower[i-1] = Horsepower[i]; //Shift all the values to index 0.
//print(Horsepower[i] + " ");
Weight[i] = float(carData[i][6]); //Read the values from 2D array, then convert the input to an int.
Weight[i-1] = Weight[i]; //Shift all the values to index 0.
//print(Weight[i] + " ");
Acceleration[i] = float(carData[i][7]); //Read the values from 2D array, then convert the input to an int.
Acceleration[i-1] = Acceleration[i];
//print(Acceleration[i] + " ");
carYear[i] = float(carData[i][8]); //Read the values from 2D array, then convert the input to an int.
carYear[i-1] = carYear[i];
//print(carYear[i] + " ");
//After storing a String element into a temp String, each integer will represent an origin. 1 = American, 2 = European, 3 = Japanese.
originTemp = carData[i][9];
if (originTemp.equals("American"))
{
Origin[i] = 1; //Read the values from 2D array, then convert the input to an int.
//print(Origin[i] + " ");
}
else if (originTemp.equals("European"))
{
Origin[i] = 2; //Read the values from 2D array, then convert the input to an int.
//print(Origin[i] + " ");
}
else if (originTemp.equals("Japanese"))
{
Origin[i] = 3; //Read the values from 2D array, then convert the input to an int.
//print(Origin[i] + " ");
}
Origin[i-1] = Origin[i];
originTemp = ""; //Reset the temp variable origin.
//System.out.println(); //Just to make the formatting look nice.
}
//Find the min and max values for each attribute.
minMPG = min(MPG);
//print("min MPG: " + minMPG + " "); //Is it 9?
maxMPG = max(MPG);
//print("max MPG: " + maxMPG + " ");
minCylinder = min(Cylinder);
//print("min Cyl: " + minCylinder + " "); //Should be 3. But min() keeps finding 0.
maxCylinder = max(Cylinder);
//print("max Cyl:" + maxCylinder + " ");
minHorsepower = min(Horsepower);
//print("min HP: " + minHorsepower + " "); //Is it 46?
maxHorsepower = max(Horsepower);
//print("max HP: " + maxHorsepower + " ");
minWeight = min(Weight);
//print("min Wt: " + minWeight + " "); //Should be 1613. min() keeps returning 0.
maxWeight = max(Weight);
//print("max wt: " + maxWeight + " ");
minAcceleration = min(Acceleration);
//print("min Acc: " + minAcceleration + " "); //Should be 8.
maxAcceleration = max(Acceleration);
//print("max Acc: " + maxAcceleration + " ");
mincarYear = min(carYear);
//print("min Yr: " + mincarYear + " "); //Should be 70.
maxcarYear = max(carYear);
//print("max Yr: " + maxcarYear + " ");
minOrigin = min(Origin);
//print("min origin: " + minOrigin + " "); //Should be 1. There is no 0 in origin name, plus 0 wouldn't be meaningful..
maxOrigin = max(Origin);
//print("max origin: " + maxOrigin + " ");
//Calculating the points, storing them in a normalized array.
for (int i = 0; i < 407; i++)
{
MPG_norm[i] = ((MPG[i] - minMPG)/(maxMPG - minMPG));
//print(MPG_norm[i] + " ");
Cylinder_norm[i] = ((Cylinder[i] - minCylinder)/(maxCylinder - minCylinder));
//print(Cylinder_norm[i] + " ");
Horsepower_norm[i] = ((Horsepower[i] - minHorsepower)/(maxHorsepower - minHorsepower));
//print(Horsepower_norm[i] + " ");
Weight_norm[i] = ((Weight[i] - minWeight)/(maxWeight - minWeight));
//print(Weight_norm[i] + " ");
Acceleration_norm[i] = ((Acceleration[i] - minAcceleration)/(maxAcceleration - minAcceleration));
//print(Acceleration_norm[i] + " ");
carYear_norm[i] = ((carYear[i] - mincarYear)/(maxcarYear - mincarYear));
//print(carYear_norm[i] + " ");
Origin_norm[i] = ((Origin[i] - minOrigin)/(maxOrigin - minOrigin));
//print(Origin_norm[i] + " ");
}
}
class Cell
{
float x, y;
float w, h;
Cell(float tempX, float tempY, float tempW, float tempH)
{
x = tempX;
y = tempY;
w = tempW;
h = tempH;
}
void display()
{
background(250);
stroke(0);
fill(220);
rect(0,0, 900, 900);
x = y = 0; //Initialize x, y.
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
rect(x+100,y+100,w,h);
x+=100;
}
x = 0;
rect(x+100,y+100,w,h);
y+=80;
}
x = y = 0; //Reinitialize the x, y.
//Drawing and plotting points.
for (int i = 0; i < 407; i++)
{
//Plotting all the MPG row.
x = MPG_norm[i] * (180-80) + 100;
y = -MPG_norm[i] * (180-100) + 180; //Make the y (-) to change the coordinate and ymax to map onto plot.
point(x,y);
x = Cylinder_norm[i] * (280-180) + 200;
y = -MPG_norm[i] * (180-100) + 180;
point(x,y);
x = Horsepower_norm[i] * (380-280) + 300;
y = -MPG_norm[i] * (180-100) + 180;
point(x,y);
x = Weight_norm[i] * (480-380) + 400;
y = -MPG_norm[i] * (180-100) + 180;
point(x,y);
x = Acceleration_norm[i] * (580-480) + 500;
y = -MPG_norm[i] * (180-100) + 180;
point(x,y);
x = carYear_norm[i] * (680-580) + 600;
y = -MPG_norm[i] * (180-100) + 180;
point(x,y);
x = Origin_norm[i] * (780-680) + 700;
y = -MPG_norm[i] * (180-100) + 180;
point(x,y);
//For the Cylinder row.
x = MPG_norm[i] * (280-180) + 100;
y = -Cylinder_norm[i] * (260-180) + 260;
point(x,y);
x = Cylinder_norm[i] * (380-280) + 200;
y = -Cylinder_norm[i] * (260-180) + 260;
point(x,y);
x = Horsepower_norm[i] * (380-280) + 300;
y = -Cylinder_norm[i] * (260-180) + 260;
point(x,y);
x = Weight_norm[i] * (480-380) + 400;
y = -Cylinder_norm[i] * (260-180) + 260;
point(x,y);
x = Acceleration_norm[i] * (580-480) + 500;
y = -Cylinder_norm[i] * (260-180) + 260;
point(x,y);
x = carYear_norm[i] * (680-580) + 600;
y = -Cylinder_norm[i] * (260-180) + 260;
point(x,y);
x = Origin_norm[i] * (780-680) + 700;
y = -Cylinder_norm[i] * (260-180) + 260;
point(x,y);
//For the Horsepower row.
x = MPG_norm[i] * (380-280) + 100;
y = -Horsepower_norm[i] * (340-260) + 340;
point(x,y);
x = Cylinder_norm[i] * (380-280) + 200;
y = -Horsepower_norm[i] * (340-260) + 340;
point(x,y);
x = Horsepower_norm[i] * (380-280) + 300;
y = -Horsepower_norm[i] * (340-260) + 340;
point(x,y);
x = Weight_norm[i] * (380-280) + 400;
y = -Horsepower_norm[i] * (340-260) + 340;
point(x,y);
x = Acceleration_norm[i] * (380-280) + 500;
y = -Horsepower_norm[i] * (340-260) + 340;
point(x,y);
x = carYear_norm[i] * (380-280) + 600;
y = -Horsepower_norm[i] * (340-260) + 340;
point(x,y);
x = Origin_norm[i] * (380-280) + 700;
y = -Horsepower_norm[i] * (340-260) + 340;
point(x,y);
//For the weight row.
x = MPG_norm[i] * (480-380) + 100;
y = -Weight_norm[i] * (420-340) + 420;
point(x,y);
x = Cylinder_norm[i] * (480-380) + 200;
y = -Weight_norm[i] * (420-340) + 420;
point(x,y);
x = Horsepower_norm[i] * (480-380) + 300;
y = -Weight_norm[i] * (420-340) + 420;
point(x,y);
x = Weight_norm[i] * (480-380) + 400;
y = -Weight_norm[i] * (420-340) + 420;
point(x,y);
x = Acceleration_norm[i] * (480-380) + 500;
y = -Weight_norm[i] * (420-340) + 420;
point(x,y);
x = carYear_norm[i] * (480-380) + 600;
y = -Weight_norm[i] * (420-340) + 420;
point(x,y);
x = Origin_norm[i] * (480-380) + 700;
y = -Weight_norm[i] * (420-340) + 420;
point(x,y);
//For the Acceleration row.
x = MPG_norm[i] * (580-480) + 100;
y = -Acceleration_norm[i] * (500-420) + 500;
point(x,y);
x = Cylinder_norm[i] * (580-480) + 200;
y = -Acceleration_norm[i] * (500-420) + 500;
point(x,y);
x = Horsepower_norm[i] * (580-480) + 300;
y = -Acceleration_norm[i] * (500-420) + 500;
point(x,y);
x = Weight_norm[i] * (580-480) + 400;
y = -Acceleration_norm[i] * (500-420) + 500;
point(x,y);
x = Acceleration_norm[i] * (580-480) + 500;
y = -Acceleration_norm[i] * (500-420) + 500;
point(x,y);
x = carYear_norm[i] * (580-480) + 600;
y = -Acceleration_norm[i] * (500-420) + 500;
point(x,y);
x = Origin_norm[i] * (580-480) + 700;
y = -Acceleration_norm[i] * (500-420) + 500;
point(x,y);
//For the car year row.
x = MPG_norm[i] * (680-580) + 100;
y = -carYear_norm[i] * (580-500) + 580;
point(x,y);
x = Cylinder_norm[i] * (680-580) + 200;
y = -carYear_norm[i] * (580-500) + 580;
point(x,y);
x = Horsepower_norm[i] * (680-580) + 300;
y = -carYear_norm[i] * (580-500) + 580;
point(x,y);
x = Weight_norm[i] * (680-580) + 400;
y = -carYear_norm[i] * (580-500) + 580;
point(x,y);
x = Acceleration_norm[i] * (680-580) + 500;
y = -carYear_norm[i] * (580-500) + 580;
point(x,y);
x = carYear_norm[i] * (680-580) + 600;
y = -carYear_norm[i] * (580-500) + 580;
point(x,y);
x = Origin_norm[i] * (680-580) + 700;
y = -carYear_norm[i] * (580-500) + 580;
point(x,y);
//For the origin row.
x = MPG_norm[i] * (780-680) + 100;
y = -Origin_norm[i] * (660-580) + 660;
point(x,y);
x = Cylinder_norm[i] * (780-680) + 200;
y = -Origin_norm[i] * (660-580) + 660;
point(x,y);
x = Horsepower_norm[i] * (780-680) + 300;
y = -Origin_norm[i] * (660-580) + 660;
point(x,y);
x = Weight_norm[i] * (780-680) + 400;
y = -Origin_norm[i] * (660-580) + 660;
point(x,y);
x = Acceleration_norm[i] * (780-680) + 500;
y = -Origin_norm[i] * (660-580) + 660;
point(x,y);
x = carYear_norm[i] * (780-680) + 600;
y = -Origin_norm[i] * (660-580) + 660;
point(x,y);
x = Origin_norm[i] * (780-680) + 700;
y = Origin_norm[i] * (660-580) + 580;
point(x,y);
}
fill(0);
text("MPG",100,99);
text("Cylinders",200,99);
text("Horsepower",300,99);
text("Weight",400,99);
text("Acceleration",500,99);
text("Year",600,99);
text("Origin",700,99);
//X-coordinate labels.
text("MPG",2,115);
text("Cylinders",2,195);
text("Horsepower",2,275);
text("Weight",2,355);
text("Acceleration",2,435);
text("Year",2,515);
text("Origin",2,595);
//Y-coordinate labels.
}
}
void mousePressed()
{
/*If the user left clicks a particular point in a scatterplot, it will create
a subrectangle inside a individual scatterplot. e.g. Clicking in the MPG vs
Cylinder scatterplot, will make a point and then clicking another point will create
a rectange based on the two points. */
if (mouseButton == LEFT)
{
println("Testing Left click mouse."); //Test code.
}
/*If the user right clicks the particular point in the scatterplot, it will determine
if the point selected was a data point. If it is, then it will return the actual content of the
data point and print it on the console screen.
*/
else if (mouseButton == RIGHT)
{
float Mx, My = 0;
//Variables to hold the mouse position, and index from array.
int temp_i = 0;
Mx = mouseX;
My = mouseY;
//println("Testing Right click mouse."); //Test code.
//All the x-axis of MPG row.
if ((Mx > 100 && My > 100) && (Mx < 200 && My < 180))
{
println("Position: (" + mouseX + ", " + mouseY + ") clicked.");
//Convert the points into normalized arrays
Mx = ((Mx-100)/(200-100));
My = ((180-My)/(180-100));
println(Mx + " " + My);
//Search through the normalized array and look for index that contains the value set.
for (int i = 0; i < 407; i++)
{
if (Mx == MPG_norm[i] || My == MPG_norm[i])
{
temp_i = i;
println(temp_i);
}
}
//After finding the index from the normalized array of the value set, then
//search through the regular array until the indexed value is found, then print out
//the valued stored there.
for (int i = 0; i <= temp_i; i++)
{
println(MPG[temp_i]);
}
}
else if ((Mx > 200 && My > 100) && (Mx < 300 && My < 180))
{
println("Position: (" + mouseX + ", " + mouseY + ") clicked.");
}
else if ((Mx > 300 && My > 100) && (Mx < 400 && My < 180))
{
println("Position: (" + mouseX + ", " + mouseY + ") clicked.");
}
else if ((Mx > 400 && My > 100) && (Mx < 500 && My < 180))
{
println("Position: (" + mouseX + ", " + mouseY + ") clicked.");
}
else if ((Mx > 500 && My > 100) && (Mx < 600 && My < 180))
{
println("Position: (" + mouseX + ", " + mouseY + ") clicked.");
}
else if ((Mx > 600 && My > 100) && (Mx < 700 && My < 180))
{
println("Position: (" + mouseX + ", " + mouseY + ") clicked.");
}
else if ((Mx > 700 && My > 100) && (Mx < 800 && My < 180))
{
println("Position: (" + mouseX + ", " + mouseY + ") clicked.");
}
}
else
{
print("No button is pressed.");
}
}
void draw()
{
background(255);
textFont(f,16);
fill(0);
text("Hello Strings!",10,100);
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
grid[i][j].display();
}
}
}
1