Mathematical function represented at xy axis

Hello!

I trying to do a small program to display a fucntion graphically.
for example:
y=x^2
to:
a xy axis represented by dots(.) and the function lines represented with (#)

It will be very helpfull if you give me some guidelines or steps that i must follow! Thanks!
well, if you want to hard code a function, then just write double f(double x){ return x*x; }
and for(double x = x_min; x < x_max; x += step) draw_line(x, f(x), x+step, f(x+step));(which can be optimized so that you don't calculate every f(x) twice)

if you want to output it into console, create a 2d array and draw on it. then print it. though http://www.cplusplus.com/forum/articles/28558/ (even though this is not a game..)

the difficulty arises when you want to parse the function from user input. that itself isn't too complicated, but what do you do when user enters 1/x ? with my code you'd get http://i56.tinypic.com/fwqdsm.png which is obviously not right. I have no idea how to prevent that without constructing a mechanism that can solve any equation.
Hello hamsterman! Thanks for the reply.

Basically i implemented this code. I want to have constants with values for x and y.

More specifically if i change the Xmin constant to -5 and Xmax to 5, Ymin to -3 and Ymax to 7 i want to get the following result.

http://hotmath.com/images/gt/lessons/genericalg1/parabola.gif

after i will put * for the parabola

My code until now (79x24 my screen dimensions)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	int yaxis,xaxis;

	for (int xaxis=0;xaxis<24;xaxis++)
	{
		for (yaxis=0;yaxis<79;yaxis++)
			if (xaxis == 21)
				cout<<"-";
			else
			if (yaxis!=40)
				cout<<" ";
			else
			if (yaxis==40)
				cout<<"|";
			else
				cout<<" ";
		cout<<endl;
	}
Line 10 is not needed. Line 15 would do that.

It would be easier to, as I suggested, create a 2d array of chars and draw onto it. Then print it.

If you want to make your code (sort of) work, add if( (yaxis-40)*(yaxis-40) == xaxis-21) cout << "*"; else ...
Topic archived. No new replies allowed.