Thread: using basic library to write curve to bmp

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    5

    Unhappy using basic library to write curve to bmp

    Hi all, I am a beginner of C and I would like to try to plot a sin and cos curve with changing amplitude and frequency.Then, save them to a bmp file.

    I have searched some information about plotting curve and the background of bmp but I am still not sure how to put a curve to a bmp file.I know there is something like EasyBMP library to download in order to read/wrire bmp files.
    But I would like to use the standard libraries in C to do. Can anyone give some example code or where can I find some useful tutorials to get more information?

    Thanks a lot for everyone.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You basically allocate a block of memory you call 'screen', which has some means of allowing you to access it in 'x' and 'y' terms.
    You locate a point on your screen, and set a pixel to some colour.

    When you're done, write out the BMP file (probably the hardest part). But if the library you have is good enough, all you should need do is point at the memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is how you might draw your sine wave.

    The header files had a getmaxx() and getmaxy() for the screen, but you can plug in the numbers that you want, directly, if you don't have these functions.

    Code:
    #define pi 3.14159265  
    
      ypos = getmaxy() /2;
      crest = getmaxy() /4;
      width = getmaxx();
    
      for (i=0; i<width; i++)
      {
         swing=crest * sin(10*pi*i/width);
         putpixel(i, ypos+swing, RED);
         putpixel(i, ypos-swing, BLUE);
      }
    I'm curious if you can't put in a getchar() right after that is drawn, and then while the screen is being "held" in place, hold down the Ctrl key and press the PrtScrn (print screen) key.

    Which should put your screen into your system's memory. Then just open up Paint (Start >> Show All Programs >> Accessories >> Paint), and paste the sine wave, right into Paint.

    Now save the image as a BMP file, in Paint.

    I do this with regular screen's to post up screenshots, so it should work for you, as well.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    Thanks for your reply.

    For the code, do I need to include <allegro.h> because putpixel is a function in the library.So, is allegro.h a basic library in C or the library that I need to download and insert in the program?
    Actually I would like to use the basic library in C to draw the curve and export to bmp.

    For the bitmap, since I want to try to export graph in program directly to know more about the export of bitmap file in C, I don't use print screen directly.

    Thanks again for everyone~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  2. Some humour...
    By Stan100 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-06-2003, 10:25 PM
  3. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM
  4. This should be the Cprogramming.com anthem!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-21-2002, 12:01 AM