Le source: SchemaCodeClipping.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Code des extremites des segments */
/* dans l'algorithme de clipping */
/* de Cohen-Sutherland */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleMenus.h"
#include "ModuleFont.h"
#include "ModuleFleche.h"
#include "ModuleReshape.h"
struct point {
int x ;
int y ; } ;
struct segment {
point pi ;
point pf ; } ;
struct rectangle {
point sg ;
point id ; } ;
static int aff = 0 ;
static segment s = { -8,6,12,-2 } ;
static int p1 = 1 ;
static int disc = 1 ;
void traceSegment(int xi,int yi,int xf,int yf,float *c) {
glColor4fv(c) ;
glBegin(GL_LINES) ;
glVertex2f((float) xi,(float) yi) ;
glVertex2f((float) xf,(float) yf) ;
glEnd() ;
}
void pixel(int x,int y,float *c) {
glColor4fv(c) ;
glBegin(GL_QUADS) ;
glVertex2f(x-0.5F,y-0.5F) ;
glVertex2f(x-0.5F,y+0.5F) ;
glVertex2f(x+0.5F,y+0.5F) ;
glVertex2f(x+0.5F,y-0.5F) ;
glEnd() ;
}
void line(int xi,int yi,int xf,int yf,float *c) {
if ( disc ) {
int dx,dy,i,xinc,yinc,cumul,x,y ;
x = xi ;
y = yi ;
dx = xf - xi ;
dy = yf - yi ;
xinc = ( dx > 0 ) ? 1 : -1 ;
yinc = ( dy > 0 ) ? 1 : -1 ;
dx = abs(dx) ;
dy = abs(dy) ;
pixel(x,y,c) ;
if ( dx > dy ) {
cumul = dx / 2 ;
for ( i = 1 ; i <= dx ; i++ ) {
x += xinc ;
cumul += dy ;
if (cumul >= dx) {
cumul -= dx ;
y += yinc ; }
pixel(x,y,c) ; } }
else {
cumul = dy / 2 ;
for ( i = 1 ; i <= dy ; i++ ) {
y += yinc ;
cumul += dx ;
if ( cumul >= dy ) {
cumul -= dy ;
x += xinc ; }
pixel(x,y,c) ; } } }
else {
glColor4fv(c);
glLineWidth(4.0);
glBegin(GL_LINES);
glVertex2f(xi,yi);
glVertex2f(xf,yf);
glEnd();
glLineWidth(1.0); }
}
void traceRectangle(rectangle *r,float *c,float *c2) {
line(r->id.x,-100,r->id.x,100,c2) ;
line(-100,r->sg.y,100,r->sg.y,c2) ;
line(r->sg.x,-100,r->sg.x,100,c2) ;
line(-100,r->id.y,100,r->id.y,c2) ;
line(r->id.x,r->id.y,r->id.x,r->sg.y,c) ;
line(r->id.x,r->sg.y,r->sg.x,r->sg.y,c) ;
line(r->id.x,r->id.y,r->sg.x,r->id.y,c) ;
line(r->sg.x,r->id.y,r->sg.x,r->sg.y,c) ;
}
int code(point *p,rectangle *r) {
int c ;
if ( p->x < r->sg.x )
c = 1 ;
else
c = 0 ;
if ( p->x > r->id.x )
c += 2 ;
if ( p->y < r->sg.y )
c += 4 ;
if ( p->y > r->id.y )
c += 8 ;
return(c) ;
}
int code_nul(int c) {
return(c == 0) ;
}
int pas1commun(int c1,int c2) {
return((c1&c2) == 0) ;
}
int code0(int c) {
return(c&1) ;
}
int code1(int c) {
return(c&2) ;
}
int code2(int c) {
return(c&4) ;
}
int code3(int c) {
return(c&8) ;
}
char *code(int c) {
static char s[4];
s[0] = ( c & 1 ) ? '1' : '0';
s[1] = ( c & 2 ) ? '1' : '0';
s[2] = ( c & 4 ) ? '1' : '0';
s[3] = ( c & 8 ) ? '1' : '0';
return(s);
}
int intersection(int ai,int bi,int af,int bf,int val) {
int res = 0 ;
if ( af-ai != 0 )
res =(int) (bi +(double) (val-ai) / (af-ai) * (bf-bi)) ;
else
res = 32767 ;
return(res) ;
}
void clipperSegment(rectangle *r,segment *s,float *c) {
segment se = *s ;
int c1,c2 ;
point p ;
c1 = code(&s->pi,r) ;
c2 = code(&s->pf,r) ;
while ( ( !code_nul(c1) || !code_nul(c2) ) && ( pas1commun(c1,c2) ) ) {
if ( code_nul(c1) ) {
p = s->pi ;
s->pi = s->pf ;
s->pf = p ;
c1 = c2 ; }
if ( code0(c1) ) {
s->pi.y = intersection(s->pi.x,s->pi.y,s->pf.x,s->pf.y,r->sg.x) ;
s->pi.x = r->sg.x ; }
else
if ( code1(c1) ) {
s->pi.y = intersection(s->pi.x,s->pi.y,s->pf.x,s->pf.y,r->id.x) ;
s->pi.x = r->id.x ; }
else
if ( code2(c1) ) {
s->pi.x = intersection(s->pi.y,s->pi.x,s->pf.y,s->pf.x,r->sg.y) ;
s->pi.y = r->sg.y ; }
else
if ( code3(c1) ) {
s->pi.x = intersection(s->pi.y,s->pi.x,s->pf.y,s->pf.x,r->id.y) ;
s->pi.y = r->id.y ; }
c1 = code(&s->pi,r) ;
c2 = code(&s->pf,r) ; }
if ( code_nul(c1) && code_nul(c2) )
line(s->pi.x,s->pi.y,s->pf.x,s->pf.y,c) ;
*s = se ;
}
void traceAxes(void) {
glColor4fv(couleurVertFonce());
glPushMatrix();
glTranslatef(-14.0F,-9.0F,0.0F);
flecheEnVolume(10.0F,0.0F,0.0F,1.0F,3.5F,0.3F);
flecheEnVolume(0.0F,10.0F,0.0F,1.0F,3.5F,0.3F);
glPopMatrix();
placeFontCursor(-2.0F,-9.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"x");
placeFontCursor(-12.0F,3.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"y");
}
float *couleurDeTrace(segment *s,rectangle *r,int *cas) {
int ci = code(&s->pi,r);
int cf = code(&s->pf,r);
float *c = couleurMagenta();
if ( ( ci == 0 ) && ( cf == 0 ) ) {
c = couleurBleu();
*cas = 1; }
else
if ( ( ci != 0 ) && ( cf != 0 ) ) {
c = couleurMagenta();
*cas = 4;
if ( ( ci & cf ) != 0 ) {
*cas = 3;
c = couleurVertFonce(); } }
else {
c = couleurRouge();
*cas = 2; }
return(c);
}
void traceSegment(segment *s,rectangle *r) {
int cas;
float *c = couleurDeTrace(s,r,&cas);
line(s->pi.x,s->pi.y,s->pf.x,s->pf.y,c) ;
placeFontCursor((s->pf.x+s->pi.x)/2,(s->pf.y+s->pi.y)/2,0.0F);
deplacementCursor(-15,-15,0);
glColor4fv(c);
simpleBitmapOutput(1,REGULAR8x13,"(%d)",cas);
}
void affichageCodes(void) {
placeFontCursor(-29.0F,0.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"1000");
placeFontCursor(0.0F,0.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"0000");
placeFontCursor(28.0F,0.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"0100");
placeFontCursor(-29.0F,24.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"1001");
placeFontCursor(0.0F,24.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"0001");
placeFontCursor(28.0F,24.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"0101");
placeFontCursor(-29.0F,-24.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"1010");
placeFontCursor(0.0F,-24.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"0010");
placeFontCursor(28.0F,-24.0F,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"0110");
}
void display() {
rectangle r = { -20,-16,19,14 } ;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
int ci = code(&s.pi,&r);
int cf = code(&s.pf,&r);
int cas;
float *c = couleurDeTrace(&s,&r,&cas);
switch ( aff) {
case 0 : traceAxes();
traceRectangle(&r,couleurBleu(),couleurJauneFonce());
affichageCodes();
glColor4fv(couleurNoir());
placeFontCursor(r.id.x,r.id.y-3,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"(Xmax,Ymax)");
placeFontCursor(r.sg.x,r.sg.y+3,0.0F);
simpleBitmapOutput(1,REGULAR8x13,"(Xmin,Ymin)");
break;
case 1 : traceAxes();
traceRectangle(&r,couleurBleu(),couleurJauneFonce());
line(s.pi.x,s.pi.y,s.pf.x,s.pf.y,c) ;
pixel(s.pi.x,s.pi.y,couleurNoir()) ;
pixel(s.pf.x,s.pf.y,couleurNoir()) ;
placeFontCursor(s.pi.x,s.pi.y,0.0F);
deplacementCursor(0,-20,0);
simpleBitmapOutput(1,REGULAR8x13,"(%s)",code(code(&s.pi,&r)));
placeFontCursor(s.pf.x,s.pf.y,0.0F);
deplacementCursor(0,20,0);
simpleBitmapOutput(1,REGULAR8x13,"(%s)",code(code(&s.pf,&r)));
placeFontCursor((s.pf.x+s.pi.x)/2,(s.pf.y+s.pi.y)/2,0.0F);
deplacementCursor(10,-20,0);
glColor4fv(c);
simpleBitmapOutput(1,REGULAR8x13,"cas (%d)",cas);
break ;
case 2 : traceAxes();
traceRectangle(&r,couleurBleu(),couleurJauneFonce());
clipperSegment(&r,&s,c) ;
pixel(s.pi.x,s.pi.y,couleurNoir()) ;
pixel(s.pf.x,s.pf.y,couleurNoir()) ;
placeFontCursor(s.pi.x,s.pi.y,0.0F);
deplacementCursor(0,-20,0);
simpleBitmapOutput(1,REGULAR8x13,"(%s)",code(code(&s.pi,&r)));
placeFontCursor(s.pf.x,s.pf.y,0.0F);
deplacementCursor(0,20,0);
simpleBitmapOutput(1,REGULAR8x13,"(%s)",code(code(&s.pf,&r)));
placeFontCursor((s.pf.x+s.pi.x)/2,(s.pf.y+s.pi.y)/2,0.0F);
deplacementCursor(10,-20,0);
glColor4fv(c);
simpleBitmapOutput(1,REGULAR8x13,"cas (%d)",cas);
break ;
case 3 : { traceAxes();
traceRectangle(&r,couleurBleu(),couleurJauneFonce());
glColor4fv(couleurNoir());
affichageCodes();
segment s1 = { -8,3,12,8 } ;
traceSegment(&s1,&r);
segment s2 = { -6,-22,17,-10 } ;
traceSegment(&s2,&r);
segment s3 = { -30,11,-5,27 } ;
traceSegment(&s3,&r);
segment s4 = { -32,5,25,22 } ;
traceSegment(&s4,&r);
segment s5 = { 36,-20,31,20 } ;
traceSegment(&s5,&r);
segment s6 = { 3,-29,-28,-20 } ;
traceSegment(&s6,&r); }
break ; }
glPopMatrix();
glFlush();
glutSwapBuffers() ;
}
void special(int k, int x, int y) {
switch (k) {
case GLUT_KEY_UP : if ( p1 ) {
s.pi.y++ ;
if ( s.pi.y > 30 )
s.pi.y = 30 ; }
else {
s.pf.y++ ;
if ( s.pf.y > 30 )
s.pf.y = 30 ; }
glutPostRedisplay();
break;
case GLUT_KEY_DOWN : if ( p1 ) {
s.pi.y-- ;
if ( s.pi.y < -30 )
s.pi.y = -30 ; }
else {
s.pf.y-- ;
if ( s.pf.y < -30 )
s.pf.y = -30 ; }
glutPostRedisplay();
break;
case GLUT_KEY_LEFT : if ( p1 ) {
s.pi.x-- ;
if ( s.pi.x < -35 )
s.pi.x = -35 ; }
else {
s.pf.x-- ;
if ( s.pf.x < -35 )
s.pf.x = -35 ; }
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT : if ( p1 ) {
s.pi.x++ ;
if ( s.pi.x > 35 )
s.pi.x = 35 ; }
else {
s.pf.x++ ;
if ( s.pf.x > 35 )
s.pf.x = 35 ; }
glutPostRedisplay();
break; }
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case 0x0D : aff = (aff+1)%4 ;
glutPostRedisplay();
break;
case 32 : p1 = !p1 ;
glutPostRedisplay();
break;
case 'D' :
case 'd' : disc = !disc ;
glutPostRedisplay();
break;
case 0x1B : exit(0);
break; }
}
void myinit() {
glShadeModel(GL_SMOOTH);
glClearColor(0.8F,0.8F,0.8F,1.0F);
setAlignement(CENTER);
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,240);
glutInitWindowPosition(50,50);
glutCreateWindow("Code de Cohen-Sutherland");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-30.0,30.0,-30.0,30.0,-500.0,500.0);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}