diff --git a/TP5/README.md b/TP5/README.md index 868ad3e68094b8432d4ed9d6115c2694b27f5f7e..f829a09e04807a3b8b95e9b7468acf8cfc5f70e6 100644 --- a/TP5/README.md +++ b/TP5/README.md @@ -9,7 +9,7 @@ ### Définir les marques -Nous choisisons les attributs "population" et "altitude" pour encoder visuellement les villes. Voici comment nous pourrions associer une variable rétinienne à chaque attribut : +Nous choisisons les attributs "population" et "altiteude" pour encoder visuellement les villes. Voici comment nous pourrions associer une variable rétinienne à chaque attribut : - Population : la variable rétinienne associée à la population sera la taille du marqueur. Ainsi, une ville avec une population plus élevée aurait une marque plus grande qu'une ville avec une population plus faible. diff --git a/TP5/sketch_240321a/City.pde b/TP5/sketch_240321a/City.pde index 1aabf0eb9535fdb4ed33c6e8b8bcd8556a7d33c3..9a8706562bc8df0c04bba787b393d196a97fcc74 100644 --- a/TP5/sketch_240321a/City.pde +++ b/TP5/sketch_240321a/City.pde @@ -8,6 +8,7 @@ class City { float altitude; float radius=0; boolean isHighlighted=false; + boolean isClicked=false; // put a drawing function in here and call from main drawing loop } public City(String postalcode, String name, float x, float y, float population, float surface, float altitude) { @@ -45,7 +46,14 @@ class City { float rectHeight = 20; fill(255); rect(x + radius/2 + 5, y - rectHeight/2, rectWidth, rectHeight, 5); - fill(0); + + if(isClicked){ + colorMode(RGB); + fill(255, 0, 0); + } + else { + fill(0); + } text(name, x + radius/2 + 10, y - 2); setBasePen(); } diff --git a/TP5/sketch_240321a/ColorPalette.pde b/TP5/sketch_240321a/ColorPalette.pde index b66f0dfffbe49767efbbb8a1147c365a14371132..1236e37cc1f3f24fec356636de423e32bf87dde4 100644 --- a/TP5/sketch_240321a/ColorPalette.pde +++ b/TP5/sketch_240321a/ColorPalette.pde @@ -3,6 +3,7 @@ float getColorForAltitude(float altitude) { } void setBasePen() { + colorMode(HSB); fill(0); textSize(12); } diff --git a/TP5/sketch_240321a/sketch_240321a.pde b/TP5/sketch_240321a/sketch_240321a.pde index 806fc4f6a0863336625b9ea3a618bddf5cd3411a..4023ed918c88bdab4b77c17e2dbb9981cb4b1ad8 100644 --- a/TP5/sketch_240321a/sketch_240321a.pde +++ b/TP5/sketch_240321a/sketch_240321a.pde @@ -13,77 +13,81 @@ int y = 2; City cities[]; int legendsHeight=100; -int minPopulationToDisplay=10000; +int minPopulationToDisplay=1; boolean redrawInProgress=false; City lastCitySelected=null; +City lastCityClicked=null; + +float sliderX, sliderY, sliderWidth, sliderHeight; +float sliderPosition; +boolean draggingSlider = false; void setup() { size(900,900); colorMode(HSB, 360, 100, 100); readData(); noLoop(); + + sliderX = 50; + sliderY = 50; + sliderWidth = width - 100; + sliderHeight = 20; + sliderPosition = map(minPopulationToDisplay, minPopulation, maxPopulation, sliderX, sliderX + sliderWidth); } -void draw(){ - background(255); - - // Draw title - fill(0); - textSize(20); - textAlign(CENTER,CENTER); - text("Afficher les populations supérieures à " + minPopulationToDisplay, width/2, 20); - setBasePen(); - - for (int i = 0 ; i < totalCount; ++i) { - // draw a point at the coordinates of the city - cities[i].draw(); - } +void draw() { + if (!redrawInProgress) { // Ne dessinez que si les données ne sont pas en cours de mise à jour + background(255); + fill(0); + textSize(20); + textAlign(CENTER, CENTER); + text("Afficher les populations supérieures à " + minPopulationToDisplay, width/2, 20); + setBasePen(); + + drawSlider(); + + // Vérifiez que l'indice est valide + for (int i = 0; i < Math.min(totalCount, cities.length); ++i) { + cities[i].draw(); + } - updatePixels(); - - // Draw the distribution of the altitude values - float[] altitudeValues = new float[cities.length]; - for (int i = 0; i < totalCount; i++) { - altitudeValues[i] = cities[i].altitude; + updatePixels(); + + if (legendsHeight > 0) { + drawLegend(); + } } +} - if (legendsHeight > 0) { - drawLegend(); - } - redrawInProgress=false; +void drawSlider() { + fill(180); + rect(sliderX, sliderY, sliderWidth, sliderHeight); + + fill(255, 0, 0); + ellipse(sliderPosition, sliderY + sliderHeight / 2, 20, 20); } void readData() { - redrawInProgress=true; + // Début de la mise à jour des données String[] lines = loadStrings("./villes.tsv"); - // check that the file has been loaded - if (lines == null) { - println("Error loading file"); - exit(); - } else { - println("File loaded"); - } - - parseInfo(lines[0]); // read the header line - ArrayList<City> filteredCities = new ArrayList<City>(); - - for (int i = 2 ; i < totalCount ; ++i) { - String[] columns = split(lines[i], TAB); - float pointX = float (columns[x]); - float pointY = float (columns[y]); - // filter the cities - if (float (columns[5]) > minPopulationToDisplay) { - filteredCities.add(new City(columns[0], columns[4], mapX(pointX), mapY(pointY), float (columns[5]), float (columns[6]), float (columns[7]))); + if (lines != null && lines.length > 2) { + parseInfo(lines[0]); // Lire les informations d'en-tête + for (int i = 2; i < lines.length; i++) { // Assurez-vous de ne pas dépasser les bornes des lignes + String[] columns = split(lines[i], TAB); + float pointX = float(columns[x]); + float pointY = float(columns[y]); + if (float(columns[5]) > minPopulationToDisplay) { + filteredCities.add(new City(columns[0], columns[4], mapX(pointX), mapY(pointY), float(columns[5]), float(columns[6]), float(columns[7]))); } + } } - - totalCount = filteredCities.size(); - cities = filteredCities.toArray(new City[0]); - println("City list created: " + totalCount + " cities"); - + totalCount = filteredCities.size(); + cities = filteredCities.toArray(new City[totalCount]); + redrawInProgress = false; // La mise à jour est terminée + redraw(); } void parseInfo(String line) { @@ -166,6 +170,45 @@ void mouseMoved() { } } +void mousePressed(){ + /*City city = pick(mouseX, mouseY); + if(city != null) { + println("city clicked: " + city.name); + } + lastCityClicked = city; + lastCityClicked.isClicked = true; + redraw();*/ + if (dist(mouseX, mouseY, sliderPosition, sliderY + sliderHeight / 2) < 10) { + draggingSlider = true; + updateSliderPosition(); + } +} + +void mouseReleased() { + // Arrête le glissement lorsque le bouton de la souris est relâché + draggingSlider = false; +} + +void mouseDragged() { + if (draggingSlider) { + sliderPosition = constrain(mouseX, sliderX, sliderX + sliderWidth); + int newMinPopulation = int(map(sliderPosition, sliderX, sliderX + sliderWidth, minPopulation, maxPopulation)); + if (newMinPopulation != minPopulationToDisplay) { // Ne mettez à jour les données que si nécessaire + minPopulationToDisplay = newMinPopulation; + redrawInProgress = true; + readData(); // readData devrait désactiver redrawInProgress à la fin + } + } +} + + +void updateSliderPosition() { + sliderPosition = constrain(mouseX, sliderX, sliderX + sliderWidth); + minPopulationToDisplay = int(map(sliderPosition, sliderX, sliderX + sliderWidth, minPopulation, maxPopulation)); + readData(); + redraw(); +} + City pick(int px, int py) { for (int i = totalCount - 1 ; i >= 0; --i) {