Drawing a sine wave into a 2d Array

Hi

I'm having trouble drawing a sinewave horizontally across the screen into an array[10][100]. The wave must be drawn using the '*' character.

I can initialize the array all to spaces but I can't figure out how to put the sinewave into it. I've been looking on the net and have been able to draw sinewaves that travel vertically down the page using this code:

array[49 + (int) (49 * sin(M_PI * (float) c / 10))] = '*';
printf("%s\n", array);
array[49 + (int) (49 * sin(M_PI * (float) c / 10))] = ' ';

I have it in a for loop and set it to 20 lines so it goes through one period. Unfortuantely I can't get it into my array of 10X100. My maths is crap so I can't figure out what calculation I need to create a sinewave horizontally into the array.

I want it to look something like this at run time, (without the zeros, I had to put them in otherwise the post ignored the spaces I put in and bunched all the '*' up together!).

0000000000*000000000000000000000000000000000
000000*0000000*00000000000000000000000000000
000*0000000000000*00000000000000000000000000
*000000000000000000*00000000000000000000000*
000000000000000000000*0000000000000000000*00
00000000000000000000000*000000000000000*0000
0000000000000000000000000*00000000000*000000
0000000000000000000000000000*000000*00000000
00000000000000000000000000000000*00000000000

I'd also like to be able to manipulate the equation so that I can fit 2 cycles into the same space, then 3 and 4 and 5. That I think I can figure out, but any help on how to fit one sinewave period into my array would be most helpful.

Thank you for your time and help.
Mike
[1885 byte] By [TheBurningFretboard] at [2007-11-20 11:43:21]
# 1 Re: Drawing a sine wave into a 2d Array
Just very brief:

Iterate along the second dimension of your array, i.e. from 0 to 99
Calculate X by mapping the range [0,100) to the range of the sine you want to display, e.g. [0,2*pi)
Calculate Y as sin(X) using the function from math.h
Calculate which array field you should fill with '*' by mapping the sine value from [-1,1] to the first dimension of your array [0,9]
Following the above, you should set exactly 100 array elements to '*'. Now just print out the array.

EDIT: Are you sure the picture above is what you want to have? It does not have a * in every column and will be much more difficult to implement!
treuss at 2007-11-9 1:26:07 >
# 2 Re: Drawing a sine wave into a 2d Array
a '*' in every column would be cool.

I'll have a go at implementing what you advised.

Thank you for your help
TheBurningFretboard at 2007-11-9 1:27:01 >
# 3 Re: Drawing a sine wave into a 2d Array
Oh man, this is a real headf*$&!

I'm really new to c++(4 weeks) and I kind of understand what you're saying, but I have no idea where to start. I don't really understand how to map * to the Y axis giving me 100 array elements to *.

I don't want it to appear that I'm trying to get you to do my work but a short example of what the for loop assigning the * char would be really helpful.

Cheers
Mike
TheBurningFretboard at 2007-11-9 1:28:08 >
# 4 Re: Drawing a sine wave into a 2d Array
OK, maybe the following code can guide you:const unsigned int MAXCOL = 100;
const unsigned int MAXROW = 10;

char arr[MAXCOL][MAXROW];

// Set all array elements to ' '

for ( unsigned int col = 0; col < MAXCOL; ++col ) {
float x = // Set x based on COL in range [0,2pi)
float y = sin(x);
unsigned int row = // Set row based on y in range [0,MAXROW)
arr[col][row] = '*';
}

// print out array row by row, column by column (at first)
treuss at 2007-11-9 1:29:07 >
# 5 Re: Drawing a sine wave into a 2d Array
Thats really helpful, thank you.

I understand what the for loop is doing and the concept of printing out row by row and column by column is fine.

What I'm really confused by is the setting x to col in the range [0,2pi), same for setting the variable row to y in the range [0,MAXROW).
Why are the '[' and ')' around the range? I also don't really get the range concept.

I'm sorry for asking more questions, I can see what we're trying to achieve but the maths for calculating a sinewave is what I'm confused about. Thank you for the help you have given me already, it's starting to make sense.

Mike
TheBurningFretboard at 2007-11-9 1:30:04 >
# 6 Re: Drawing a sine wave into a 2d Array
Oh man, this is a real headf*$&!

I'm really new to c++(4 weeks) and I kind of understand what you're saying, but I have no idea where to start. I don't really understand how to map * to the Y axis giving me 100 array elements to *.

I don't want it to appear that I'm trying to get you to do my work but a short example of what the for loop assigning the * char would be really helpful.

Cheers
Mike

you need one period, so you are setting the y range from 0 to 2pi.

and you want that to be represented by 100 values. so all you do is divide 2pi by 100 and multiply by the index which will be your array column.

if it was me i would make the array 11 x 101. (i.e. char Array[11][101];)

then for the x direction you get:
A[0][y] ~ sin[pi/2] = 1
A[1][y] ~
A[2][y] ~
A[3][y] ~
A[4][y] ~
A[5][y] ~ sin[0] = 0
A[6][y] ~
A[7][y] ~
A[8][y] ~
A[9][y] ~
A[10][y] ~ sin[3pi/2] = -1

and for the y direction you get:
A[x][0] ~ 0
and
A[x][100] ~ 2pi

then all you have to do is initialise the array to ' ' and put one '*' in each column in the row that represents the value of sin[y] for the mapped y value of that column...

i.e. for the column where Y represents pi (this would be A[x][50] as above) you need to put a * in the A[5][y] row since this is equal to zero...

something like:

char Array[11][101];

for(int x=0; i<11; x++)
for(int y=0; i<101; y++)
Array[x][y] = ' ';

for(int i=0; i<101; i++) //loop through all columns
{
int X = 5 - (5 * sin(i*2*pi/100) );

Array[X][i] = '*';
}

to get this line:
int X = 5 - (5 * sin(i*2*pi/100) );

mapped y-value = i*2*pi/100
required x-value = sin(i*2*pi/100)
mapped x-value = 5 - (5 * required_value )
in order to produce a number in the range 0-10 as before...

i hope that makes sense :S

edit: i missed the 2*pi out before, doh!
LoKi_79 at 2007-11-9 1:31:14 >
# 7 Re: Drawing a sine wave into a 2d Array
You have to calculate what the x value should be based on the range of the sine wave you want to display and the amount of collumbs you have to display it in. If you want the wave to show [0,2pi), and you want to divide it into 10 collumbs, each x would be (2pi-0)/10 apart. Plug the variables into that equation and it shouild work. Also for choosing what row it should be in, you have to change the float value sin returns (range of [-1,1] ), to an int value for the array ( range values of [0,MAXROW) ).

The '[' and ')' around [0,MAXROW) is how you define a range, and '[' means inclusive, and ')' means exclusive.
bovinedragon at 2007-11-9 1:32:09 >
# 8 Re: Drawing a sine wave into a 2d Array
Cheers for all your help guys. I'll have a go and hopefully get this **** code to do what I want it to.

Mike
TheBurningFretboard at 2007-11-9 1:33:15 >
# 9 Re: Drawing a sine wave into a 2d Array
Hey guys

Thanks for all your help, it all clicked into place eventually. Went through the code with my tutor and it all works great. Here is a version of my finished code, drawing one cycle of the sine wave into the array. Hope this helps any other confused students out there. Thanks again!

#include <iostream>
#include<math.h>
#define MAXCOL 101
#define MAXROW 11
using namespace std;

void setSine(char[MAXCOL][MAXROW], int);

int main()
{
char array[MAXCOL][MAXROW];
int cycles = 1;
setSine(array, cycles);

for(int a=0;a<MAXROW;a++)
{

for(int b=0;b<MAXCOL;b++)
cout<<array[b][a];
cout<<endl;
}

return 0;
}

void setSine(char array[MAXCOL][MAXROW], int cycles)
{
for(int a=0;a<MAXCOL;a++)
{

for(int b=0;b<MAXROW;b++)
array[a][b] = ' ';

}

for(int i=0; i<MAXCOL;i++)
{
int X = 5 - (5.0 * sin(i*2*cycles*M_PI/MAXCOL));
array[i][X] = '*';
}

}

EDIT: thank you Bovine!
TheBurningFretboard at 2007-11-9 1:34:13 >
# 10 Re: Drawing a sine wave into a 2d Array
I don't know how to put it into a nice code box

http://www.dev-archive.com/forum/misc.php?do=bbcode

Put "[code]" around the code
bovinedragon at 2007-11-9 1:35:14 >