From bf2ab336accc2c59fcac5d54a19045e701b59977 Mon Sep 17 00:00:00 2001
From: Lucas Philippe <lucasphilippe@MacBook-Air-de-Lucas.local>
Date: Thu, 4 Apr 2024 11:39:54 +0200
Subject: [PATCH] Ajout etape 4

---
 TP5/sketch_240321a/City.pde           | 12 +++++++++---
 TP5/sketch_240321a/sketch_240321a.pde | 19 ++++++++++++++++++-
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/TP5/sketch_240321a/City.pde b/TP5/sketch_240321a/City.pde
index eaa4767..4af5e34 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 759da7d..6155715 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;
 }
-- 
GitLab