Skip to content

Instantly share code, notes, and snippets.

@emilydelorme
Last active November 18, 2017 10:30
Show Gist options
  • Save emilydelorme/65e142dbe5a7adfcb6ab1f9904b6876c to your computer and use it in GitHub Desktop.
Save emilydelorme/65e142dbe5a7adfcb6ab1f9904b6876c to your computer and use it in GitHub Desktop.
Fonctions affect, union et isPixelOn
// Fonctions réalisé avec Camille Police et Charles Delorme (uniquement)
public void affect(AbstractImage img) {
Iterator<Node> it = this.iterator()
Iterator<Node> it2 = img.iterator();
it.clear(); //On clear this pour ensuite recopier img dans this
affectAux(it, it1); // On appelle une fonction récursive pour recréer l'arbre
}
private void affectAux(Iterator<Node> it, Iterator<Node> it2) {
// Si l'état du noeud est 0, on ajoute un noeud avec un état 0 dans this
if (it.getValue().state == 0) {
it.addValue(new Node(0));
}
// Si l'état du noeud est 1, on ajoute un noeud avec un état 1 dans this
if (it2.getValue().state == 1) {
it2.addvalue(new Node(1));
} else {
// Sinon on ajoute un noeud avec un état 2 dans this
// et on appelle la fonction recursive de chaque côté
// du noeud courant
it.addValue(new Node(2));
it.goLeft;
it2.goLeft();
affectAux(it, it2);
it.goUp();
it2.goUp();
it.goRight();
it2.goRight();
affectAux(it, it2);
it.goUp();
it2.goUp();
}
}
public void union(AbstractImage image1, AbstractImage image1) {
Iterator<Node> it = this.iterator()
Iterator<Node> it1 = image1.iterator();
Iterator<Node> it2 = image2.iterator();
it.clear(); // On clear this pour pouvoir avoir le résultat de l'union de image 1 et image 2 dans une img vide
unionAux(it, it1, it2);
}
// Fonctions pour réduire le nombre d'instructions et faire des mouvements groupé.
// Mais pas utilisée car je ne sais pas si on peut utiliser les "..."
private void groupMove(String move, Iterator<Node> its...) {
switch(move) {
case "L": // GoLeft
Arrays.stream(its).forEach(it -> it.goLeft());
break;
case "R"; // GoRight
Arrays.stream(its).forEach(it -> it.goRight());
break;
case "U": //GoUp
Arrays.stream(its).forEach(it -> it.goUp());
break;
default:
break;
}
}
private void unionAux(Iterator<Node> it, Iterator<Node> it1, Iterator<Node> it2) {
// Si l'état des nodes de img1 et img2 sont identique (state == 1) alors on recréer un node avec un état 1 dans this
if (it1.getValue().state == 1 || it2.getValue().state == 1)
it.addvalue(new Node(1));
// Si l'états des nodes de img1 et img2 sont identique (state == 0) alors on recréer un node avec un état 0 dans this
else if (it1.getvalue().state == 0 && it2.getValue().state == 0)
it.addValue(new Node(0));
// Si l'état des nodes de img1 et img2 sont identiques (state == 2) alors on recréer un node avec un état 2 dans this.
// Puis Appeler la fonction récursive dans chaque nouveau noeud créé
else if (it1.getvalue().state == 2 && it2.getValue().state == 2) {
it.addvalue(new Node(2));
it.goLeft();
it1.goLeft();
it2.goLeft();
unionAux(it, it1, it2);
it.goUp();
it1.goUp();
it2.goUp();
it.goRight();
it1.goRight();
it2.goRight();
unionAux(it, it2, it3);
// Une fois les fonctions récursives appellées on vérifie le résultat pour pouvoir simplifier l'arbre si possible
int x = it.goLeft().getValue().state;
it.goUp();
int y = it.Right().getValue().state;
it.goUp();
// Si les deux états sont identiques et différents de 2, alors on met la valeur d'un enfant dans le noeud courant et
// on supprime les fils
if (x == y && x != 2) {
it.set(y);
it.goLeft().remove();
it.goUp();
it.goRight();
it.remove();
it.goUp();
}
}
// Dans les deux prochaines conditions, on ajoute un noeud à l'état 2 dans this puis
// on appelle la fonction récursive dans chaque enfants.
// Car dans l'une des deux images il y a une nouvelle branche que l'autre image n'as pas
else if(it1.getValue().state == 0 && it2.getValue().state == 2) {
it.addValue(new Node(2));
it.goLeft();
it2.goLeft();
unionAux(it, it1, it2);
it.goUp();
it2.goUp();
it.goRight();
it2.goRight();
unionAux(it, it1, it2);
it.goUp();
it2.goUp();
}
else if(it2.getValue.state == 0 && it1.getValue().state == 2) {
it.addValue(new Node(2));
it.goLeft();
it1.goLeft();
unionAux(it, it1, it2);
it.goUp();
it1.goUp();
it.goRight();
it1.goRight();
unionAux(it, it1, it2);
it.goUp();
it1.goUp();
}
}
public boolean isPixelOn(int x, int y) {
Iterator<Node> it = this.iterator();
int hauteur = 0;
int[] region = {0, 0, 255, 255}; //x0, y0, x1, y1
// C'est comme une recherche dichotomique mais avec 2 coupe différente
while (it.getvalue().state == 2) {
if (hauteur % 2 == 0) { // on coupe horizontalement
if (y < region[1] + region[3] / 2) {
it.goLeft();
region[3] = (region[1] + region[3]) / 2;
} else {
it.goRight();
region[1] = (region[1] + region[3]) / 2 + 1;
}
} else { // on coupe verticalement
if (x < (region[0] + region[2]) / 2) {
it.goLeft();
region[2] = (region[0] + region[2]) / 2;
} else {
it.goRight();
region[0] = (region[0] + region[2]) / 2 + 1;
}
hauteur++;
}
}
return it.getValue().state == 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment