tvz wrote:Hello everyone
Can someone tell me how do you use sin and cos? How can i use these on canvas to draw objects?
A small tutorial can help.
sin/cos are just two trig functions, so really they are just math. It so happens that sin/cos is very useful for shapes, such as circles and triangles. It's hard to teach one how to draw objects on canvas using them in that all really you just copy math functions. So I can tell you how to draw a circle for instance, but to use them to draw things you'd have to know the math behind the functions and what you personally want to do with it. Here's an example of drawing a sin wave.
- Code: Select all
int i;
setpen(255, 255, 255, 0, 2);
for(i=0; i<width; i++)
{
int sin_value = sin((double)i/width*(2*PI));
putpixel(i, height/2 + height/2.0*sin_value);
}
And here's a circle.
- Code: Select all
int i;
double radius = width/2;
setpen(255, 255, 255, 0, 2);
for(i=0; i<360; i++) // loops through angles 0-359 degrees
{
double rad = degtorad(i); // convert i to radians
int xp = radius*cos(rad); // get the x value of the circle at this angle
int yp = radius*sin(rad); // get the y value of the circle at this angle
putpixel(width/2 + xp, height/2 + yp); // place it in the center of the canvas
}