begin process at 2012 02 08 09:52:30
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Divers

 > CRÉATION DE LABYRINTHE POUR POVRAY

CRÉATION DE LABYRINTHE POUR POVRAY


 Description

Cliquez pour voir la capture en taille normale
ce code crée un fichier *.inc représentant un labyrinthe aléatoire généré en créant un ensemble de Tarjan. Ne vous sera utile que si vous avez déjà PovRay. On spécifie en dur dans le code la hauteur et la largeur.

Source

  • /* création de fichier include pour POV-Ray de défnition de labyrinthe
  • par la méthode des ensembles de Tarjan*/
  • #include <stdio.h>
  • typedef enum{false, true}bool;
  • #define largeur 13
  • #define hauteur 14
  • bool murs[largeur+1][hauteur+1][2]; // 0: horizontal, 1: vertical
  • bool done[largeur+1][hauteur+1][2];
  • // > 0:horizontal, 1:vertical
  • bool possible(int departx, int departy, int arriveex, int arriveey, int code)
  • {
  • if ((departx==arriveex) && (departy==arriveey))
  • return true;
  • else
  • {
  • // en haut
  • if (code!= 2)
  • {
  • if ((murs[departx][departy][0]==0) && (departy-1>=0))
  • if (possible(departx, departy-1, arriveex, arriveey, 1)) return true;
  • }
  • // en bas
  • if (code!=1)
  • {
  • if ((murs[departx][departy+1][0]==0) && (departy+1<hauteur))
  • if (possible(departx, departy+1, arriveex, arriveey, 2)) return true;
  • }
  • // à droite
  • if (code!=4)
  • {
  • if ((murs[departx+1][departy][1]==0) && (departx+1<largeur))
  • if (possible(departx+1, departy, arriveex, arriveey, 3)) return true;
  • }
  • // à gauche
  • if (code!=3)
  • {
  • if ((murs[departx][departy][1]==0) && (departx-1>=0))
  • if (possible(departx-1, departy, arriveex, arriveey, 4)) return true;
  • }
  • return false;
  • }
  • }
  • main()
  • {
  • int i,j,k;
  • // initialisation du tableau à 1, on met des murs partout
  • for (i=0; i<largeur+1; i++)
  • for (j=0; j<hauteur+1; j++)
  • for (k=0; k<2; k++)
  • { murs[i][j][k]=1;
  • done[i][j][k]=0;
  • }
  • srand(time(NULL));
  • unsigned long a_faire=(largeur-1)*hauteur+(hauteur-1)*largeur;
  • bool choix;
  • unsigned long nb_done=0;
  • while (nb_done!=a_faire)
  • {
  • //on choisit un mur vertical ou horizontal
  • // i : abscisse
  • // j : ordoonée
  • do
  • {
  • choix=rand()%2;
  • if (choix==0)
  • {
  • j=1+rand()%(hauteur-1);
  • i=rand()%largeur;
  • }
  • else
  • {
  • j=rand()%hauteur;
  • i=1+rand()%(largeur-1);
  • }
  • }
  • while (done[i][j][choix]==1);
  • // on teste le mur i,j,choix
  • if (choix==0)
  • {
  • if (!possible(i,j,i,j-1,0))
  • murs[i][j][0]=0;
  • done[i][j][0]=1;
  • }
  • if (choix==1)
  • {
  • if (!possible(i,j,i-1,j,0))
  • murs[i][j][1]=0;
  • done[i][j][1]=1;
  • }
  • nb_done++;
  • } // while pour tout le tableau
  • FILE *fichier=fopen("labyrinthe.inc", "w");
  • fprintf(fichier, "union{\n");
  • for (i=0; i<largeur; i++)
  • for (j=0; j<hauteur+1; j++)
  • {
  • if (((i==0) && (j==0)) || ((i==largeur-1) && (j==hauteur))) continue;
  • if (murs[i][j][0])
  • fprintf(fichier, "box{<%d,0,%2.1f>, <%d,1,%2.1f>}\n",i,j-.1, i+1,j+.1);
  • }
  • for (i=0; i<largeur+1; i++)
  • for (j=0; j<hauteur; j++)
  • {
  • if (murs[i][j][1])
  • fprintf(fichier, "box{<%2.1f,0,%d>, <%2.1f,1,%d>}\n",i-.1,j,i+.1,j+1);
  • }
  • fprintf(fichier, "pigment{Red}\n");
  • fprintf(fichier,"}\n");
  • fclose(fichier);
  • }
/* création de fichier include pour POV-Ray de défnition de labyrinthe 
par la méthode des ensembles de Tarjan*/

#include <stdio.h>

typedef enum{false, true}bool;

#define largeur 13
#define hauteur 14

