It depends how sophisticated you want to get. If it is a simple text based console program, you can make an array of strings. Each string contains let's say eighty characters, all spaces but for one asterisk. It will be your genius to place the lonely asterisk at the right position according to y = sin(x). Print out about 50 strings with a new line each, and you have the rudimentary look of a sine function.
All in all a good exercise in arrays.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Oops, if you want to go in the horizontal direction there might be more than one astrisk in the string! Fuzzy thinking on my part!
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Not too pretty but you are getting there!
Print out your x and y values.
I don't think y should go negative.
For troubleshooting purposes
add: cout << "y=" << y << " x=" << x << endl;
above: pos[y][x]= '#';
comment out: // cout << pos[i][j];
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
The code shouldn't be that complicated. My suggestion would be to make a program that runs the sine curve down the vertical axis and get that one to work. You have to work with the idea of an axis down the center of the screen, positive y to the right, negative y to the left.
Once that all works, then transpose it it to the horizontal axis.
Another approach would be to locate the cursor at point x,y and print the character # or * or whatever. I have attached some code for that written in Dev-C++. It uses the Windows API call SetConsoleCursorPosition().
[php]// plot a text based sine curve down the vertical center axis
// written in DevCpp v.4.9.9.0
#include
#include // covers windows API calls
using namespace std;
void LocateCursor(int col,int row);
HANDLE hConsole;
int main(int argc, char *argv[])
{
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
double x, y; // radians
int col, row; // screen loacations
for (x = -3.2; x < 6.4; x += 0.1) {
y = sin(x);
row = ((int) (x * 10)) + 32; // cast into positive screen location
col = ((int) (y * 30)) + 35; // center this one
LocateCursor(col, row);
}
cin.get(); // wait
return 0;
}
void LocateCursor(int col,int row)
{
COORD cursor;
cursor.X = col;
cursor.Y = row;
SetConsoleCursorPosition(hConsole,cursor);
cout << '*';
}
[/php]
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Looks like someone hijacked a thread and forgot to read the Rules
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944