Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 [tutorial] sine, cosine and tangent
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message

PostPosted: Sat Mar 06, 2004 12:16 am   Post subject: [tutorial] sine, cosine and tangent

well here's the first one of a series of sine/cosine tutorials (i hope)... all the information is commented infront of the program...

%circle proggy:
%use sine and cosine to generate circles
%sine of a number will always be between 1 and -1 and if you graph it you will
%get a "sine wave". cosine is similar to sine in that when graphed it produces
%a wave but the trough of the wave is half a wave length to the right. this
%relationship can be used to generate circles...
%to generate a circle, we need to generate x values by using sine
%and y values using cosine. the numbers that we find the sine/cosine of are
%0 - 360 (1 for each angle in a circle) this is because in one full sine wave,
%there are are 360 degrees. the thing is, these angles need to
%be expressed in radians, so to convert degrees into radians we multiply the
%angle by PI/180. so our formula would be x = sin(ang*(PI/180)). now to make
%things easier, we could use the predefined functoin cosd() which automaticaly
%converts the angle into radians. since the result of the sine/cosine of a
%number is number between -1 and 1, this means that we can use this number as
%a percent. therefore we multiply our x by the desired diameter and we get the
%coordinate for our circle...

procedure drawCircle (centerx, centery, diam, c : int)
    var x, y, oldx, oldy : real
    oldx := cos (0) * diam + centerx
    oldy := sin (0) * diam + centery
    for i : 1 .. 360
        %in radians:
        x := cos (i * (Math.PI / 180)) * diam + centerx
        %using sind() to automatically convert to radians:
        y := sind (i) * diam + centery
        drawline (round (oldx), round (oldy), round (x), round (y), c)
        oldx := x
        oldy := y
    end for
end drawCircle

drawCircle (maxx div 2, maxy div 2, 100, 7)

if you have some questions please ask away (i'm bad at explaining things)


PostPosted: Sat Mar 06, 2004 1:04 am   Post subject: (No subject)

here's a usefull application for tangent.

%angle finder:
%this program finds the angle of one point relative to another point. in this
%example, the dynamoc point will be the mouse and the angle will be measured
%relative to the center of the screen. to find an angle in a right triangle,
%you have to find the length of the opposite and adjacent sides of the triangle
%(base and height). then you calculate the inverse of tan(ratio of opposite and
%adjacent) [angle = tan**-1(height/base)]. to make things simple we will use the
%predefined function arctand(). so the formula will be angle = arctand(height/width).
%NOTE: this method only works finding angles between 0 - 90. to explain this
%lets consider the following triangles:
%            p1   p2
%            /|   |\
%           / |   | \
%       a  /__|   |__\  b
%notice that angle a and b are equal. but in reality a < 90 and b > 90... to
%solve this, we check wheter the base is less than 0. if it is then the angle
%is equal to 180 - b. the same applies when the triangle is upside down. if it
%is then the angle is equal to 360 - angle

function whatAngle (x, y : int) : int
    var mx, my, md : int
    var ratio, angle : real
    mousewhere (mx, my, md)
    %find the base of the triangle
    mx -= x
    %find the height of the triangle
    my -= y
    %since you can't devide a number by 0 (will be undefined), then you devide
    %the number by something very close to 0
    if mx not= 0 then
        ratio := my / mx
        ratio := my / 0.001
    end if
    %find the angle
    angle := arctand (abs (ratio))
    %correct then angle
    if mx < 0 then
        angle := 180 - angle
    end if
    if my < 0 then
        angle := 360 - angle
    end if
    result round (angle)
end whatAngle

drawoval (maxx div 2, maxy div 2, 3, 3, 7)
    locate (1, 1)
    put whatAngle (maxx div 2, maxy div 2)
end loop


PostPosted: Sat Mar 06, 2004 11:51 am   Post subject: (No subject)

I think it would be easier to understand, if you broke it down line by like or a couple lines at a time, not just a huge code block with comments in it. make it like



etc. Smile (VERY confusing.)
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to: