Blitz3D+ Command Reference

Tan# ( degrees# )

Parameters

degrees - an angle, measured in degrees

Description

Returns the tangent of an angle given in degrees.

Tangent is simply Sin divided by Cos, which makes it the "slope" of an angle: how far something rises for each step forward. Tan(45) is 1, a perfect diagonal.

In game code it shows up most in camera and projection maths - working out how wide the view is at a given distance from a field-of-view angle, or how tall a wall should be drawn in a raycaster.

The gotcha is that tangent shoots off to infinity at 90 and 270 degrees, where the cosine underneath it hits zero. Near those angles the result becomes huge and unreliable, so never build a value you divide by out of a raw Tan without checking the angle first. Tan repeats every 180 degrees, and accuracy falls away for very large angles.

See also: Sin, Cos, ATan, ATan2, Pi.

Example

; Tan Example
; -----------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; Blitz3D trig works in DEGREES. Tan(angle)=Sin(angle)/Cos(angle) is
; the SLOPE of the rotating arm. Where the arm's line crosses a wall at
; distance r, the crossing height is r*Tan(angle) - it shoots off to
; infinity as the angle nears 90 or 270.

cx=240
cy=240
r=120

angle#=20

While Not KeyDown(1)

    ; Left/Right arrows steer the angle; it also drifts on its own
    If KeyDown(203) Then angle=angle-1
    If KeyDown(205) Then angle=angle+1
    angle=angle+0.2
    If angle>=360 Then angle=angle-360
    If angle<0 Then angle=angle+360

    ; The documented command: the slope for the current angle
    t#=Tan(angle)

    Cls

    ; The circle and a blue "wall" at distance r to the right
    Color 70,70,70
    Oval cx-r,cy-r,r*2,r*2,False
    Color 0,150,255
    Line cx+r,0,cx+r,479

    ; The rotating arm
    Color 255,255,0
    Line cx,cy,cx+r*Cos(angle),cy+r*Sin(angle)

    ; Red mark on the wall: the arm's line crosses it r*Tan(angle)
    ; below the centre height (skipped when Tan is huge near 90/270)
    If Abs(t)<10 Then
        Color 255,0,0
        Line cx+r,cy,cx+r,cy+r*t
        Oval cx+r-4,cy+r*t-4,8,8,True
    EndIf

    Color 255,255,255
    Text 0,0,"Left/Right: change angle   Esc: exit"
    Text 0,20,"angle = "+angle
    Text 0,40,"Tan(angle) = "+t

    Flip

Wend

End

Index