Thanks for this, didn't notice the post till after I completed my result. I'll try it out thanks.
Edit: I tried it, problem with it is that it displays the canvas twice, i think it's preferred to be just displayed once.
So this is a sine-wave plotted against time?
Since waves will have an amplitude (maximum height) and a frequency (number of cycles per second). amplitude and frequency will be constants. y will be a function of time, amplitude, and frequency, using time as the variable. Or you can create a variable theta based on time and frequency and derive y based on theta and amplitude. Or you can calcuate y using theta and the fact that cosine squared plus sine squared have to equal 1, then multiply by amplitude. There are a variety of ways to do the math. Figure out the values of y based on t and stick them in an array.
const int AMPLITUDE = 4;
const double FREQUENCY = 1.0 / 12.0; // 1 cycle every twelve seconds
const int TOTAL_TIME = 60; // seconds
int y[TOTAL_TIME+1]; // rounded y values
for (int t = 0; t <= TOTAL_TIME; t++)
{
// calculate theta based on t and FREQUENCY
// calculate/round off y[t] using trigonometry based on AMPLITUDE and theta.
}
// now do the plotting
for (int t = 0; t <= TOTAL_TIME; t++)
{
for (int i = AMPLITUDE; i >= -AMPLITUDE; i--)
{
// compare y[t] to i. If equal, display '*'. If not, display '.'
}
// display newline
}
This is one way to make a fairly generic graph. Simply change the constants and only the constants when you want a different graph. or if you have a different equation, change the y[] calculation part.
The display part is pretty much always the same.
[EDIT]
My display is actually backwards. The outer loop should range from AMPLITUDE to -AMPLITUDE and the inner loop should span across time since time is horizontal and the inner loop is always the horizontal element. My bad.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Thanks, although its not against time, just merely plotting a math function.
Sorry, forgot to mention that it was a math function. The double t is actually another variable to match the ratio of the scale to the array column. That is the max horizontal axis value/max scale value -> 75/15 = 5, so 1 value on the scale is equivalent to about 5 columns in the two dimensional array. therefore, 1 column = 0.2 = t.
OK. Same concept though. One value of y for every x. x is calculated on a range. Set up an array of values the size of your x range. Calculate y for each x and store it in that array. Again, my plot code was backwards regarding the inner/outer loop. You'll have some maximum and minimum x and maximum and minimum y and you'll have a nested loop to display, using those minimums and maximums as the loop ranges. You can either use a 2-dimensional array as you did originally or you can use a 1-dimensional as I did and do the comparisons to decide which character to print.
As far as the scaling goes, that's when your maximum and minimum values come in:
for (int y = Y_MAX; y >= Y_MIN; y--)
{
cout << y << " "; // y scale
for (int x = X_MIN; x <= X_MAX; x++)
{
// display a character
}
}
// display x scale here using loop. Parameters are X_MIN to X_MAX.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
The array must be 21, 76. the extra 1 in the row and column are for the scales.
Here's my attempt:
for (j = 21; j > -1; j--){
if (j == 20){
i = 0;
plot[j][0] = '4';
cout << plot[j][i];
}
else if (j == 15){
i = 0;
plot[15][0] = '3';
cout << plot[j][i];
}
else if (j == 10){
i = 0;
plot[j][i] = '2';
cout << plot[j][i];
}
else if (j == 5){
i = 0;
plot[j][i] = '1';
cout << plot[j][i];
}
else if (j == 0){
i = 0;
plot[j][i] = '0';
cout << plot[j][i];
}
else
{
i = 0;
plot[j][i] = ' ';
}
}
Problem with it is that I get unwanted symbols at the top. http://i109.photobucket.com/albums/n79/johnz13/scalewithgraph.jpg
In the picture I placed the loop after it assigns the '.' and before it calculates the function.
Also lost on whereabouts to put the loop i.e before the plotting the function, or after, or anywhere else?
Note: I am using the layout suggested by tony.
I edited it so that it displays once, wasn't thinking last night as i was tired.
This code looks like it has nothing to do with the top. The top scaling all has to happen before you get here, so this gives no clue to the problem. According to the picture, this part works fine, so it looks like you are posting the part that works, not the part that doesn't work. Could be wrong, but post the whole program each time. It's short enough. That way we don't have to go back to the earlier posts to see the missing code.
[EDIT]
Are you trying to put the horizontal scale at the bottom or the top?
[/EDIT]
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711