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:
Returns the sine of the specified angle. The angle must be in radians.
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:
Returns the cosine of the specified angle. The angle must be in radians.
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:
Returns the tangent of the specified angle. The angle must be in radians.
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:
Returns the arcsine (in radians) of the specified value. The value must be between -1 and 1.
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:
Returns the arccosine (in radians) of the specified value. The value must be between -1 and 1.
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:
Returns the arctangent (in radians) of the specified value.
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:
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)
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:
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
Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.
Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.