bool murs[largeur+1][hauteur+1][2];  // 0: horizontal, 1: vertical
bool done[largeur+1][hauteur+1][2];
//                              > 0:horizontal, 1:vertical    


bool possible(int departx, int departy, int arriveex, int arriveey, int code)
{
     if ((departx==arriveex) && (departy==arriveey))
         return true;
     else
     {
          // en haut
          if (code!= 2) 
             {
                 if ((murs[departx][departy][0]==0) && (departy-1>=0))
                    if (possible(departx, departy-1, arriveex, arriveey, 1)) return true;
             }    
          // en bas
          if (code!=1)
             {
                 if ((murs[departx][departy+1][0]==0) && (departy+1<hauteur))
                    if (possible(departx, departy+1, arriveex, arriveey, 2)) return true;
             } 
          // à droite
          if (code!=4)
             {
                 if ((murs[departx+1][departy][1]==0) && (departx+1<largeur))
                    if (possible(departx+1, departy, arriveex, arriveey, 3)) return true;
             } 
          // à gauche
          if (code!=3)
             {
                 if ((murs[departx][departy][1]==0) && (departx-1>=0))
                    if (possible(departx-1, departy, arriveex, arriveey, 4)) return true;
             }     
          return false;
     }       
}    


main()
{
 
  
  int i,j,k;
  // initialisation du tableau à 1, on met des murs partout
  for (i=0; i<largeur+1; i++)
     for (j=0; j<hauteur+1; j++)
        for (k=0; k<2; k++)
             { murs[i][j][k]=1;
               done[i][j][k]=0;
             }    
  
  srand(time(NULL));
  unsigned long a_faire=(largeur-1)*hauteur+(hauteur-1)*largeur;
  bool choix;
  unsigned long nb_done=0;
  while (nb_done!=a_faire)
  {
      //on choisit un mur vertical ou horizontal
      // i : abscisse
      // j : ordoonée    
      do
      {
      choix=rand()%2;
      if (choix==0)
          {
              j=1+rand()%(hauteur-1);
              i=rand()%largeur;
          }       
      else
          {
              j=rand()%hauteur;
              i=1+rand()%(largeur-1);
          }
      }    
      while (done[i][j][choix]==1); 
  

      // on teste le mur i,j,choix
      if (choix==0)
          {
              if (!possible(i,j,i,j-1,0))
                 murs[i][j][0]=0;
              done[i][j][0]=1;
          }
      
      if (choix==1)
          {
              if (!possible(i,j,i-1,j,0))
                  murs[i][j][1]=0;
              done[i][j][1]=1; 
          }    
   nb_done++;
  
  }  // while pour tout le tableau  
   
FILE *fichier=fopen("labyrinthe.inc", "w");
fprintf(fichier, "union{\n");
for (i=0; i<largeur; i++)
  for (j=0; j<hauteur+1; j++)
    {
      if (((i==0) && (j==0)) || ((i==largeur-1) && (j==hauteur))) continue;
      if (murs[i][j][0])
        fprintf(fichier, "box{<%d,0,%2.1f>, <%d,1,%2.1f>}\n",i,j-.1, i+1,j+.1);   
    }    
         
for (i=0; i<largeur+1; i++)
  for (j=0; j<hauteur; j++)         
    {
      if (murs[i][j][1])
        fprintf(fichier, "box{<%2.1f,0,%d>, <%2.1f,1,%d>}\n",i-.1,j,i+.1,j+1); 
    }    
                    
fprintf(fichier, "pigment{Red}\n");
fprintf(fichier,"}\n");
fclose(fichier); 

}    

 Conclusion

pour dessiner le labyrinthe sous PovRay, tapez le code suivant :

#include "colors.inc"

background { color White }  

#declare cam_pos = <6,20,5>;            
            
camera { perspective
         location    cam_pos
         look_at <5,0,5>
         }
        
light_source {cam_pos rgb 2 }
light_source {0 color    rgb 2}
light_source {cam_pos+<0,1,0> rgb 0.4 }


#include "labyrinthe.inc"


 Sources du même auteur

Source avec une capture ÉCRITURE DE LABYRINTHE
Source avec une capture PONG EN OPENGL
Source avec une capture PROBLÈME DES 8 REINES
Source avec une capture RÉSOLUTION DE SYSTÈMES D'ÉQUATIONS LINÉAIRES
DÉCRYPTER VIGENERE AVEC OU SANS LA CLEF

 Sources de la même categorie

Source avec Zip ÉDITEUR DE RECTANGLES EN CONSOLE par seoseo
CONVERSION DE FICHIER EN FICHIER BMP par seoseo
Source avec Zip DETECTEUR EJP par idpro
Source avec Zip Source avec une capture SHOP MANAGER CONSOLE SUR WINDOWS par antho974
Source avec Zip JOUR DE NAISSANCE par fredg19

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,671 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales