#include #include #include #define xc_min 20 // on definit un carre en donnant 2 points #define xc_max 70 // le coin superieure a gauche #define yc_min 20 // et le coin inferieure a droite #define yc_max 70 /* Info.Graphique(Infographie) Fenetrage / Clipping de droite 26-02-04 ©2004 http://www.Software-DS.com Didier STRAUS Compilation : (Fonctionne sur MacOSX si vous avez installe X11) gcc -o clipping clipping.c -L/usr/X11R6/lib -lX11 */ typedef struct { unsigned gauche; unsigned droite; unsigned inf; unsigned sup; unsigned somme; /* permet de connaitre rapidement si le segment est visible */ } code; /* prototypes */ void contact_serveur(); void Creat_fenetre(void); void gerer_evenement(); code CalculCode(double x, double y, double xMin, double xMax, double yMin, double yMax); void Cohen_Sutherland(double x1, double y1, double x2, double y2, double xMin, double xMax, double yMin, double yMax, int valeur); /* variables globales */ GC gc; Display *display; int screen; Window win,root; int win_larg,win_haut; unsigned long white_pixel, black_pixel; /* ******************************************************************** */ void contact_serveur() { if ((display = XOpenDisplay("")) == NULL) { fprintf(stderr, "Can't open Display\n"); exit(1); } } /* contact_serveur */ /* ******************************************************************** */ void Creat_fenetre(void) { short border_width; screen = DefaultScreen(display); root = RootWindow(display,screen); white_pixel= WhitePixel(display,screen); black_pixel= BlackPixel(display,screen); border_width = 3; win_larg = DisplayWidth(display,screen)/4; win_haut = DisplayHeight(display,screen)/4; win = XCreateSimpleWindow(display,root,0,0,win_larg,win_haut, border_width,black_pixel,white_pixel); gc=DefaultGC(display,screen); XStoreName(display,win,"Clipping - DS"); XMapWindow(display,win); } /* Creat_fenetre */ /* ******************************************************************** */ void gerer_evenement() { char *str1="http://www.Software-DS.com"; int i,a,b,c,d; XSelectInput(display,win,ExposureMask | KeyPressMask); for(;;) { XEvent ev; XNextEvent(display,&ev); switch(ev.type){ case Expose : /* On va afficher des dessins ;) */ /* on dessine un carre */ XDrawLine(display,win,gc,xc_min, yc_min, xc_min, yc_max); XDrawLine(display,win,gc,xc_max, yc_max, xc_min, yc_max); XDrawLine(display,win,gc,xc_max, yc_max, xc_max, yc_min); XDrawLine(display,win,gc,xc_min, yc_min, xc_max, yc_min); /* les droites a afficher dans ce carre */ Cohen_Sutherland(80,10,60,30 ,xc_min, xc_max, yc_min, yc_max,0); Cohen_Sutherland(60,60,80,80 ,xc_min, xc_max, yc_min, yc_max,0); Cohen_Sutherland(10,80,40,50 ,xc_min, xc_max, yc_min, yc_max,0); Cohen_Sutherland(10,10,30,30 ,xc_min, xc_max, yc_min, yc_max,0); Cohen_Sutherland(10,60,45,40 ,xc_min, xc_max, yc_min, yc_max,0); Cohen_Sutherland(40,10,40,40 ,xc_min, xc_max, yc_min, yc_max,0); Cohen_Sutherland(55,40,80,60 ,xc_min, xc_max, yc_min, yc_max,0); Cohen_Sutherland(50,50,40,80 ,xc_min, xc_max, yc_min, yc_max,0); break; case KeyPress : // on appuie sur une touche pour quitter XClearWindow(display,win); XDrawString(display,win,gc,win_larg/5,win_haut*2/5,str1,strlen(str1)); XFlush(display); sleep(4); // petite pause avant de fermer XDestroyWindow(display,win); XCloseDisplay(display); exit(0); break; default : fprintf(stderr,"J'ai recu un evt : %d\n",ev.type); break; } /* switch */ } /* for */ } /* gerer_evts */ /* ******************************************************************** */ code CalculCode(double x, double y, double xMin, double xMax, double yMin, double yMax) { /* Calcul le code d'un point(x,y) d'un segment de droite (Une extremite du segment) */ code c; /* On initialise tout a zero */ c.droite=0; c.gauche=0; c.sup=0; c.inf=0; c.somme=0; /* On fait les tests */ if (x>xMax) { c.droite = 1; c.somme++; } else if (xyMax) { c.sup = 1; c.somme++; } else if (y