You are not logged in.

#1 2009-10-05 14:27:26

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

SDL-Graph, a tiny C library for printing graphs

I've made a small API for printing graphs, using SDL in, well, C (making binds for C++ should be very easy too :V) tongue Mostly for my own learning and benefit, but I think I can see some uses for this for others too as it is pretty simple to use, so I decided to post it here.

http://code.google.com/p/sdl-grapher/

The following documentation (in the header file) should work for a start. There is also an example.c file.

Offline

#2 2009-10-12 15:31:22

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: SDL-Graph, a tiny C library for printing graphs

Support for printing current Graph to a .bmp file has been added via write_BMP() eg. write_BMP(&myGraph, "crazystuff.bmp"); if anyone was using this. A couple of bugs have been fixed too :') Making crazy wallpapers ftw.

Offline

#3 2009-10-12 18:43:39

makimaki
Member
From: Ireland
Registered: 2009-04-02
Posts: 109

Re: SDL-Graph, a tiny C library for printing graphs

Thanks Themaister, that's a really nice little lib. I have been using Xgraphics or gnuplot to plot mine, but I'll have a play with your work. smile


====* -- Joke
    O
    \|/ --- Me
    / \             Whooooosh

Offline

#4 2009-10-15 19:27:37

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: SDL-Graph, a tiny C library for printing graphs

Been playing around with fractals, especially http://en.wikipedia.org/wiki/Mandelbrot_set. Holy tacos, it's awesome big_smile Gief fancy color shading algorithms too^^

http://maister.homelinux.org/downloads/fractal.jpg

#include <sdlgraph.h>

int main()
{
    Graph a;

    a.xMin = -10.0;
    a.xMax = 10.0;
    a.yMin = -10.0;
    a.yMax = 10.0;
    a.xScale = 1.0;
    a.yScale = 1.0;
    a.height = 1000;
    a.width = 1000; // Try this with 10000*10000 and watch your CPU and harddrive cry :D 300MB .bmp ftw.

    init_graph(&a);

    int x, y;
    float xcoor, ycoor, XTEMP, X, Y;
    int iteration, max = 200;

    for ( x = 0; x < 1000; x++ )
    {
        for ( y = 0; y < 1000; y++ )
        {
            xcoor = x/500.0-1.0;
            ycoor = y/500.0-1.0;
            iteration = 0; X = 0; Y = 0;
            while ( X*X + Y*Y <= 100.0 && iteration < max )
            {
                XTEMP = X*X - Y*Y + xcoor;
               Y = 2*X*Y + ycoor;
                X = XTEMP;
               iteration++;    
            }

            if ( iteration == max )
            {
                set_color ( &a, 0, 0, 0);
            }
            else
            {
                set_color(&a, 255*(float)iteration/(float)max, 7+230*(float)iteration/(float)max, 11+200*(float)iteration/(float)max);
            }

                print_pixel_by_window(&a, x, y); // Using the windows coords rather than graph coords :p Nice for printing full pictures rather than plain graphs.
                //fprintf(stderr, "Printing pixel %4d, %4d\n", x, y);
        }
    }

    update_screen(&a);
    write_BMP(&a, "fractal.bmp");

    idle();
        quit();
        return 0;
}

Last edited by Themaister (2009-10-15 19:32:21)

Offline

Board footer

Powered by FluxBB