Thread: How to plot a graph using C

  1. #1
    Registered User
    Join Date
    Apr 2009

    How to plot a graph using C

    Hi.. I am a beginner in C and I work on linux platform. I need to plot a simple X-Y graph. I have the x co-ords and y co-ords saved in two different integer arrays in correspoding order. I basically need to see if I get a straight line or not. Can I please get help with a sample code or any bits that might help? Thanks a lot.

  2. #2
    Registered User
    Join Date
    Apr 2007
    If you are just wanting to know if it is a straight line or not (as in a yes or no answer), just ask yourself how you would check that with pen and paper, then implement the same check in code.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Well you can determine characteristics of the line mathematically. If you still want to visibly display the graph, you should look into a graphics library. SDL might be a good choice. ( Then of course you could use | \ / and -'s to display the line!

  4. #4
    Math wizard
    Join Date
    Dec 2006
    An alternative is to write a BMP file. When I wanted to see how the landscape of my 2D game was going to be, I had the program write a BMP file, at 72,000x48,000 pixels and using 1-bit color. Then just open the bitmap image in a standard image-editing program. All you need to know is the BMP file's format (I like making the width a multiple of 4 for simplicity reasons (32 for the 1-bit image)), and how to change the pixel colors accordingly. 24-bit is the easiest to work with.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    If you're writing your own (not using a library) method of outputting BMP files, I'd suggest going with TGA instead. They're much simpler to figure out. If you're looking for more file types:

    Hope is the first step on the road to disappointment.

  6. #6
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    And if you just want to visualize the data aquired by a C program, dump values in a file, and use gnuplot to do the plotting. You can even write a script file doing the plotting using C, and launch gnuplot with simple system("") call. (or fork and exec). re-inventing the wheel can be cool and teach you a lot, but if the point is to get results...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error help making no sense
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2007, 07:55 PM
  2. problem with plot a linear graph on the command line
    By dionys in forum C Programming
    Replies: 5
    Last Post: 04-17-2004, 07:01 AM
  3. plot a graph on the screen
    By dionys in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 02:46 PM
  4. get the data and plot the graph
    By lwong in forum Windows Programming
    Replies: 3
    Last Post: 01-01-2004, 09:00 AM
  5. Minimize crossing edges in undirected graph
    By Shiro in forum C Programming
    Replies: 0
    Last Post: 12-26-2001, 04:48 AM

Tags for this Thread