how do you make a graph?

I have to write a program that will plot a sine signal graph where y = 1 unit and x = 20 ms

i have no idea how to even begin this program because i have never done graphs before so if anyone could help i'll really appreciate it

thanks

Last edited on
Hi

Your question is a little bit too vague to answer as it stands. For example, are you working with a particular GUI framework/API? Or are you wanting to write a console application?

Andy
Last edited on
sorry its a console application in visual studios
OK, for a console application, I suggest you split the problem up.

- first calculate the required data
- plot it

How may chars can you fit across the console?

=> Use this to work out the increment (of x) which gets you from your starting value to your ending value in that many increments.

How many rows of chars can you display on your console?

=> That will determine your vertical resolution.

Now you know how many value you need, you can populate an array (calculate it).

Then, as you plot/draw each stripe, you can test to see if the y value is within the range for a box. If so, draw a char of some kind. Otherwise a blank.

You will also need to think about how to draw the axis.

Andy
so your basically just saying i need to use a load of for loops and cout statements to draw the graph?
Yep!

You did say you wanted to just use the console?
Last edited on
ya i just want a simple way to make a graph

thanks for the help
thanks for the help you gave but i was just wondering is there another way to make a graph?
I've googled around a bit and have seem bits of code and heard of a library called graphs.

Is there some simple code to made a curved line graph?

heres a link of the type of graph i want:
http://www.google.ie/imgres?q=sine+wave+graph&hl=en&biw=1360&bih=587&gbv=2&tbm=isch&tbnid=jrfn6zklvfPsiM:&imgrefurl=http://www.atpm.com/6.02/digitalaudio.shtml&docid=jRo44_Zcgy7BiM&imgurl=http://www.atpm.com/6.02/images/sine.gif&w=306&h=213&ei=WkWwTqXnOommhAeDhoS_Ag&zoom=1&iact=hc&vpx=174&vpy=97&dur=40&hovh=170&hovw=244&tx=131&ty=120&sig=101615137201103890365&page=3&tbnh=110&tbnw=156&start=42&ndsp=21&ved=1t:429,r:14,s:42

thanks
If you're using Microsoft Visual C++ Professional (or better), you'll have the MFC classes. They're very easy for drawing. I'm not sure if they can handle mathematical functions as input, but I imagine so.
Have you been any guidance about what libraries to use? I have no idea of what you already know, or what's expected of you.

It's not esp. difficult to draw a graph using WIN32 or MFC, or another GIU toolkit, but if you've never coded a GUI app before you've got a little bit of learning to do before it's easy.
I just started GUI so i know the basics but i have never done graphs before or been told how to make them
When you say you're just started GUI, do you mean WIN32, WM_PAINT, DCs, and the like?

If so, you could do your own graph easily enough.
ya WIN32 and all that
how would a draw a graph if i used those?
In WM_PAINT

* CreatePen/ExtCreatePen to create a pen to draw with

* SelectObject to use the pen, remembering the old pen

* MoveToEx, LineTo to draw the lines, inc. the axes (if the segments are small enough, the line will look smooth; but not too small!)

* Select the old pen

* DeleteObject to destroy your pen

See MSDN for further info. If you find the entry for (e.g.) MoveToEx, you have found the GDI section in MSDN. Then also see DrawText/TextOut/etc.

Topic archived. No new replies allowed.