Animation gtk qui représente l'évolution des "cases" d'une matrice

Contenu du snippet

C'est une animation qui , suivant une liste de matrice, va nous le représenter graphiquement. Plus une case est sombre, plus le nombre que contient cette case est grand.
On peut ainsi suivre " visuellement " l'évolution de la matrice.

Source / Exemple :


#include <gtk/gtk.h>
#include <stdio.h>
#include <math.h>
#include <fcntl.h>

#define IMAGE_WIDTH	64
#define IMAGE_HEIGHT	64
#define N 5

struct liste{
  int num;
  struct liste *next;
};

struct matrice{
  int num;
  struct liste *liens;
  int tableau [N][N]; 
};

struct liste_matrices{
  struct matrice *m;
  struct liste_matrices *next;
};

struct liste_liste_matrices{
  struct liste_matrices *l;
  struct liste_liste_matrices *next;
};

/**

  • par default,pr une case on a une longueur et une largeur de 64;
  • il nous suffira de multiplier ces valeurs par le nombre de grains au depart d un tas...
    • /
typedef struct matrice Matrice; typedef struct liste_matrices Liste; guchar rgbbuf[IMAGE_WIDTH * IMAGE_HEIGHT * 3]; GdkPixmap *pixmap; GdkGC *crayon; GdkColor couleur; GdkWindow *mywindow; gboolean on_darea_expose (GtkWidget *widget, GdkEventExpose *event, gpointer user_data); void dessine_carre (gint intensite,gint absc,gint ord) { couleur.red = couleur.green = couleur.blue = intensite; gdk_colormap_alloc_color(gdk_colormap_get_system(),&couleur,FALSE,TRUE); gdk_gc_set_foreground(crayon,&couleur); gdk_draw_rectangle(pixmap,crayon,1,ord,absc,IMAGE_WIDTH,IMAGE_HEIGHT); } void dessine_selon_matrice (Matrice *mat) { /*
  • il faut initialiser la taille de la fentre ->N*IMAGE_HEIGHT
  • il faut determiner le nombre de case a afficher.-> cf taille de la matrice
  • il faut initialiser l intensite max pr l element de valeur la plus grde
  • pour chaque element de la matrice,il faut calculer l intensite.
  • /
gint unite,i,j,val_cur,tmp1,tmp2; unite=65000/N; for(i=0;i < N;i++){ for(j=0;j < N;j++){ tmp1=i+1; tmp2=j+1; val_cur=65000-unite*mat->tableau[i][j]; dessine_carre(val_cur,tmp1*64-64,tmp2*64-64); /************ printf ("yo %d \n",val_cur); printf ("yo %d \n",tmp1*64-64); printf ("yo %d \n",tmp2*64-64);
                            • /
} } } void animation(Liste *lst){ /*
  • il faut dessiner selon la matrice pour chaques matrices de la liste...
  • /
while (lst->next!=NULL) { dessine_selon_matrice (lst->m); gdk_draw_pixmap(mywindow, crayon,pixmap, 0,0,0,0, IMAGE_WIDTH*N, IMAGE_HEIGHT*N); printf("yo %d\n",lst->m->num); lst=lst->next; sleep(1); } dessine_selon_matrice (lst->m); sleep(1); gdk_draw_pixmap(mywindow, crayon,pixmap, 0,0,0,0, IMAGE_WIDTH*N, IMAGE_HEIGHT*N); printf("yoda %d\n",lst->m->num); } int main (int argc, char *argv[]) { GtkWidget *window, *darea; gint intensite,absc,ord,i2; int p,q,z; Matrice *mat,*mat2,*mat3,*mat4,*mat5,*mat6,*mat7,*mat8,*mat9; Liste *lst,*lst2,*lst3,*lst4,*lst5,*lst6,*lst7,*lst8,*lst9; gtk_init (&argc, &argv); gdk_rgb_init(); gdk_colormap_alloc_color(gdk_colormap_get_system(),&couleur,FALSE,TRUE); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); darea = gtk_drawing_area_new(); gtk_drawing_area_size (GTK_DRAWING_AREA (darea), IMAGE_WIDTH*N, IMAGE_HEIGHT*N); gtk_container_add (GTK_CONTAINER (window), darea); gtk_signal_connect (GTK_OBJECT (darea), "expose-event", GTK_SIGNAL_FUNC (on_darea_expose), NULL); gtk_widget_show_all (window); pixmap=gdk_pixmap_new(window->window,IMAGE_WIDTH*N,IMAGE_HEIGHT*N,gdk_window_get_visual(window->window)->depth); crayon=gdk_gc_new(pixmap); gdk_gc_set_line_attributes(crayon,1,GDK_LINE_SOLID,GDK_CAP_BUTT,GDK_JOIN_BEVEL); z=0; mywindow=darea->window; /******* test 1 ***** intensite=0; absc=128; ord=0; dessine_carre(intensite,absc,ord);
                                              • /
/********* test 2 ***** mat = (Matrice*)malloc(sizeof(Matrice)); mat->tableau[0][0]=5; mat->tableau[0][1]=4; mat->tableau[0][2]=3; mat->tableau[0][3]=2; mat->tableau[0][4]=1; for(p=1;p<N;p++){ for(q=0;q<N;q++){ mat->tableau[p][q]=abs(p-q); } } dessine_selon_matrice (mat);
                                              • /
/********* test 3 ***** mat = (Matrice*)malloc(sizeof(Matrice)); mat2 = (Matrice*)malloc(sizeof(Matrice)); mat3 = (Matrice*)malloc(sizeof(Matrice)); mat4 = (Matrice*)malloc(sizeof(Matrice)); mat5 = (Matrice*)malloc(sizeof(Matrice)); mat6 = (Matrice*)malloc(sizeof(Matrice)); mat7 = (Matrice*)malloc(sizeof(Matrice)); mat8 = (Matrice*)malloc(sizeof(Matrice)); mat9 = (Matrice*)malloc(sizeof(Matrice)); lst=(Liste*)malloc(sizeof(Liste)); lst2=(Liste*)malloc(sizeof(Liste)); lst3=(Liste*)malloc(sizeof(Liste)); lst4=(Liste*)malloc(sizeof(Liste)); lst5=(Liste*)malloc(sizeof(Liste)); lst6=(Liste*)malloc(sizeof(Liste)); lst7=(Liste*)malloc(sizeof(Liste)); lst8=(Liste*)malloc(sizeof(Liste)); lst9=(Liste*)malloc(sizeof(Liste)); mat->num=0; mat2->num=2; mat3->num=3; mat4->num=4; mat5->num=5; for(p=0;p<N;p++){ for(q=0;q<N;q++){ mat->tableau[p][q]=0; mat2->tableau[p][q]=0; mat3->tableau[p][q]=0; mat4->tableau[p][q]=0; mat5->tableau[p][q]=0; mat6->tableau[p][q]=0; mat7->tableau[p][q]=0; mat8->tableau[p][q]=0; mat9->tableau[p][q]=0; } } mat->tableau[0][0]=3; mat2->tableau[0][1]=3; mat3->tableau[0][2]=3; mat4->tableau[1][2]=3; mat5->tableau[1][1]=3; mat6->tableau[1][0]=3; mat7->tableau[2][0]=3; mat8->tableau[2][1]=3; mat9->tableau[2][2]=3; lst->m=(Matrice*)malloc(sizeof(Matrice)); lst->next =(Liste*)malloc(sizeof(Liste)); lst->m=mat; lst->next =lst2; lst2->m=(Matrice*)malloc(sizeof(Matrice)); lst2->next = (Liste*)malloc(sizeof(Liste)); lst2->m=mat2; lst2->next=lst3; lst3->m=(Matrice*)malloc(sizeof(Matrice)); lst3->next = (Liste*)malloc(sizeof(Liste)); lst3->m=mat3; lst3->next=lst4; lst4->m=(Matrice*)malloc(sizeof(Matrice)); lst4->next = (Liste*)malloc(sizeof(Liste)); lst4->m=mat4; lst4->next=lst5; lst5->m=(Matrice*)malloc(sizeof(Matrice)); lst5->next = (Liste*)malloc(sizeof(Liste)); lst5->m=mat5; lst5->next=lst6; lst6->m=(Matrice*)malloc(sizeof(Matrice)); lst6->next =(Liste*)malloc(sizeof(Liste)); lst6->m=mat6; lst6->next =lst7; lst7->m=(Matrice*)malloc(sizeof(Matrice)); lst7->next = (Liste*)malloc(sizeof(Liste)); lst7->m=mat7; lst7->next=lst8; lst8->m=(Matrice*)malloc(sizeof(Matrice)); lst8->next = (Liste*)malloc(sizeof(Liste)); lst8->m=mat8; lst8->next=lst9; lst9->m=(Matrice*)malloc(sizeof(Matrice)); lst9->next = (Liste*)malloc(sizeof(Liste)); lst9->m=mat9; lst9->next=lst; animation(lst);
                              • /
/************** test 4 ********** mat = (Matrice*)malloc(sizeof(Matrice)); mat2 = (Matrice*)malloc(sizeof(Matrice)); mat3 = (Matrice*)malloc(sizeof(Matrice)); mat4 = (Matrice*)malloc(sizeof(Matrice)); mat5 = (Matrice*)malloc(sizeof(Matrice)); lst=(Liste*)malloc(sizeof(Liste)); lst2=(Liste*)malloc(sizeof(Liste)); lst3=(Liste*)malloc(sizeof(Liste)); lst4=(Liste*)malloc(sizeof(Liste)); lst5=(Liste*)malloc(sizeof(Liste)); mat->num=0; mat2->num=2; mat3->num=3; mat4->num=4; mat5->num=5; for(p=0;p<N;p++){ for(q=0;q<N;q++){ mat->tableau[p][q]=abs(q-p); mat2->tableau[p][q]=abs(q-p)+4*p; mat3->tableau[p][q]=abs(p-q)-5*q; mat4->tableau[p][q]=p+q; mat5->tableau[p][q]=abs(q-p); } } lst->m=(Matrice*)malloc(sizeof(Matrice)); lst->next =(Liste*)malloc(sizeof(Liste)); lst->m=mat; lst->next =lst2; lst2->m=(Matrice*)malloc(sizeof(Matrice)); lst2->next = (Liste*)malloc(sizeof(Liste)); lst2->m=mat2; lst2->next=lst3; lst3->m=(Matrice*)malloc(sizeof(Matrice)); lst3->next = (Liste*)malloc(sizeof(Liste)); lst3->m=mat3; lst3->next=lst4; lst4->m=(Matrice*)malloc(sizeof(Matrice)); lst4->next = (Liste*)malloc(sizeof(Liste)); lst4->m=mat4; lst4->next=lst5; lst5->m=(Matrice*)malloc(sizeof(Matrice)); lst5->next = (Liste*)malloc(sizeof(Liste)); lst5->m=mat5; lst5->next=lst; animation(lst);
                                              • /
/************* test 5 **********/ mat = (Matrice*)malloc(sizeof(Matrice)); mat2 = (Matrice*)malloc(sizeof(Matrice)); mat3 = (Matrice*)malloc(sizeof(Matrice)); mat4 = (Matrice*)malloc(sizeof(Matrice)); mat5 = (Matrice*)malloc(sizeof(Matrice)); lst=(Liste*)malloc(sizeof(Liste)); lst2=(Liste*)malloc(sizeof(Liste)); lst3=(Liste*)malloc(sizeof(Liste)); lst4=(Liste*)malloc(sizeof(Liste)); lst5=(Liste*)malloc(sizeof(Liste)); mat->num=0; mat2->num=2; mat3->num=3; mat4->num=4; mat5->num=5; for(p=0;p<N;p++){ for(q=0;q<N;q++){ mat->tableau[p][q]=0; mat2->tableau[p][q]=0; mat3->tableau[p][q]=0; mat4->tableau[p][q]=0; mat5->tableau[p][q]=0; } } mat->tableau[0][0]=5; mat2->tableau[0][0]=4; mat2->tableau[0][1]=1; mat3->tableau[0][0]=3; mat3->tableau[0][1]=1; mat3->tableau[1][0]=1; mat4->tableau[0][0]=2; mat4->tableau[0][1]=2; mat4->tableau[1][0]=1; mat5->tableau[0][0]=2; mat5->tableau[0][1]=1; mat5->tableau[1][0]=1; mat5->tableau[1][1]=1; lst->m=(Matrice*)malloc(sizeof(Matrice)); lst->next =(Liste*)malloc(sizeof(Liste)); lst->m=mat; lst->next =lst2; lst2->m=(Matrice*)malloc(sizeof(Matrice)); lst2->next = (Liste*)malloc(sizeof(Liste)); lst2->m=mat2; lst2->next=lst3; lst3->m=(Matrice*)malloc(sizeof(Matrice)); lst3->next = (Liste*)malloc(sizeof(Liste)); lst3->m=mat3; lst3->next=lst4; lst4->m=(Matrice*)malloc(sizeof(Matrice)); lst4->next = (Liste*)malloc(sizeof(Liste)); lst4->m=mat4; lst4->next=lst5; lst5->m=(Matrice*)malloc(sizeof(Matrice)); lst5->next = (Liste*)malloc(sizeof(Liste)); lst5->m=mat5; lst5->next=lst; animation(lst); /****************************/ gtk_main(); return 0; } gboolean on_darea_expose (GtkWidget *widget, GdkEventExpose *event, gpointer user_data) { gdk_draw_pixmap(widget->window, crayon,pixmap, 0,0,0,0, IMAGE_WIDTH*N, IMAGE_HEIGHT*N); }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.