/* Coloriage dans une matrice de int */ public class ColoriageRecursif { static void cercle(int px,int py,int r,int [][] t,int coul) { int dx; int dy; int l; int c; for ( l = 0 ; l < t.length ; l = l+1 ) { for ( c = 0 ; c < t[0].length ; c = c+1 ) { dx = px-c; dy = py-l; if ( dx*dx+dy*dy < r*r ) { t[l][c] = coul; } } } } /* Methode de creation d'une matrice de int */ /* a 0 initialisee avec deux cercles de int */ /* aux valeurs 1 et 2 */ static int [][] initMatrice(int colonnes,int lignes) { int i; int j; int [][] tab = new int[lignes][colonnes]; cercle(20,10,9,tab,1); cercle(40,15,11,tab,2); cercle(30,12,6,tab,3); return tab; } /* Methode d'affichage des valeurs contenues */ /* dans une matrice de int */ static void affichageMatrice(boolean [][] t) { int i; int j; for ( i = 0 ; i < t.length ; i = i+1 ) { for ( j = 0 ; j < t[0].length ; j = j+1 ) { if ( t[i][j] ) { Ecran.afficher(" X"); } else { Ecran.afficher(" O"); } } Ecran.sautDeLigne(); } } /* Methode d'affichage d'un entier */ /* sur l caracteres de large */ static void afficher(int v,int l) { int i; int val; val = v; int nbc = 0; if ( v != 0 ) { while ( val != 0 ) { nbc = nbc+1; val = val/10; } } else { nbc = 1; } if ( v < 0 ) { nbc = nbc+1; } for ( i = 0 ; i < l-nbc ; i = i+1 ) { Ecran.afficher(" "); } Ecran.afficher(v); } /* Methode d'affichage des valeurs contenues */ /* dans une matrice de int */ static void affichageMatrice(int [][] t) { int i; int j; for ( i = 0 ; i < t.length ; i = i+1 ) { for ( j = 0 ; j < t[0].length ; j = j+1 ) { afficher(t[i][j],1); } Ecran.sautDeLigne(); } } ///////////////////////////////////////////////// /* Methode recursive de coloriage d'une zone */ /* de pixels de valeurs identiques */ static void coloriageRecursif(int px,int py,int [][] t, int c,int ct) { if ( ( px >= 0 ) && ( px < t[0].length ) && ( py >= 0 ) && ( py < t.length ) ) { if ( ( t[py][px] != c ) && ( t[py][px] == ct ) ) { t[py][px] = c; coloriageRecursif(px+1,py,t,c,ct); coloriageRecursif(px-1,py,t,c,ct); coloriageRecursif(px,py+1,t,c,ct); coloriageRecursif(px,py-1,t,c,ct); } } } /* Methode de coloriage d'une zone de pixels */ /* de valeurs identiques */ static void coloriage(int px,int py,int [][] t,int c) { coloriageRecursif(px,py,t,c,t[py][px]); } /* Methode recursive de remplissage d'une zone */ /* de pixels delimitee par une valeur */ static void remplissageRecursif(int px,int py,int [][] t, int c,int cl) { if ( ( px >= 0 ) && ( px < t[0].length ) && ( py >= 0 ) && ( py < t.length ) ) { if ( ( t[py][px] != c ) && ( t[py][px] != cl ) ) { t[py][px] = c; remplissageRecursif(px+1,py,t,c,cl); remplissageRecursif(px-1,py,t,c,cl); remplissageRecursif(px,py+1,t,c,cl); remplissageRecursif(px,py-1,t,c,cl); } } } ///////////////////////////////////////////////// /* Menu principal */ static int menuPrincipal() { int v; Ecran.sautDeLigne(); Ecran.afficherln("1: Colorier une tache"); Ecran.afficherln("2: Colorier une zone delimitee"); Ecran.afficherln("0: Quitter"); Ecran.afficher("Que voulez vous faire ? "); v = Clavier.saisirInt(); Ecran.sautDeLigne(); return v; } /* Programme principal */ public static void main(String [] args) { int [][] t = initMatrice(60,20); Ecran.afficherln("Image avant dessin"); affichageMatrice(t); Ecran.sautDeLigne(); int px; int py; int c; int cl; int choix; choix = menuPrincipal(); while ( choix != 0 ) { switch (choix) { case 1 : { Ecran.afficher("Position en x du germe ? "); px = Clavier.saisirInt(); Ecran.afficher("Position en y du germe ? "); py = Clavier.saisirInt(); Ecran.afficher("Couleur de trace ? "); c = Clavier.saisirInt(); coloriage(px,py,t,c); Ecran.sautDeLigne(); Ecran.afficherln("Image après dessin"); affichageMatrice(t); } break; case 2 : { Ecran.afficher("Position en x du germe ? "); px = Clavier.saisirInt(); Ecran.afficher("Position en y du germe ? "); py = Clavier.saisirInt(); Ecran.afficher("Couleur de trace ? "); c = Clavier.saisirInt(); Ecran.afficher("Couleur limite ? "); cl = Clavier.saisirInt(); remplissageRecursif(px,py,t,c,cl); Ecran.sautDeLigne(); Ecran.afficherln("Image après dessin"); affichageMatrice(t); } break; } choix = menuPrincipal(); } } }