Computer Science Canada

Trig Graphs+Angles

Author:  MichaelM [ Fri Feb 01, 2008 3:03 pm ]
Post subject:  Trig Graphs+Angles

This is a program that finds trig values ( ex. sin(60) ) manually by searching a graph. It may give you a better understanding of what the trig functions do. Plus it looks cool Very Happy

code:

setscreen ("graphics:1000;800")
var y : real := 0
var choice, a : int
loop
    y := 0
    put "Enter the Trig Function"
    put "1 - Sin  |  4 - Csc"
    put "2 - Cos  |  5 - Sec"
    put "3 - Tan  |  6 - Cot"
    put "      7 - Quit"
    loop
        get choice
        exit when choice > 0 and choice <=7
        put "That is not a valid choice"
    end loop
    exit when choice=7
    put "Enter the Angle"
    get a   
    cls
    if a > 0 then
        a := a mod 360
    elsif a < 0 then
        a := (a mod 360) - 360
    end if
    drawline (maxx div 2, 0 + 300, maxx div 2, maxy - 300, grey)
    drawline (0 + 100, maxy div 2, maxx - 100, maxy div 2, grey)
    for x : -360 .. 360
        if choice = 1 then
            y := sind (x)
        elsif choice = 2 then
            y := cosd (x)
        elsif choice = 3 and x not= 90 and x not= 270 and x not= -90 and x not= -270 then
            y := sind (x) / cosd (x)
        elsif choice = 4 and x mod 180 not= 0 then
            y := 1 / sind (x)
        elsif choice = 5 and x not= 90 and x not= 270 and x not= -90 and x not= -270 then
            y := 1 / cosd (x)
        elsif choice = 6 and x mod 180 not= 0 then
            y := cosd (x) / sind (x)
        end if
        drawdot (x + (maxx div 2), round (y * 50) + (maxy div 2), black)
    end for
    var d : int := 0
    var ans : real := -1
    loop
        if whatdotcolour (a + (maxx div 2), (maxy div 2) + d) = black then
            ans := (maxy div 2) + d
            drawoval (a + (maxx div 2), (maxy div 2) + d, 3, 3, blue)           
            exit
        elsif whatdotcolour (a + (maxx div 2), (maxy div 2) - d) = black then
            ans := (maxy div 2) - d
            drawoval (a + (maxx div 2), (maxy div 2) - d, 3, 3, blue)           
            exit
        end if
        if (maxy div 2) + d > maxy or (maxy div 2) - d < 0 then
            put "OOPS, YOU CHOSE AN ANGLE THAT FALLS ON AN ASYMPTOTE!"
            put "THE CHOSEN ANGLE MAY BE OFF THE SCREEN"
            exit
        end if
        drawdot (a + (maxx div 2), (maxy div 2) + d, 40)
        drawdot (a + (maxx div 2), (maxy div 2) - d, 40)
        delay (15)
        d := d + 1
    end loop
    if choice = 1 then
        put "Sin(", a, ") = " ..
    elsif choice = 2 then
        put "Cos(", a, ") = " ..
    elsif choice = 3 then
        put "Tan(", a, ") = " ..
    elsif choice = 4 then
        put "Csc(", a, ") = " ..
    elsif choice = 5 then
        put "Sec(", a, ") = " ..
    else
        put "Cot(", a, ") = " ..
    end if
    if ans >= 0 then
        put (ans - (maxy div 2)) / 50
    else
        put "Undefined"
    end if       
end loop

Author:  fishtastic [ Sun Feb 03, 2008 3:28 pm ]
Post subject:  RE:Trig Graphs+Angles

cool, it covers all 6 trig ratios and it doesn't crash when you input an angle that will be undefined. great way of using whatdotcolor.

Since i was making the shortest tetris, whenever i look at your code, i only feel like making it shorter. Very Happy


: