Simple Case‎ > ‎

### Exp2. Drawing A Mathematical Formula

This example demonstrate
ATest_sEx2.zip 667k

Screen Shot

Program Code
A LineTo Function can draw a line
 // This function will draw a line on surface "scr" from (x1,y1) to (x2,y2)inline void LineTo(SDL_Surface *scr , int x1 , int y1 , int x2 , int y2 ) {    SDL_Rect dotRect; // For drawing a dot     int vtx , vty; // The vector from (x1,y1) to (x2,y2)    int x = x1 , y = y1; // The coordinate of current interpolate point    vtx = x2 - x1 ;    vty = y2 - y1 ;// This can deal with the single case    if(vtx == 0 && vty == 0 ) {        dotRect.x = x;        dotRect.x = x;        dotRect.w = dotRect.h = 2;        SDL_FillRect(scr , &dotRect , 0xFFFF22);        return;        }// This can deal with the single case    if(vtx == 0) {        dotRect.x = x;        if(y1 < y2) dotRect.y = y1;        if(y2 < y1) dotRect.y = y2;        dotRect.w = 2 ;        dotRect.h = abs(vty) ;        SDL_FillRect(scr , &dotRect , 0xFFFF22);        return;        }// This can deal with the single case    if(vty == 0) {        dotRect.y = y;        if(x1 < x2) dotRect.x = x1;        if(x2 < x1) dotRect.x = x2;        dotRect.w = abs(vtx) ;        dotRect.h = 2 ;        SDL_FillRect(scr , &dotRect , 0xFFFF22);        return;        }    // Case vtx >= vty    // Take the bigger one as basis to draw a line , so we can    // get a continuous line instead of a broken one.     if( abs(vtx) >= abs(vty) ) {  // abs( x ) return absolute value of x        while( abs(x-x1) <= abs(vtx) ) {            y = y1 + vty * (x - x1) / vtx ; // Get y base on the ratio of x -x1 / vtx            dotRect.x = x;            dotRect.y = y;            dotRect.w = dotRect.h = 2 ;            SDL_FillRect(scr , &dotRect , 0xFFFF22);            if(vtx>0) x++;            if(vtx<0) x--;            } // while( y - y1 < vty ) { END        } // if( abs(vtx) >= abs(vty) ) {END    // Case vtx < vty    if( abs(vtx) < abs(vty) ) {        while( abs(y - y1) <= abs(vty) ) {            x = x1 + vtx * (y - y1) / vty ;            dotRect.x = x;            dotRect.y = y;            dotRect.w = dotRect.h = 2 ;            SDL_FillRect(scr , &dotRect , 0xFFFF22);            if(vty>0) y++;            if(vty<0) y--;            } // while( y - y1 < vty ) { END        } // if( abs(vtx) < abs(vty) ) { END    return;    }// inline void LineTo(...) { END

Some tricks when using C++
 // Some small skill that good to know// Take Absolute Valueinline int abs(int a) {return a>=0 ? a : (-a) ; }inline int MaxNo(int a,int b) {return a>=b ? a : b ; }inline bool MaxNo(float a,float b) {return a>=b ? 0 : 1 ; }
This is not a very important trick but good to know.

In the main program

In the Polar Coordinate System ,
x = r sin( theta )
y = r cos( theta )

Here demonstrate a way to draw mathematical formula in a screen.    You can modify it and try
other functions.

In the main program , demonstrate a way to display a picture that the location lower than 0
The concept is , each object has its coordinate , named (xidx , yidx) in here.
And it's not the same as the coordinate of its picture.    They're different.    The picture of
an object , is its body , or outlook.    The coordinate of a picture should base on the object.
animRect.x = obj.xidx + offsetX ;
animRect.y = obj.yidx + offsetY;

Exp2 END
.