Lesson 19: Trigonometric Functions in VB2019

Master trigonometric functions for geometric calculations and angular operations

Key Takeaway

VB2019 provides comprehensive trigonometric functions that allow you to perform geometric calculations and solve problems involving angles and triangles.

In the previous lesson, we learned how to use various mathematical functions in Visual Basic 2019. In this lesson, we'll explore trigonometric functions that deal with angles and lengths of polygons. We'll cover the three basic trigonometric functions (Sin, Cos, Tan) and their inverse functions (Asin, Acos, Atan).

Important Note

Visual Basic trigonometric functions work with radians, not degrees. To convert between degrees and radians:

Radians = Degrees × π/180
Degrees = Radians × 180/π

Where π = 2 × Math.Asin(1)

19.1 Trigonometric Functions Overview

Visual Basic 2019 includes trigonometric functions that belong to the Math class. These functions are essential for geometric calculations, physics simulations, game development, and more.

Basic Functions

Sin, Cos, Tan for angle calculations

Inverse Functions

Asin, Acos, Atan for angle determination

Conversions

Convert between degrees and radians

Practical Applications

Solve real-world geometry problems

19.1(a) Trigonometric Function Reference

Here's a reference table for trigonometric functions in VB2019:

Function Syntax Description Example
Sin Math.Sin(angle) Returns the sine of an angle Math.Sin(π/2) = 1
Cos Math.Cos(angle) Returns the cosine of an angle Math.Cos(π) = -1
Tan Math.Tan(angle) Returns the tangent of an angle Math.Tan(π/4) ≈ 1
Asin Math.Asin(value) Returns the arcsine (in radians) Math.Asin(1) = π/2
Acos Math.Acos(value) Returns the arccosine (in radians) Math.Acos(0) = π/2
Atan Math.Atan(value) Returns the arctangent (in radians) Math.Atan(1) = π/4

19.2 Trigonometric Functions

Let's explore each trigonometric function in detail with practical examples:

Sin Function
Math.Sin(angleInRadians)

Returns the sine of the specified angle. The angle must be in radians.

Form1.vb
Private Sub BtnSin_Click(sender As Object, e As EventArgs) Handles BtnSin.Click
    Dim pi As Double = 2 * Math.Asin(1) ' π = 2 * Asin(1)
    Dim angleInRadians As Double = pi / 2 ' 90 degrees in radians
    
    Dim result As Double = Math.Sin(angleInRadians)
    LblResult.Text = "Sin(90°) = " & Math.Round(result, 4)
End Sub

Output:

> Click Run > Sin(90°) = 1.0
Cos Function
Math.Cos(angleInRadians)

Returns the cosine of the specified angle. The angle must be in radians.

Form1.vb
Private Sub BtnCos_Click(sender As Object, e As EventArgs) Handles BtnCos.Click
    Dim pi As Double = 2 * Math.Asin(1)
    Dim angleInRadians As Double = pi ' 180 degrees in radians
    
    Dim result As Double = Math.Cos(angleInRadians)
    LblResult.Text = "Cos(180°) = " & Math.Round(result, 4)
End Sub

Output:

> Click Run > Cos(180°) = -1.0
Tan Function
Math.Tan(angleInRadians)

Returns the tangent of the specified angle. The angle must be in radians.

Form1.vb
Private Sub BtnTan_Click(sender As Object, e As EventArgs) Handles BtnTan.Click
    Dim pi As Double = 2 * Math.Asin(1)
    Dim angleInRadians As Double = pi / 4 ' 45 degrees in radians
    
    Dim result As Double = Math.Tan(angleInRadians)
    LblResult.Text = "Tan(45°) = " & Math.Round(result, 4)
End Sub

Output:

> Click Run > Tan(45°) = 1.0
Asin Function
Math.Asin(value)

Returns the arcsine (in radians) of the specified value. The value must be between -1 and 1.

Form1.vb
Private Sub BtnAsin_Click(sender As Object, e As EventArgs) Handles BtnAsin.Click
    Dim pi As Double = 2 * Math.Asin(1)
    
    Dim radians As Double = Math.Asin(1) ' Asin(1) = π/2 radians
    Dim degrees As Double = radians * 180 / pi ' Convert to degrees
    
    LblResult.Text = "Asin(1) = " & Math.Round(degrees, 0) & "°"
End Sub

Output:

> Click Run > Asin(1) = 90°
Acos Function
Math.Acos(value)

Returns the arccosine (in radians) of the specified value. The value must be between -1 and 1.

Form1.vb
Private Sub BtnAcos_Click(sender As Object, e As EventArgs) Handles BtnAcos.Click
    Dim pi As Double = 2 * Math.Asin(1)
    
    Dim radians As Double = Math.Acos(0) ' Acos(0) = π/2 radians
    Dim degrees As Double = radians * 180 / pi ' Convert to degrees
    
    LblResult.Text = "Acos(0) = " & Math.Round(degrees, 0) & "°"
End Sub

Output:

> Click Run > Acos(0) = 90°
Atan Function
Math.Atan(value)

Returns the arctangent (in radians) of the specified value.

Form1.vb
Private Sub BtnAtan_Click(sender As Object, e As EventArgs) Handles BtnAtan.Click
    Dim pi As Double = 2 * Math.Asin(1)
    
    Dim radians As Double = Math.Atan(1) ' Atan(1) = π/4 radians
    Dim degrees As Double = radians * 180 / pi ' Convert to degrees
    
    LblResult.Text = "Atan(1) = " & Math.Round(degrees, 0) & "°"
End Sub

Output:

> Click Run > Atan(1) = 45°
Sine Rule Application

Trigonometric functions can solve real-world problems like the sine rule for triangles. Given angle A = 60°, angle B = 40°, and side a = 4, we can calculate side b:

b = (a × sin(B)) / sin(A)

Form1.vb
Private Sub BtnSineRule_Click(sender As Object, e As EventArgs) Handles BtnSineRule.Click
    Dim pi As Double = 2 * Math.Asin(1)
    
    ' Convert degrees to radians
    Dim A As Double = 60 * pi / 180
    Dim B As Double = 40 * pi / 180
    Dim a As Double = 4 ' Side opposite angle A
    
    ' Calculate side b (opposite angle B)
    Dim b As Double = (a * Math.Sin(B)) / Math.Sin(A)
    
    LblResult.Text = "Side b = " & Math.Round(b, 2)
End Sub

Output:

> Click Run > Side b = 3.06

Lesson Summary

In this lesson, you've learned how to leverage VB2019's trigonometric functions to perform geometric calculations:

Basic Functions

Mastered Sin, Cos, and Tan for angle calculations

Inverse Functions

Learned to use Asin, Acos, and Atan for angle determination

Radians Conversion

Understood how to convert between degrees and radians

Practical Applications

Applied trigonometric functions to solve real-world geometry problems

These trigonometric functions are essential for applications involving geometry, physics, game development, and graphics programming. In the next lesson, we'll explore the Format function for customizing data presentation.

Next Lesson

Ready to learn about formatting data? Continue to Lesson 20: Format Function.

Related Resources

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon