Draw cartesian functions.

Draw cartesian functions.

See version :

Pas de dépendances

Download :

#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;
    }
}



double mon_polynome(double x)
{
    // changez ici la fonction que vous voulez afficher. (pas forcément un polynome)
    return 0.3*x*x*x + 0.2*x*x + 0.1*x -1;
}




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);


}

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);
    ShowFoncion(screen,0xFFFF00,sin);
    ShowFoncion(screen,0x00FF00,mon_polynome);
    
    
    if (SDL_MUSTLOCK(screen))
        SDL_UnlockSurface(screen);        
    SDL_Flip(screen);
    waitkey();
    return 0;
}





Explanations

	No explanations yet.