Using float()/int() in processing.js
in
Processing with Other Languages
•
3 years ago
I'm experimenting with processing.js, hoping to adapt a sketch I am working on, but when I try exporting it I get a (very nicely explained) error message informing me that I cannot use the functions 'float variable = float(number)'. what other means are there to convert values of different types? - my code below shows that mainly I am needing to convert strings to floats so I can read latitude & longitude values from a text file and use them as floats.
Thanks! R.
- float leedsXPos = -1.549072;
- float leedsYPos = 53.787672;
- float leedsXPosMapped;
- float leedsYPosMapped;
- float UKLeftEdge = -10.458984;
- float UKRightEdge = 1.761932;
- float UKTopEdge = 58.671226;
- float UKBottomEdge = 49.965356;
- float movingXPos;
- float movingYPos;
- PImage mapImage;
- String mapImageUrl = "http://www.samhumphrey.co.uk/studentscattermap/map.png";
- String[] databaseRowsArray;
- PFont font;
- ArrayList studentsArrayList = new ArrayList();
- String[] studentNamesArray;
- void setup(){
- size(283, 341);
- mapImage = loadImage(mapImageUrl);
- font = createFont("Helvetica", 10);
- textFont(font);
- frameRate(10);
- smooth();
- mapImage = loadImage("map.png");
- movingXPos = 0;
- movingYPos = 0;
- leedsXPosMapped = map(leedsXPos, UKLeftEdge, UKRightEdge, 0, width);
- leedsYPosMapped = map(leedsYPos, UKTopEdge, UKBottomEdge, 0, height);
- //load all students in from list (& count how many objects to ceate).
- studentNamesArray = loadStrings("namesList.txt");
- int numberOfStudents = studentNamesArray.length;
- println("There are "+numberOfStudents+" student records:");
- println(studentNamesArray);
- println("");
- for(int i=0; i<numberOfStudents; i++){
- File f = new File(dataPath(studentNamesArray[i]+".txt"));
- if (f.exists()) {
- println("Loading entries.."+(i+1));
- databaseRowsArray = loadStrings(f);//load text file.
- println(databaseRowsArray);
- studentsArrayList.add(new StudentObject(databaseRowsArray, width/2, height/2));//creates a student object from their entries. // SET HOME X&Y TO THEIR HOMETIWN...?
- }
- }
- println("Loaded all student entries");
- println("END OF SETUP.");
- }//end of setup.
- void draw(){
- background(0);
- image(mapImage, 0, 0);
- for(int i = 0; i < studentsArrayList.size(); i++){//LOOP THROUGH STUDENT OBJECTS..
- StudentObject currentObject = (StudentObject) studentsArrayList.get(i);
- currentObject.updateXYPos();//draws circles.
- }
- println("mouseX translated = "+map(mouseX, 0, width, UKLeftEdge, UKRightEdge)+", mouseY translated = "+map(mouseY, 0, height, UKTopEdge, UKBottomEdge));
- //println("Y = "+map(mouseY, 0, height, UKTopEdge, UKBottomEdge));
- }//end of draw.
- class StudentObject{//define class.
- //attributes.
- String[] databaseEntries;
- float[] locationXPos;
- float[] locationYPos;
- float[] temporaryX;
- float[] temporaryY;
- //constructor.
- public StudentObject(String[] tempDataBaseEntries, float tempLocationXPos, float tempLocationYPos){
- databaseEntries = tempDataBaseEntries;
- temporaryX = new float[databaseEntries.length];//x array is set to same length as there ae rows in data file.
- temporaryY = new float[databaseEntries.length];//y array is set to same length as there ae rows in data file.
- locationXPos = new float[databaseEntries.length];
- locationYPos = new float[databaseEntries.length];
- for(int i=0; i<databaseEntries.length; i++){
- temporaryX[i] = map(leedsXPos, UKLeftEdge, UKRightEdge, 0, width);
- temporaryY[i] = map(leedsYPos, UKTopEdge, UKBottomEdge, 0, height);
- }
- }//end of constructor.
- void updateXYPos(){
- String allEntryDetails;
- movingXPos = map(leedsXPos, UKLeftEdge, UKRightEdge, 0, width);
- movingYPos = map(leedsYPos, UKTopEdge, UKBottomEdge, 0, height);
- for(int i = 1; i < databaseEntries.length; i++){//first row is just category headings.
- allEntryDetails = databaseEntries[i];//load row.
- //STRING FORMAT - name graduationYear homeTown homeLatitude homeLongitude currentLocation currentLat currentLong status date
- int detailsSpacer1 = allEntryDetails.indexOf(" ");//space between name & gradYear.
- int detailsSpacer2 = allEntryDetails.indexOf(" ", detailsSpacer1+1);//space between gradYear & homeTown.
- int detailsSpacer3 = allEntryDetails.indexOf(" ", detailsSpacer2+1);//space between homeTown & homeX.
- int detailsSpacer4 = allEntryDetails.indexOf(" ", detailsSpacer3+1);//space between homeX & homeY.
- int detailsSpacer5 = allEntryDetails.indexOf(" ", detailsSpacer4+1);//space between homeY & currentLocation.
- int detailsSpacer6 = allEntryDetails.indexOf(" ", detailsSpacer5+1);//space between currentLocation & currentLat.
- int detailsSpacer7 = allEntryDetails.indexOf(" ", detailsSpacer6+1);//space between currentLat & currentLong.
- int detailsSpacer8 = allEntryDetails.indexOf(" ", detailsSpacer7+1);//space between currentLong & status.
- int detailsSpacer9 = allEntryDetails.indexOf(" ", detailsSpacer8+1);//space between status & date.
- String currentLocationString = allEntryDetails.substring(detailsSpacer5+1, detailsSpacer6);//location string.
- String locationXPosString = allEntryDetails.substring(detailsSpacer7+1, detailsSpacer8);//location xPos string.
- String locationYPosString = allEntryDetails.substring(detailsSpacer6+1, detailsSpacer7);//location yPos string.
- locationXPos[i] = float(locationXPosString);
- locationYPos[i] = float(locationYPosString);
- locationXPos[i] = map(locationXPos[i], UKLeftEdge, UKRightEdge, 0, width);
- locationYPos[i] = map(locationYPos[i], UKTopEdge, UKBottomEdge, 0, height);
- String homeTownXPosString = allEntryDetails.substring(detailsSpacer4+1, detailsSpacer5);//homeTown xPos string.
- String homeTownYPosString = allEntryDetails.substring(detailsSpacer3+1, detailsSpacer4);//homeTown yPos string.
- float homeTownX = float(homeTownXPosString);
- float homeTownY = float(homeTownYPosString);
- float homeTownXMapped = map(homeTownX, UKLeftEdge, UKRightEdge, 0, width);
- float homeTownYMapped = map(homeTownY, UKTopEdge, UKBottomEdge, 0, height);
- stroke(0, 0, 150);
- fill(0, 150, 0, 100);
- ellipse(leedsXPosMapped, leedsYPosMapped, 20, 20);//ellipse at leeds.
- fill(0, 0, 200, 100);
- ellipse(homeTownXMapped, homeTownYMapped, 20, 20);//ellipse at hometown.
- line(homeTownXMapped, homeTownYMapped, leedsXPosMapped, leedsYPosMapped);//line from homeTown to Leeds.
- line(leedsXPosMapped, leedsYPosMapped, temporaryX[0], temporaryY[0]);//line from Leeds to first location entry.
- for(int n=0; n<databaseEntries.length-1; n++){//loop through the students entries..
- fill(250-(n*50), 0, 0, 100);
- stroke(150, 0, 0);
- ellipse(temporaryX[n], temporaryY[n], 10, 10);//moving spots from leeds to each location in rows.
- if(n<databaseEntries.length-2){
- line(temporaryX[n], temporaryY[n], temporaryX[n+1], temporaryY[n+1]);
- }
- noFill();
- println("temporaryY = "+temporaryY[n]);
- // if(map(leedsXPos, UKLeftEdge, UKRightEdge, 0, width) > temporaryX[n]){
- // bezier(map(leedsXPos, UKLeftEdge, UKRightEdge, 0, width), map(leedsYPos, UKTopEdge, UKBottomEdge, 0, height), map(leedsXPos, UKLeftEdge, UKRightEdge, 0, width)+20, map(leedsYPos, UKTopEdge, UKBottomEdge, 0, height)+20, map(leedsXPos, UKLeftEdge, UKRightEdge, 0, width)-20, map(leedsYPos, UKTopEdge, UKBottomEdge, 0, height)-20, temporaryX[n], temporaryY[n]);
- // }else{
- // bezier(map(leedsXPos, UKLeftEdge, UKRightEdge, 0, width), map(leedsYPos, UKTopEdge, UKBottomEdge, 0, height), map(leedsXPos, UKLeftEdge, UKRightEdge, 0, width)-20, map(leedsYPos, UKTopEdge, UKBottomEdge, 0, height)+20, map(leedsXPos, UKLeftEdge, UKRightEdge, 0, width)+20, map(leedsYPos, UKTopEdge, UKBottomEdge, 0, height)-20, temporaryX[n], temporaryY[n]);
- // }
- //line(leedsXPosMapped, leedsYPosMapped, temporaryX[n], temporaryY[n]);
- }
- }//end of loop through database rows.
- change();
- }//end of update xp pos.
- void change(){//moves initial spot XY (leeds) to location spots.
- for(int i = 1; i < databaseEntries.length; i++){
- float movingPointX = temporaryX[i-1];
- float locationPointX = locationXPos[i];
- if(temporaryX[i-1] < floor(locationXPos[i])){
- temporaryX[i-1]++;
- }else{
- temporaryX[i-1]--;
- }
- if(temporaryY[i-1] < floor(locationYPos[i])){
- temporaryY[i-1]++;
- }else{
- temporaryY[i-1]--;
- }
- }//end loop.
- //println("change = "+map(movingXPos, UKLeftEdge, UKRightEdge, 0, width)+" "+map(movingYPos, UKTopEdge, UKBottomEdge, 0, height));
- }//end change.
- }//end of class.
1