Thread: Graphing data

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    12

    Graphing data

    Hi there,

    I have a little problem. I'm running numerical simulations in C that run a large numer of timesteps (1000's). I want to create animations of this process in 2d.

    I was wondering if anyone knows of any way that I can implement this in a C program or any apps that can do this or are made for this purpose?

    Thank your for your help in advance.

    Wayne.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    First pick a graphics API, for 2d graphs I'd say SDL should do what you need and be easy to use.

    Here you can find tutorials on how to set it up to work with various OS/compilers.

    Then all you need to do is link it to your project.

    Heres some code I made that sets up a screen buffer and draws an animated sine wave:
    Code:
    #include <SDL\SDL.h>
    #include <math.h>
    
    #define GRAPHICS_WIDTH 256
    #define GRAPHICS_HEIGHT 256
    #define BITDEPTH 32
    #define DEG_TO_RAD 0.0174532925
    
    SDL_Surface *screen = NULL;  //This will be the screen buffer
    SDL_Event event;
    
    void WritePixel(int x, int y, Uint32 col)
    {
        Uint32 *pix;  
        pix=screen->pixels+(x+y*GRAPHICS_WIDTH)*4;
        *pix=col; 
    }  
    
    int main()
    {
    	//First we set up the screen. If something goes wrong we quit the prog.
    	if(SDL_Init(SDL_INIT_VIDEO) !=0) 
    		return 1;
       	screen = SDL_SetVideoMode(GRAPHICS_WIDTH, GRAPHICS_HEIGHT, BITDEPTH, 0);
    	if(screen == NULL) 
    		return 1;
    	
    	int x;
    	int quit = 0;
    	int anim = 0;	
    
    	//This is the main loop that repeats until the program is ended
    	while(!quit)
    	{
    		//Start each loop by clearing the screen
    		SDL_FillRect(screen, NULL, 0);		
    		
    		//Draw stuff
    		for(x=0; x<GRAPHICS_WIDTH; x++)
    			WritePixel(x, 128+cos(x*DEG_TO_RAD+anim)*100, 0xFFFFFFFF);
    
    		//Flip it into view
    		if(SDL_Flip(screen)==-1) 
    			return 1;
    		
    		//This code checks for a keypress
    		while(SDL_PollEvent(&event))
               if(event.type==SDL_KEYDOWN)
                   quit=1;
    
    		SDL_Delay(50);
    		if(++anim > 359) anim = 0; 	//Increment  animation
    	}
    
    	SDL_Quit();
    	return 0;
    }
    You should be able to get it to draw different kinds of graphs by playing around with it.

    Hope that helps.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    12
    it does indeed, thanks a million for your fast reply!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM