Click to See Complete Forum and Search --> : drawing a sin() function


yaniv
August 5th, 2005, 09:25 AM
hi,

i need to draw a sin() fonction that move in the client area ( like the preformance of the CPU in the os' )

the grafh[the sin() func'] must have the ability to chang the frequency
and the amplitude.

h e l p ! ! !

thanks

thre3dee
August 6th, 2005, 10:33 PM
well the equation for a sin wave is

y = a * cos/sin (bx) + c

a = amplitude
b = wave length
c = vertical shift

so for a 20 pixel high sin wave on a 640 x 480 screen you would calculate the y value like the following (all depends on how you will draw the line)

for (int x= -1; ++x< 640;)
{
int y = 20 * sin/cos (x) + 240;
// Do stuff
}

yaniv
August 7th, 2005, 05:14 AM
thanks for your reply,

this is general algorithm ( i also have it ).
for exemple if you enter =>
sin(90 ) you dont recive 1. // regular
sin(Pi) you dont recive 1. // radian

but
sin(1.5705) = 1
1. why ?
2. how can i make the grafh move
on the screen?


thanks for your help
y.

yaniv
August 7th, 2005, 05:52 AM
i find it ( MFC)

void CMdiSin1View::OnDraw(CDC* pDC)
{
CMdiSin1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

//------------- Attributes-------------

int Xaxis ;
int Amplitude = 1 * 15 ; // *15 to increase the high of the wave
double cyclicality = 1 * 0.01; // *0.01 to increase the lengh of the wave
const double M_PI = 3.14159265358979323846 ;
//-------------------------------------
CClientDC dc(this); // device context for painting
CRect rect;
GetClientRect (&rect);

CPoint Centerpoint = rect.CenterPoint ();
dc.MoveTo (0 , Centerpoint.y );

for (Xaxis =-1 ; ++Xaxis < rect.right ; )
{
dc.LineTo ( Xaxis ,Centerpoint.y + Amplitude * sin((M_PI*(cyclicality*Xaxis - VerticallyShifted ))));
dc.MoveTo ( Xaxis ,Centerpoint.y + Amplitude * sin((M_PI*(cyclicality*Xaxis - VerticallyShifted ))));

}
}

yaniv
August 7th, 2005, 05:54 AM
but still,

how can i make it move on the client area ???

thans

y.

thre3dee
August 9th, 2005, 04:08 AM
but still,

how can i make it move on the client area ???

thans

y.
the loop for shifting it would be something like this

int shift = #; // amount shifted to left/right
for (int x = -1; ++x < 640;)
{
int y = a * cos/sin (b * (x + shift)) + c;
// Do some stuff
}