/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Mars 2002 */ /* Contours d'un polygone convexe */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #include "ModuleFont.h" struct coord3D { float x; float y; } ; struct polygone { int np; coord3D *p; } ; static int aff = 0 ; static int pol = 0 ; static coord3D pt1[5] = { { 55,30 }, { 35,48 }, { 20,44 }, { 9,18 }, { 44,13 } }; static coord3D pt2[7] = { { 56,32 }, { 37,52 }, { 20,52 }, { 9,29 }, { 23,13 }, { 40,8 }, { 49,11 } }; static polygone p1 = { 5,pt1 }; static polygone p2 = { 7,pt2 }; void myinit() { glClearColor(0.8F,0.8F,0.8F,1.0F); setAlignement(CENTER); } void dessinePolygone(float *coul,polygone *p) { int i; glPushMatrix() ; glColor4fv(coul) ; glBegin(GL_POLYGON) ; for ( i = 0 ; i < p->np ; i++ ) glVertex2fv((float *) &p->p[i]); glEnd() ; glColor4fv(couleurNoir()) ; glLineWidth(2.0) ; glBegin(GL_LINE_LOOP) ; for ( i = 0 ; i < p->np ; i++ ) glVertex2fv((float *) &p->p[i]); glEnd() ; glLineWidth(1.0) ; for ( i = 0 ; i < p->np ; i++ ) { float x = p->p[i].x-30; float y = p->p[i].y-30; float d = sqrt(x*x+y*y); x /= d/15.0F; y /= d/15.0F; placeFontCursor(p->p[i].x,p->p[i].y,0.0) ; deplacementCursor(x,-y,0) ; simpleBitmapOutput(1,REGULAR8x13,"P%d",i) ; deplacementCursor(x,(p->p[i].y>30) ? -y-16:-y+16,0) ; simpleBitmapOutput(1,REGULAR8x13,"(%d,%d)",(int) p->p[i].x,(int) p->p[i].y) ; } glPopMatrix() ; } void minMaxPolygone(int *min,int *max,polygone *p) { int i; for ( i = 0 ; i < p->np ; i++ ) { p->p[i] = p->p[i]; } *min = 0; *max = 0; for ( i = 0 ; i < p->np ; i++ ) { if ( p->p[*min].y > p->p[i].y ) *min = i ; if ( p->p[*max].y < p->p[i].y ) *max = i ; } } void affichageMinMaxPolygone(polygone *p) { dessinePolygone(couleurRouge(),p); int min; int max; minMaxPolygone(&min,&max,p); glColor4fv(couleurJaune()) ; placeFontCursor(30.0,30.0,1.0) ; simpleBitmapOutput(1,REGULAR8x13,"MIN = %d",min) ; deplacementCursor(0,20,0) ; simpleBitmapOutput(1,REGULAR8x13,"MAX = %d",max) ; } void contoursPolygone(float *cd,float *cg,polygone *p) { int i; int min; int max; minMaxPolygone(&min,&max,p); glPushMatrix() ; glLineWidth(2.0) ; glColor4fv(cd) ; glBegin(GL_LINE_STRIP) ; for ( i = min ; i != max ; i = (i+1)%p->np ) { glVertex2fv((float *) &p->p[i]) ; } glVertex2fv((float *) &p->p[i]) ; glEnd() ; glColor4fv(cg) ; glBegin(GL_LINE_STRIP) ; for ( i = max ; i != min ; i = (i+1)%p->np ) { glVertex2fv((float *) &p->p[i]) ; } glVertex2fv((float *) &p->p[i]) ; glEnd() ; glLineWidth(1.0) ; glColor4fv(couleurNoir()) ; for ( i = 0 ; i < p->np ; i++ ) { float x = p->p[i].x-30; float y = p->p[i].y-30; float d = sqrt(x*x+y*y); x /= d/15.0F; y /= d/15.0F; placeFontCursor(p->p[i].x,p->p[i].y,0.0) ; deplacementCursor(x,-y,0) ; simpleBitmapOutput(1,REGULAR8x13,"P%d",i) ; deplacementCursor(x,(p->p[i].y>30) ? -y-16:-y+16,0) ; simpleBitmapOutput(1,REGULAR8x13,"(%d,%d)",(int) p->p[i].x,(int) p->p[i].y) ; } glPopMatrix() ; } void display() { polygone *p; switch ( pol ) { case 0 : p = &p1; break; case 1 : p = &p2; break; } glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); switch (aff) { case 0 : dessinePolygone(couleurRouge(),p) ; break ; case 1 : affichageMinMaxPolygone(p) ; break ; case 2 : contoursPolygone(couleurRouge(),couleurVert(),p) ; break ; } glPopMatrix(); glFlush(); glutSwapBuffers() ; } void key(unsigned char key,int x,int y) { switch ( key ) { case 0x1B : exit(0); break; case 0x0D : aff = (aff+1)%3; glutPostRedisplay(); break; case 'p' : case 'P' : pol = (pol+1)%2; glutPostRedisplay(); break; } } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(340,300); glutInitWindowPosition(50,50); glutCreateWindow("Contours d'un polygone convexe"); myinit(); creationMenuBasique(); setParametresOrthoBasique(0.0,60.0,0.0,60.0,-50.0,50.0); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutDisplayFunc(display); glutMainLoop(); return(0); }