using sin and cos

Anything that doesn't belong in the other forums.

using sin and cos

Postby tvz » Fri May 16, 2014 3:29 pm

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. :)
I have not left.
User avatar
tvz
 
Posts: 98
Joined: Sun Mar 10, 2013 7:13 am
Location: Your PC screen
Score: 3 Give a positive score

Re: using sin and cos

Postby skydereign » Sat May 17, 2014 3:44 pm

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
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: using sin and cos

Postby tvz » Mon May 19, 2014 8:47 am

1. I understood the first program but it just draws a straight white line horizontally on the screen. :(
and
2. I think i should first complete the unit on radian in my math book to understand circle drawing.

btw thank you :)
+1
I have not left.
User avatar
tvz
 
Posts: 98
Joined: Sun Mar 10, 2013 7:13 am
Location: Your PC screen
Score: 3 Give a positive score

Re: using sin and cos

Postby skydereign » Mon May 19, 2014 4:32 pm

tvz wrote:1. I understood the first program but it just draws a straight white line horizontally on the screen. :(

Ah, that's due to an error on my part. It should be this.
Code: Select all
int i;
setpen(255, 255, 255, 0, 2);
for(i=0; i<width; i++)
{
    float sin_value = sin((double)i/width*(2.0*PI));
    putpixel(i, height/2 + height/2.0*sin_value);
}

The reason was due to the floating point value being cut off of the sin_value. Since sin returns numbers between -1 and 1 it was being cast to 0.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to Other

Who is online

Users browsing this forum: No registered users and 1 guest