diff --git a/TP5/sketch_240321a/City.pde b/TP5/sketch_240321a/City.pde index eaa47676d6259ee12b8acd7981fafde2f0752ed7..4af5e34ce75dc23d3334d4677ae6a609bdd9e80c 100644 --- a/TP5/sketch_240321a/City.pde +++ b/TP5/sketch_240321a/City.pde @@ -6,6 +6,7 @@ class City { float population; float density; float altitude; + float radius=0; // 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) { @@ -23,14 +24,19 @@ class City { float porcentageAlt = map(this.altitude, minAltitude, maxAltitude, 0, 100); float hue = getColorForAltitude(porcentageAlt); - // Set marker size based on population - float size = map(this.population, minPopulation, maxPopulation, 3, 100); + // Set marker size based on populationxxxx + radius=map(this.population, minPopulation, maxPopulation, 3, 100); // Draw marker fill(hue, 100, 100); - ellipse(x, y, size, size); + ellipse(x, y, radius, radius); } + boolean contains(int px, int py){ + float dist = dist(x, y, px, py); + return dist<radius/2+1; + } + } diff --git a/TP5/sketch_240321a/sketch_240321a.pde b/TP5/sketch_240321a/sketch_240321a.pde index 759da7d25c560410f68b4239ea24bd94fb26220e..6155715d84d131f14d1b3764b392d80447bdf835 100644 --- a/TP5/sketch_240321a/sketch_240321a.pde +++ b/TP5/sketch_240321a/sketch_240321a.pde @@ -15,6 +15,7 @@ City cities[]; int legendsHeight=100; int minPopulationToDisplay=10000; boolean redrawInProgress=false; +City lastCitySelected=null; void setup() { size(900,900); @@ -145,5 +146,21 @@ void keyPressed() { } void mouseMoved() { - println("(x: " + mouseX + ", y: " + mouseY + ")"); + //println("(x: " + mouseX + ", y: " + mouseY + ")"); + City city = pick(mouseX, mouseY); + if (city != null) { + if (lastCitySelected != city) { + lastCitySelected = city; + println(city.name); + } + } +} + +City pick(int px, int py) { + for (int i = totalCount - 1 ; i >= 0; --i) { + if (cities[i].contains(px, py)) { + return cities[i]; + } + } + return null; }