Pas de dépendances
#include <math.h> #include <sdl/sdl.h> #define XRES 800 #define YRES 600 #define MINX -5.0 #define MINY -6.0 #define MAXX 10.0 #define MAXY 10.0 #ifdef WIN32 #pragma comment(lib,"sdl.lib") #pragma comment(lib,"sdlmain.lib") #endif void waitkey() // attend qu'on appuie sur ESC { SDL_Event event; while(1) // boucle { while(SDL_PollEvent(&event)) // aquisition d'evenement { if (event.type == SDL_KEYDOWN) // on appuie sur une touche ? { if (event.key.keysym.sym == SDLK_ESCAPE) return; // c'est "ESC" ? } } SDL_Delay(1); } } void SDL_PutPixel32(SDL_Surface *surface, int x, int y, Uint32 pixel) { Uint8 *p; if (x<0 || y<0 || x>surface->w-1 || y>surface->h-1) return; p = (Uint8*)surface->pixels + y * surface->pitch + x * 4; *(Uint32*)p = pixel; } void Line(SDL_Surface* surf,int x1,int y1, int x2,int y2,Uint32 couleur) // Bresenham { int x,y; int Dx,Dy; int xincr,yincr; int erreur; int i; Dx = abs(x2-x1); Dy = abs(y2-y1); if(x1<x2) xincr = 1; else xincr = -1; if(y1<y2) yincr = 1; else yincr = -1; x = x1; y = y1; if(Dx>Dy) { erreur = Dx/2; for(i=0;i<Dx;i++) { x += xincr; erreur += Dy; if(erreur>Dx) { erreur -= Dx; y += yincr; } SDL_PutPixel32(surf,x, y,couleur); } } else { erreur = Dy/2; for(i=0;i<Dy;i++) { y += yincr; erreur += Dx; if(erreur>Dy) { erreur -= Dy; x += xincr; } SDL_PutPixel32(surf,x, y,couleur); } } SDL_PutPixel32(surf,x1,y1,couleur); SDL_PutPixel32(surf,x2,y2,couleur); } int Re_to_EcrX(double r) { return (int)(XRES*(r-MINX)/(MAXX-MINX)); } int Re_to_EcrY(double r) { int y = (int)(YRES*(r-MINY)/(MAXY-MINY)); y = YRES - y - 1; return y; } double Ecr_to_ReX(int i) { return MINX + i*(MAXX-MINX)/XRES; } double Ecr_to_ReY(int i) { return MAXY - i*(MAXY-MINY)/YRES; } void ShowFoncion(SDL_Surface* screen,Uint32 couleur,double (*fonc)(double)) { int i; int lastx = 0; int lasty = 0; int x,y; double resfonc; for(i=0;i<XRES;i++) { x = i; resfonc = fonc(Ecr_to_ReX(i)); y = Re_to_EcrY(resfonc); if (i!=0) Line(screen,x,y,lastx,lasty,couleur); lastx = x; lasty = y; } } void ShowParametrique(SDL_Surface* screen,Uint32 couleur,double (*xfonc)(double),double (*yfonc)(double),double tmin,double tmax,double tstep) { int lastx = 0; int lasty = 0; int x,y; int i = 0; double t = tmin; while(t<tmax) { x = Re_to_EcrX(xfonc(t)); y = Re_to_EcrY(yfonc(t)); if (i!=0) Line(screen,x,y,lastx,lasty,couleur); lastx = x; lasty = y; t+=tstep; i++; } } void ShowAxis(SDL_Surface* screen) { int centreX = Re_to_EcrX(0.0); int centreY = Re_to_EcrY(0.0); Line(screen,centreX,0,centreX,YRES-1,0xFFFFFF); // axe vertical Line(screen,0,centreY,XRES-1,centreY,0xFFFFFF); // axe horizontal while(0); } double myx(double t) { return (t/3.0)*cos(t); } double myy(double t) { return (t/3.0)*sin(t); } int main(int argc,char** argv) { SDL_Surface* screen; SDL_Init(SDL_INIT_VIDEO); screen=SDL_SetVideoMode(XRES,YRES,32,SDL_SWSURFACE|SDL_DOUBLEBUF); if (SDL_MUSTLOCK(screen)) SDL_LockSurface(screen); ShowAxis(screen); ShowFoncion(screen,0xFF0000,cos); ShowParametrique(screen,0x00FF00,cos,sin,0.0,2*3.15,0.01); ShowParametrique(screen,0x0000FF,myx,myy,0.0,15.0,0.01); if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); SDL_Flip(screen); waitkey(); return 0; }
Voici comment afficher des fonctions paramétriques. Voir exemple précédent pour afficher des fonctions cartésiennes. Cette fois, je passe 2 fonctions dans "ShowParametrique", une fonction x(t) et une y(t). Je passe également le tmin et le tmax que je veux observer, avec un tstep qui indiquera le "pas" que je voudrais faire. Plus le pas sera petit, plus lisse sera la courbe, plus il sera grand, plus elle sera segmentée.