Le source : TP-RemplissagePolygoneConvexe.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 2002 */
/* Contours d'un polygone convexe */
#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 "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 pixel(int x,int y) {
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 lignePixels(int xi,int yi,int xf,int yf) {
int i,cumul ;
int x = xi ;
int y = yi ;
int dx = xf - xi ;
int dy = yf - yi ;
int xinc = ( dx > 0 ) ? 1 : -1 ;
int yinc = ( dy > 0 ) ? 1 : -1 ;
dx = abs(dx) ;
dy = abs(dy) ;
pixel(x,y) ;
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) ; } }
else {
cumul = dy / 2 ;
for ( i = 1 ; i <= dy ; i++ ) {
y += yinc ;
cumul += dx ;
if ( cumul >= dy ) {
cumul -= dy ;
x += xinc ; }
pixel(x,y) ; } }
}
void cotesPolygoneDiscret(float *cd,float *cg,polygone *p) {
int i;
int min;
int max;
minMaxPolygone(&min,&max,p);
glPushMatrix() ;
glColor4fv(cd) ;
for ( i = min ; i != max ; i = (i+1)%p->np ) {
lignePixels((int) p->p[i].x,(int) p->p[i].y,(int) p->p[(i+1)%p->np].x,(int) p->p[(i+1)%p->np].y); }
glColor4fv(cg) ;
for ( i = max ; i != min ; i = (i+1)%p->np ) {
lignePixels((int) p->p[i].x,(int) p->p[i].y,(int) p->p[(i+1)%p->np].x,(int) p->p[(i+1)%p->np].y); }
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 ligneGauche(int xi,int yi,int xf,int yf,int *px) {
int i,cumul ;
int x = xi ;
int y = yi ;
int dx = xf - xi ;
int dy = yf - yi ;
int xinc = ( dx > 0 ) ? 1 : -1 ;
int yinc = ( dy > 0 ) ? 1 : -1 ;
dx = abs(dx) ;
dy = abs(dy) ;
px[y] = x ;
if ( dx > dy ) {
cumul = dx / 2 ;
for ( i = 1 ; i <= dx ; i++ ) {
x += xinc ;
cumul += dy ;
if (cumul >= dx) {
cumul -= dx ;
y += yinc ; }
if ( x < px[y] )
px[y] = x ; } }
else {
cumul = dy / 2 ;
for ( i = 1 ; i <= dy ; i++ ) {
y += yinc ;
cumul += dx ;
if ( cumul >= dy ) {
cumul -= dy ;
x += xinc ; }
if ( x < px[y] )
px[y] = x ; } }
}
void ligneDroite(int xi,int yi,int xf,int yf,int *px) {
int i,cumul ;
int x = xi ;
int y = yi ;
int dx = xf - xi ;
int dy = yf - yi ;
int xinc = ( dx > 0 ) ? 1 : -1 ;
int yinc = ( dy > 0 ) ? 1 : -1 ;
dx = abs(dx) ;
dy = abs(dy) ;
px[y] = x ;
if ( dx > dy ) {
cumul = dx / 2 ;
for ( i = 1 ; i <= dx ; i++ ) {
x += xinc ;
cumul += dy ;
if (cumul >= dx) {
cumul -= dx ;
y += yinc ; }
if ( x > px[y] )
px[y] = x ; } }
else {
cumul = dy / 2 ;
for ( i = 1 ; i <= dy ; i++ ) {
y += yinc ;
cumul += dx ;
if ( cumul >= dy ) {
cumul -= dy ;
x += xinc ; }
if ( x > px[y] )
px[y] = x ; } }
}
void contoursPolygoneDiscret(float *cd,float *cg,polygone *p) {
int i;
int min;
int max;
minMaxPolygone(&min,&max,p);
glPushMatrix() ;
int px1[60] ;
int px2[60] ;
for ( i = 0 ; i < 60 ; i++ ) {
px1[i] = -100 ;
px2[i] = 100 ; }
for ( i = min ; i != max ; i = (i+1)%p->np ) {
ligneDroite((int) p->p[i].x,(int) p->p[i].y,(int) p->p[(i+1)%p->np].x,(int) p->p[(i+1)%p->np].y,px1) ; }
for ( i = max ; i != min ; i = (i+1)%p->np ) {
ligneGauche((int) p->p[i].x,(int) p->p[i].y,(int) p->p[(i+1)%p->np].x,(int) p->p[(i+1)%p->np].y,px2) ; }
glColor4fv(cd) ;
for ( i = 0 ; i < 60 ; i++ )
if ( px1[i] != -100 )
pixel(px1[i],i);
glColor4fv(cg) ;
for ( i = 0 ; i < 60 ; i++ )
if ( px2[i] != 100 )
pixel(px2[i],i);
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 polygoneDiscret(float *cd,float *cg,float *c,polygone *p) {
int i;
int min;
int max;
minMaxPolygone(&min,&max,p);
glPushMatrix() ;
int px1[60] ;
int px2[60] ;
for ( i = 0 ; i < 60 ; i++ ) {
px1[i] = -100 ;
px2[i] = 100 ; }
for ( i = min ; i != max ; i = (i+1)%p->np ) {
ligneDroite((int) p->p[i].x,(int) p->p[i].y,(int) p->p[(i+1)%p->np].x,(int) p->p[(i+1)%p->np].y,px1) ; }
for ( i = max ; i != min ; i = (i+1)%p->np ) {
ligneGauche((int) p->p[i].x,(int) p->p[i].y,(int) p->p[(i+1)%p->np].x,(int) p->p[(i+1)%p->np].y,px2) ; }
glColor4fv(c) ;
for ( i = 0 ; i < 60 ; i++ )
if ( px1[i] != -100 )
for ( int x = px2[i] ; x < px1[i] ; x++ )
pixel(x,i);
glColor4fv(cd) ;
for ( i = 0 ; i < 60 ; i++ )
if ( px1[i] != -100 )
pixel(px1[i],i);
glColor4fv(cg) ;
for ( i = 0 ; i < 60 ; i++ )
if ( px2[i] != 100 )
pixel(px2[i],i);
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(),couleurVertFonce(),p) ;
break ;
case 3 : cotesPolygoneDiscret(couleurRouge(),couleurVertFonce(),p) ;
break ;
case 4 : contoursPolygoneDiscret(couleurRouge(),couleurVertFonce(),p) ;
break ;
case 5 : polygoneDiscret(couleurRouge(),couleurVertFonce(),couleurBleu(),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)%6;
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("Remplissage 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);
}