| 1 | ///////////////////////////////////////////////////////////////////// |
|---|
| 2 | class LocationUtils { |
|---|
| 3 | Vector locations ; |
|---|
| 4 | |
|---|
| 5 | LocationUtils() { |
|---|
| 6 | locations = new Vector() ; |
|---|
| 7 | build() ; |
|---|
| 8 | repart() ; |
|---|
| 9 | } |
|---|
| 10 | //////////////////// Filling locations |
|---|
| 11 | void build() { |
|---|
| 12 | Client cli ; |
|---|
| 13 | for(int k=0;k<nClients;k++) { |
|---|
| 14 | cli = getClient(k) ; |
|---|
| 15 | // init searching |
|---|
| 16 | int idLoc = 0 ; |
|---|
| 17 | boolean alreadyDone = false ; |
|---|
| 18 | for(int u=0;u<locations.size();u++) { |
|---|
| 19 | Location loc = (Location)locations.elementAt(u) ; |
|---|
| 20 | if(cli.location.equals(loc.name)) { // same loc found |
|---|
| 21 | alreadyDone=true ; |
|---|
| 22 | idLoc = u ; |
|---|
| 23 | } |
|---|
| 24 | } |
|---|
| 25 | if(!alreadyDone) { // adding new Location |
|---|
| 26 | Location newLoc = new Location(cli.location,cli) ; |
|---|
| 27 | locations.addElement(newLoc) ; |
|---|
| 28 | } |
|---|
| 29 | else { // adding in existing location |
|---|
| 30 | Location exLoc = (Location)locations.elementAt(idLoc) ; |
|---|
| 31 | exLoc.addClient(cli) ; |
|---|
| 32 | } |
|---|
| 33 | } |
|---|
| 34 | print("THERE IS : "+locations.size()+" locations\n") ; |
|---|
| 35 | } |
|---|
| 36 | //////////////////// Positioning |
|---|
| 37 | void repart() { |
|---|
| 38 | int locDone = 0 ; |
|---|
| 39 | while(locDone<locations.size()) { // tant quon a pas tout fait |
|---|
| 40 | int maxN=0 ; |
|---|
| 41 | int idLoc = 0 ; |
|---|
| 42 | /////// on recupere la plus grosse |
|---|
| 43 | for(int u=0;u<locations.size();u++) { |
|---|
| 44 | Location loc = (Location)locations.elementAt(u) ; |
|---|
| 45 | if(loc.px==-1 && loc.n>maxN) { // (location not done) |
|---|
| 46 | maxN=loc.n ; |
|---|
| 47 | idLoc=u ; |
|---|
| 48 | //print("Location max found: "+idLoc+"\n") ; |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | ////// la plus grosse : |
|---|
| 53 | Location locToPlace = (Location)locations.elementAt(idLoc) ; |
|---|
| 54 | //print("Location to place: "+locToPlace.name+"\n") ; |
|---|
| 55 | |
|---|
| 56 | ////// on essaie de placer au pif, en checkant si ca passe |
|---|
| 57 | boolean ok=false ; // true si espace dispo |
|---|
| 58 | while(!ok) { |
|---|
| 59 | float borgMarg = 70 ; |
|---|
| 60 | float m = borgMarg+locToPlace.r ; |
|---|
| 61 | float nx = random(m,width-m) ; |
|---|
| 62 | float ny = random(m,height-m) ; |
|---|
| 63 | ok=true; |
|---|
| 64 | for(int u=0;u<locations.size();u++) { // on regarde les locs deja placees |
|---|
| 65 | Location loc = (Location)locations.elementAt(u) ; |
|---|
| 66 | if(loc.px!=-1 && loc.isInside(nx,ny,locToPlace.r)) { // si jamais trop pres d'une loc (deja faite) on est pas ok |
|---|
| 67 | ok=false; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | if(ok) { // means we found space |
|---|
| 71 | locToPlace.px=nx ; |
|---|
| 72 | locToPlace.py=ny ; |
|---|
| 73 | locDone++ ; |
|---|
| 74 | //print("Location placed: "+locToPlace.name+"\n") ; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | //print("Location repart WHILE done: "+locDone+"\n") ; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | /////////////////////////////// |
|---|
| 81 | void repartClients() { |
|---|
| 82 | Location loc ; |
|---|
| 83 | for(int u=0;u<locations.size();u++) { |
|---|
| 84 | loc = (Location)locations.elementAt(u) ; |
|---|
| 85 | //print("Reparting Loc : "+loc.name+" "+loc.r+"\n") ; |
|---|
| 86 | loc.repartClients() ; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | void trace() { |
|---|
| 90 | Location loc ; |
|---|
| 91 | for(int u=0;u<locations.size();u++) { |
|---|
| 92 | loc = (Location)locations.elementAt(u) ; |
|---|
| 93 | loc.trace() ; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | //////////////////////////////////////////////// |
|---|
| 98 | int LOCRMIN=27 ; |
|---|
| 99 | int LOCRSTEP=3 ; |
|---|
| 100 | class Location { |
|---|
| 101 | Vector clients ; |
|---|
| 102 | String name ; |
|---|
| 103 | // Position |
|---|
| 104 | float px = -1 ; |
|---|
| 105 | float py = -1 ; |
|---|
| 106 | // Size |
|---|
| 107 | float r ; |
|---|
| 108 | int n ; |
|---|
| 109 | Location(String nam, Client cli) { |
|---|
| 110 | name=nam ; |
|---|
| 111 | clients=new Vector() ; |
|---|
| 112 | n=1 ; |
|---|
| 113 | r=LOCRMIN ; |
|---|
| 114 | clients.addElement(cli) ; |
|---|
| 115 | //print("Creating Loc : "+name+"\n") ; |
|---|
| 116 | } |
|---|
| 117 | void addClient(Client cli) { |
|---|
| 118 | n++ ; |
|---|
| 119 | r+=LOCRSTEP ; |
|---|
| 120 | clients.addElement(cli) ; |
|---|
| 121 | //print("Adding to Loc : "+cli.login+" "+name+" "+n+"\n") ; |
|---|
| 122 | } |
|---|
| 123 | void repartClients() { |
|---|
| 124 | Client cli ; |
|---|
| 125 | for(int u=0;u<clients.size();u++) { |
|---|
| 126 | cli = (Client)clients.elementAt(u) ; |
|---|
| 127 | float m = 2 ; |
|---|
| 128 | float ang = random(0,2*PI) ; |
|---|
| 129 | float ds = random(2,r-m) ; |
|---|
| 130 | float xx = ds*cos(ang); |
|---|
| 131 | float yy = ds*sin(ang); |
|---|
| 132 | cli.moveTo(px+xx,py+yy) ; |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | boolean isInside(float a, float b, float ir) { |
|---|
| 136 | float dd=dist(a,b,px,py) ; |
|---|
| 137 | float margin=15; // au cas ou pour le texte |
|---|
| 138 | if(dd>r+ir+margin) return false ; |
|---|
| 139 | else return true ; |
|---|
| 140 | } |
|---|
| 141 | void trace() { |
|---|
| 142 | stroke(200) ; |
|---|
| 143 | fill(80,100) ; |
|---|
| 144 | ellipse(px,py,2*r,2*r); |
|---|
| 145 | noStroke() ; |
|---|
| 146 | fill(255) ; |
|---|
| 147 | textAlign(CENTER) ; |
|---|
| 148 | |
|---|
| 149 | String locName="" ; |
|---|
| 150 | String locAddress="" ; |
|---|
| 151 | String[] sp=split(name," - "); |
|---|
| 152 | if(sp.length>1) { |
|---|
| 153 | locName=sp[0]; |
|---|
| 154 | locAddress=sp[1]; |
|---|
| 155 | } else { |
|---|
| 156 | locName=name; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | textFont(font,13); |
|---|
| 160 | text(locName,px,py+r+17); |
|---|
| 161 | textFont(font,10); |
|---|
| 162 | text(locAddress,px,py+r+30); |
|---|
| 163 | textAlign(LEFT) ; |
|---|
| 164 | |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | //////////////////////////////////////////////// |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|