//------------------------------------------------------ // module : Tp4-IFT2425-2.c // author : // date : // version : 1.0 // language: C++ // note : //------------------------------------------------------ // //------------------------------------------------ // FICHIERS INCLUS ------------------------------- //------------------------------------------------ #include #include #include #include #include /************************************************************************/ /* WINDOWS */ /************************************************************************/ #include Display *display; int screen_num; int depth; Window root; Visual* visual; GC gc; //------------------------------------------------ // DEFINITIONS ----------------------------------- //------------------------------------------------ #define CARRE(X) ((X)*(X)) #define OUTPUT_FILE "Tp4-Img-II" #define VIEW_PGM "xv" #define DEBUG 1 #define TROIS 3 //-Cst-Modele #define X_1 0.0 #define Y_1 1.0 #define X_2 -1.0/sqrt(2.0) #define Y_2 -1.0/2.0 #define X_3 +1.0/2*sqrt(2.0) #define Y_3 -1.0/2.0 #define C 0.25 #define R 0.1 #define D 0.3 //-Cst-Runge-Kutta #define H 0.1 #define T_0 0.0 #define T_F 20.0 #define NB_INTERV (T_F-T_0)/H //-Cst-Image #define WIDTH 128 #define HEIGHT 128 #define MAX_X 4.0 #define MAX_Y 4.0 #define EVOL_GRAPH 3000 #define WHITE 255 #define GREYWHITE 230 #define GREY 200 #define GREYDARK 120 #define BLACK 0 //------------------------------------------------ // GLOBAL CST ------------------------------------ //------------------------------------------------ float Xmin=0.0; float Xmax=0.0; float Ymin=0.0; float Ymax=0.0; float xx_1=((WIDTH/MAX_X)*X_1)+(WIDTH/2); float yy_1=(-(HEIGHT/MAX_Y)*Y_1)+(HEIGHT/2); float xx_2=((WIDTH/MAX_X)*X_2)+(WIDTH/2); float yy_2=(-(HEIGHT/MAX_Y)*Y_2)+(HEIGHT/2); float xx_3=((WIDTH/MAX_X)*X_3)+(WIDTH/2); float yy_3=(-(HEIGHT/MAX_Y)*Y_3)+(HEIGHT/2); float X_1_INI; float X_2_INI; float X_3_INI; float X_4_INI; /************************************************************************/ /* OPEN_DISPLAY() */ /************************************************************************/ int open_display() { if ((display=XOpenDisplay(NULL))==NULL) { printf("Connection impossible\n"); return(-1); } else { screen_num=DefaultScreen(display); visual=DefaultVisual(display,screen_num); depth=DefaultDepth(display,screen_num); root=RootWindow(display,screen_num); return 0; } } /************************************************************************/ /* FABRIQUE_WINDOW() */ /* Cette fonction crée une fenetre X et l'affiche à l'écran. */ /************************************************************************/ Window fabrique_window(char *nom_fen,int x,int y,int width,int height,int zoom) { Window win; XSizeHints size_hints; XWMHints wm_hints; XClassHint class_hints; XTextProperty windowName, iconName; char *name=nom_fen; if(zoom<0) { width/=-zoom; height/=-zoom; } if(zoom>0) { width*=zoom; height*=zoom; } win=XCreateSimpleWindow(display,root,x,y,width,height,1,0,255); size_hints.flags=PPosition|PSize|PMinSize; size_hints.min_width=width; size_hints.min_height=height; XStringListToTextProperty(&name,1,&windowName); XStringListToTextProperty(&name,1,&iconName); wm_hints.initial_state=NormalState; wm_hints.input=True; wm_hints.flags=StateHint|InputHint; class_hints.res_name=nom_fen; class_hints.res_class=nom_fen; XSetWMProperties(display,win,&windowName,&iconName, NULL,0,&size_hints,&wm_hints,&class_hints); gc=XCreateGC(display,win,0,NULL); XSelectInput(display,win,ExposureMask|KeyPressMask|ButtonPressMask| ButtonReleaseMask|ButtonMotionMask|PointerMotionHintMask| StructureNotifyMask); XMapWindow(display,win); return(win); } /****************************************************************************/ /* CREE_XIMAGE() */ /* Crée une XImage à partir d'un tableau de float */ /* L'image peut subir un zoom. */ /****************************************************************************/ XImage* cree_Ximage(float** mat,int z,int length,int width) { int lgth,wdth,lig,col,zoom_col,zoom_lig; float somme; unsigned char pix; unsigned char* dat; XImage* imageX; /*Zoom positiv*/ /*------------*/ if (z>0) { lgth=length*z; wdth=width*z; dat=(unsigned char*)malloc(lgth*(wdth*4)*sizeof(unsigned char)); if (dat==NULL) { printf("Impossible d'allouer de la memoire."); exit(-1); } for(lig=0;lig0) { lgth=length*z; wdth=width*z; dat=(unsigned char*)malloc(lgth*(wdth*4)*sizeof(unsigned char)); if (dat==NULL) { printf("Impossible d'allouer de la memoire."); exit(-1); } for(lig=0;lig au format ppm //---------------------------------------------------------- void SaveImagePpm(char* Name,float*** matrvb,int wdth,int lgth) { int i,j; char buff[200]; FILE* fuser; //extension strcpy(buff,Name); strcat(buff,".ppm"); //ouverture fichier fuser=fopen(buff,"w"); if (fuser==NULL) { printf(" probleme dans la sauvegarde de %s",buff); exit(-1); } //affichage printf("\n Sauvegarde de %s au format %s",buff,".ppm"); fflush(stdout); //sauvegarde de l'entete fprintf(fuser,"P6"); fprintf(fuser,"\n# IMG Module"); fprintf(fuser,"\n%d %d",lgth,wdth); fprintf(fuser,"\n255\n"); //enregistrement for(i=0;i0)&&(y_co>0)) MatPict[y_co][x_co]=BLACK; } } //------------------------------------------------------------------------ // Fill_Pict //------------------------------------------------------------------------ void Fill_Pict(float** MatPts,float** MatPict,int PtsNumber,int NbPts) { int i,j; int x_co,y_co; int k,k_Init,k_End; //Init for(i=0;i0)&&(y_co>0)) MatPict[y_co][x_co]=BLACK; } } //------------------------------------------------ // FONCTIONS TPs---------------------------------- //------------------------------------------------ //---------------------------------------------------------- //---------------------------------------------------------- // MAIN //---------------------------------------------------------- //---------------------------------------------------------- int main (int argc, char **argv) { int i,j,k; int flag_graph; int zoom; XEvent ev; Window win_ppicture; XImage *x_ppicture; char nomfen_ppicture[100]; char BufSystVisu[100]; //>AllocMemory float*** MatPict=dmatrix_allocate_3d(TROIS,HEIGHT,WIDTH); float** MatPts=dmatrix_allocate_2d((int)(NB_INTERV),2); //>Init for(k=0;kQuestion 2 //--------------------------------------------------------------------- //Il faut travailler ici ...et dans > // FONCTIONS TPs //Un exemple ou la matrice de points MatPict est remplie //par une image couleur donné par l'équation d'en bas... et non pas par //les bassins d'attractions //for(k=0;kSave&Visu de MatPict SaveImagePpm((char*)OUTPUT_FILE,MatPict,HEIGHT,WIDTH); //--------------------------- //>Affiche Statistique printf("\n\n Stat: Xmin=[%.2f] Xmax=[%.2f] Ymin=[%.2f] Ymax=[%.2f]\n",Xmin,Xmax,Ymin,Ymax); //-------------------------------------------------------------------------------- //-------------- visu sous XWINDOW ----------------------------------------------- //-------------------------------------------------------------------------------- if (flag_graph) { //>Uuverture Session Graphique if (open_display()<0) printf(" Impossible d'ouvrir une session graphique"); sprintf(nomfen_ppicture,"Évolution du Graphe"); win_ppicture=fabrique_window(nomfen_ppicture,10,10,HEIGHT,WIDTH,zoom); x_ppicture=cree_XimageCoul(MatPict,zoom,HEIGHT,WIDTH); printf("\n\n Pour quitter,appuyer sur la barre d'espace"); fflush(stdout); //boucle d'evenements for(;;) { XNextEvent(display,&ev); switch(ev.type) { case Expose: XPutImage(display,win_ppicture,gc,x_ppicture,0,0,0,0,x_ppicture->width,x_ppicture->height); break; case KeyPress: XDestroyImage(x_ppicture); XFreeGC(display,gc); XCloseDisplay(display); flag_graph=0; break; } if (!flag_graph) break; } } //>Retour printf("\n Fini... \n\n\n"); return 0; }