Trigonometric Functions
Master VB 2026's trig functions — Sin, Cos, Tan and their inverses, radian↔degree conversion, the unit circle, and real-world applications in graphics, navigation, physics, and wave generation.
Math.Sin, Math.Cos, Math.Tan) work in radians, not degrees. To convert: radians = degrees * Math.PI / 180 and degrees = radians * 180 / Math.PI. The inverse functions (Math.Asin, Math.Acos, Math.Atan, Math.Atan2) also return radians. Math.Atan2(y, x) is preferred over Math.Atan(y/x) because it handles all four quadrants and avoids division-by-zero when x=0.
19.1 Radians vs Degrees — The Critical Difference
A full circle is 360° or 2π radians. VB 2026's trig functions always expect radians. Forgetting to convert is the single most common trig bug in student code.
' Conversion helpers -- define once, use everywhere Private Function ToRad(degrees As Double) As Double Return degrees * Math.PI / 180 End Function Private Function ToDeg(radians As Double) As Double Return radians * 180 / Math.PI End Function ' Key radian values ToRad(0) ' 0 radians (0°) ToRad(30) ' PI/6 radians (30°) ToRad(45) ' PI/4 radians (45°) ToRad(60) ' PI/3 radians (60°) ToRad(90) ' PI/2 radians (90°) ToRad(180) ' PI radians (180°) ToRad(360) ' 2*PI radians (360°) ' Common trig values (exact) Math.Sin(ToRad(0)) ' 0.0 Math.Sin(ToRad(30)) ' 0.5 Math.Sin(ToRad(45)) ' 0.7071... (√2/2) Math.Sin(ToRad(90)) ' 1.0 Math.Cos(ToRad(0)) ' 1.0 Math.Cos(ToRad(60)) ' 0.5 Math.Cos(ToRad(90)) ' ≈ 0 (floating-point near-zero) Math.Tan(ToRad(45)) ' 1.0 ' WRONG -- passing degrees directly without conversion Math.Sin(90) ' 0.8939... NOT 1.0 (treating 90 as 90 radians!)
Passing degrees directly to Math.Sin, Math.Cos, or Math.Tan produces wrong results silently — no exception is thrown. Always convert with degrees * Math.PI / 180 before calling a trig function, or define a ToRad helper at the top of your form and use it everywhere.
19.2 Sin, Cos, and Tan
| Angle | Math.Sin | Math.Cos | Math.Tan | Notes |
|---|---|---|---|---|
| 0° | 0 | 1 | 0 | Start of unit circle |
| 30° | 0.5 | 0.866 | 0.577 | sin = 1/2 |
| 45° | 0.707 | 0.707 | 1 | sin = cos = √2/2 |
| 60° | 0.866 | 0.5 | 1.732 | cos = 1/2 |
| 90° | 1 | ≈0 | ±∞ | Tan undefined at 90° |
| 180° | ≈0 | -1 | ≈0 | Half circle |
| 270° | -1 | ≈0 | ±∞ | Tan undefined at 270° |
| 360° | ≈0 | 1 | ≈0 | Full circle = 0° |
' Right-triangle side calculator ' Given hypotenuse and angle (degrees), find opposite and adjacent sides Private Sub CalcTriangle(hyp As Double, angleDeg As Double) Dim rad = ToRad(angleDeg) Dim opposite = Math.Round(hyp * Math.Sin(rad), 4) Dim adjacent = Math.Round(hyp * Math.Cos(rad), 4) Dim tangent = Math.Round(Math.Tan(rad), 4) lblOpposite.Text = opposite.ToString() lblAdjacent.Text = adjacent.ToString() lblTangent.Text = tangent.ToString() End Sub ' Pythagorean identity: sin²θ + cos²θ = 1 (always true) For deg As Integer = 0 To 360 Step 15 Dim r = ToRad(deg) Dim identity = Math.Pow(Math.Sin(r), 2) + Math.Pow(Math.Cos(r), 2) ' identity ≈ 1.0 for all angles Next ' Plotting a sine wave to a PictureBox ' x goes 0 to 360 degrees; y = A * sin(f * rad) + offset Private Sub DrawSineWave(g As Graphics, w As Integer, h As Integer) Dim cx = w \ 2, cy = h \ 2 ' centre of drawing area Dim amp = cy - 10 ' amplitude (pixels) Dim pts(360) As PointF For i = 0 To 360 Dim x = CSng(i / 360.0 * w) Dim y = CSng(cy - amp * Math.Sin(ToRad(i))) pts(i) = New PointF(x, y) Next g.DrawLines(Pens.Blue, pts) End Sub
Drag the angle slider or type a degree value. The unit circle updates in real time showing sin, cos, and tan values, with the point on the circle and the right-triangle sides.
19.3 Inverse Trig Functions — Asin, Acos, Atan, Atan2
The inverse trig functions answer "what angle has this sin/cos/tan value?" They all return radians. Math.Atan2(y, x) is especially important in graphics programming because it determines the angle of a vector (from origin to point) and correctly handles all four quadrants.
' Math.Asin -- inverse sine, returns [-PI/2, PI/2] ToDeg(Math.Asin(0)) ' 0° ToDeg(Math.Asin(0.5)) ' 30° ToDeg(Math.Asin(1)) ' 90° ToDeg(Math.Asin(-1)) ' -90° Math.Asin(2) ' Double.NaN (domain: -1 to 1 only) ' Math.Acos -- inverse cosine, returns [0, PI] ToDeg(Math.Acos(1)) ' 0° ToDeg(Math.Acos(0.5)) ' 60° ToDeg(Math.Acos(0)) ' 90° ToDeg(Math.Acos(-1)) ' 180° ' Math.Atan -- inverse tangent, returns (-PI/2, PI/2) ' WARNING: does not distinguish quadrants! ToDeg(Math.Atan(1)) ' 45° (same as Atan(-1,-1) in quadrant 3!) ToDeg(Math.Atan(-1)) ' -45° ' Math.Atan2(y, x) -- PREFERRED, full (-PI, PI] range, all quadrants ToDeg(Math.Atan2(1, 1)) ' 45° (Q1: x>0, y>0) ToDeg(Math.Atan2(1, -1)) ' 135° (Q2: x<0, y>0) ToDeg(Math.Atan2(-1, -1)) ' -135° (Q3: x<0, y<0) ToDeg(Math.Atan2(-1, 1)) ' -45° (Q4: x>0, y<0) ToDeg(Math.Atan2(1, 0)) ' 90° (x=0: no div-by-zero!) ' Practical: angle between two screen points Private Function AngleBetween(x1 As Double, y1 As Double, x2 As Double, y2 As Double) As Double Return ToDeg(Math.Atan2(y2 - y1, x2 - x1)) End Function ' Find missing angle in right triangle given two sides Private Function AngleFromSides(opposite As Double, hypotenuse As Double) As Double Return ToDeg(Math.Asin(opposite / hypotenuse)) End Function AngleFromSides(3, 5) ' 36.87° (3-4-5 right triangle)
19.4 Wave Generation
Sine and cosine are periodic functions — they repeat every 2π radians. By adjusting amplitude (height), frequency (how fast it repeats), and phase (where it starts), you can model any periodic phenomenon: sound waves, oscillating springs, rotating objects, and game animations.
' General wave formula: y = A * sin(2π * f * t + φ) ' A = amplitude, f = frequency (Hz), t = time (sec), φ = phase (radians) Private Function SineWave(t As Double, amplitude As Double, frequency As Double, Optional phase As Double = 0) As Double Return amplitude * Math.Sin(2 * Math.PI * frequency * t + phase) End Function ' Lissajous figure: x = A*cos(a*t + δ), y = B*sin(b*t) ' When a:b = 1:2, produces a figure-8 Private Sub DrawLissajous(g As Graphics, cx As Integer, cy As Integer, a As Integer, b As Integer) Dim pts(629) As PointF For i = 0 To 629 Dim t = i * 0.01 ' t from 0 to 2π (approx) Dim x = CSng(cx + 90 * Math.Cos(a * t + Math.PI / 2)) Dim y = CSng(cy + 90 * Math.Sin(b * t)) pts(i) = New PointF(x, y) Next g.DrawLines(Pens.Blue, pts) End Sub ' Circular motion: object orbiting a center point ' Used in game engines, clock hands, radar sweeps Private Sub UpdateOrbit(t As Double, cx As Integer, cy As Integer, radius As Double, speed As Double) Dim angle = t * speed ' radians -- advances with time Dim x = cx + CInt(radius * Math.Cos(angle)) Dim y = cy + CInt(radius * Math.Sin(angle)) picObject.Location = New Point(x, y) End Sub
Adjust amplitude, frequency, phase, and wave type to see how each parameter affects the curve. Overlay Sin + Cos to see phase relationships.
19.5 Navigation — Bearing and Distance
' Haversine formula: great-circle distance between two GPS coordinates ' Returns distance in kilometres Private Function Haversine(lat1 As Double, lon1 As Double, lat2 As Double, lon2 As Double) As Double Const R = 6371.0 ' Earth radius in km Dim dLat = ToRad(lat2 - lat1) Dim dLon = ToRad(lon2 - lon1) Dim a = Math.Pow(Math.Sin(dLat / 2), 2) + Math.Cos(ToRad(lat1)) * Math.Cos(ToRad(lat2)) * Math.Pow(Math.Sin(dLon / 2), 2) Dim c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)) Return Math.Round(R * c, 2) End Function ' Compass bearing from point 1 to point 2 (degrees from North, 0-360) Private Function Bearing(lat1 As Double, lon1 As Double, lat2 As Double, lon2 As Double) As Double Dim r1 = ToRad(lat1), r2 = ToRad(lat2) Dim dL = ToRad(lon2 - lon1) Dim y = Math.Sin(dL) * Math.Cos(r2) Dim x = Math.Cos(r1) * Math.Sin(r2) - Math.Sin(r1) * Math.Cos(r2) * Math.Cos(dL) Return (ToDeg(Math.Atan2(y, x)) + 360) Mod 360 End Function ' Example: Kuala Lumpur to Singapore Dim km = Haversine(3.1390, 101.6869, 1.3521, 103.8198) ' ≈ 319 km Dim bearing = Bearing(3.1390, 101.6869, 1.3521, 103.8198) ' ≈ 156° (SSE)
Solve a right triangle using trig functions. Enter any two known values (angle + side, or two sides) and compute all missing sides, angles, area, and perimeter. Visual diagram updates in real time.
19.6 Practical: Analogue Clock & Rotation
' Analogue clock hand positions ' Clock hands rotate clockwise; 12 o'clock is -90° (top = -PI/2 radians) Private Function HandEndPoint(cx As Integer, cy As Integer, length As Integer, angleDeg As Double) As Point Dim rad = ToRad(angleDeg - 90) ' -90 shifts 0° from right to top Return New Point(cx + CInt(length * Math.Cos(rad)), cy + CInt(length * Math.Sin(rad))) End Function Private Sub DrawClock(g As Graphics, now As DateTime) Dim cx = 100, cy = 100 ' centre ' Hour hand: 360° / 12h = 30° per hour, +0.5° per minute Dim hourAngle = now.Hour * 30 + now.Minute * 0.5 ' Minute hand: 360° / 60min = 6° per minute Dim minAngle = now.Minute * 6 ' Second hand: 6° per second Dim secAngle = now.Second * 6 g.DrawLine(Pens.Black, New Point(cx, cy), HandEndPoint(cx, cy, 55, hourAngle)) g.DrawLine(Pens.DarkBlue, New Point(cx, cy), HandEndPoint(cx, cy, 75, minAngle)) g.DrawLine(Pens.Red, New Point(cx, cy), HandEndPoint(cx, cy, 80, secAngle)) End Sub ' Rotate a point around an origin (2D rotation matrix) ' x' = x*cos(θ) - y*sin(θ) ' y' = x*sin(θ) + y*cos(θ) Private Function RotatePoint(px As Double, py As Double, ox As Double, oy As Double, angleDeg As Double) As PointF Dim rad = ToRad(angleDeg) Dim dx = px - ox, dy = py - oy Return New PointF(CSng(ox + dx * Math.Cos(rad) - dy * Math.Sin(rad)), CSng(oy + dx * Math.Sin(rad) + dy * Math.Cos(rad))) End Function
A live analogue clock drawn entirely with trig functions. Each hand position is computed using Math.Sin and Math.Cos with the degree-to-radian conversion. Shows the VB formula for each hand.
Enter any angle (degrees or radians) and see all six trig values: sin, cos, tan, asin, acos, atan. Includes the inverse calculation and the VB code for each call.
19.7 GitHub Copilot — Trig Patterns
' Given a satellite position (azimuth and elevation in degrees), calculate the dish motor angles needed to point at it. Use Math.Sin, Math.Cos, Math.Atan2.'' Satellite dish pointing calculator -- azimuth/elevation to motor angles. Private Sub PointDish(azimuthDeg As Double, elevationDeg As Double) Dim az = ToRad(azimuthDeg) Dim el = ToRad(elevationDeg) ' Convert spherical (az, el) to Cartesian unit vector Dim x = Math.Cos(el) * Math.Sin(az) Dim y = Math.Cos(el) * Math.Cos(az) Dim z = Math.Sin(el) ' Back to polar for motor control Dim motorAz = ToDeg(Math.Atan2(x, y)) Dim motorEl = ToDeg(Math.Asin(z)) ' Normalise azimuth to 0–360 If motorAz < 0 Then motorAz += 360 lblMotorAz.Text = $"Motor Azimuth: {motorAz:F2}°" lblMotorEl.Text = $"Motor Elevation: {motorEl:F2}°" End Sub
' Simulate projectile motion: given initial speed and launch angle, calculate range, max height, and flight time. Use Math.Sin, Math.Cos, Math.Pow. Ignore air resistance.'' Projectile motion calculator (no air resistance). Private Sub CalculateProjectile(speed As Double, angleDeg As Double) Const g = 9.81 Dim rad = ToRad(angleDeg) ' Horizontal and vertical velocity components Dim vx = speed * Math.Cos(rad) ' horizontal (constant) Dim vy = speed * Math.Sin(rad) ' vertical (decelerates) ' Flight time: T = 2*vy / g Dim T = Math.Round(2 * vy / g, 3) ' Maximum height: H = vy² / (2g) Dim H = Math.Round(Math.Pow(vy, 2) / (2 * g), 3) ' Horizontal range: R = vx * T (or v²*sin(2θ)/g) Dim R = Math.Round(Math.Pow(speed, 2) * Math.Sin(2 * rad) / g, 3) lblFlightTime.Text = $"Flight time: {T} s" lblMaxHeight.Text = $"Max height: {H} m" lblRange.Text = $"Range: {R} m" lblAngle45.Text = "Maximum range is achieved at 45°" End Sub
Try these in the Copilot Chat panel while working with trig functions:
- "Write a Sub DrawPolygon(g, cx, cy, sides, radius) that draws a regular polygon using Math.Cos and Math.Sin with 2*PI/sides spacing"
- "Implement a radar sweep animation: draw a rotating line using Math.Cos/Sin in a Timer Tick event, and plot blip points"
- "Create a Function SoundPressureLevel(distance) that uses Math.Log10 and the inverse square law to compute dB at distance"
- "Use the law of cosines (cos C = (a²+b²-c²)/(2ab)) with Math.Acos to solve an oblique triangle given all three sides"
Lesson Summary
- All VB 2026 trig functions use radians. Convert with
degrees * Math.PI / 180before passing toMath.Sin,Math.Cos, orMath.Tan. Passing degrees directly produces wrong values silently. Math.SinandMath.Cosalways return values in [−1, 1].Math.Tanis undefined at 90° and 270° (±∞). The Pythagorean identity sin²θ + cos²θ = 1 always holds.- Inverse functions:
Math.Asinreturns [−π/2, π/2];Math.Acosreturns [0, π];Math.Atanreturns (−π/2, π/2). All inputs outside valid domains returnDouble.NaN. - Use
Math.Atan2(y, x)instead ofMath.Atan(y/x).Atan2handles all four quadrants and avoids division-by-zero when x = 0. - Circular motion:
x = cx + r * Math.Cos(angle),y = cy + r * Math.Sin(angle). This pattern drives clock hands, orbiting objects, radar sweeps, and regular polygon drawing. - Wave generation:
y = A * Math.Sin(2π * f * t + φ). Adjusting amplitude (A), frequency (f), and phase (φ) models sound, oscillations, tides, and AC voltage.
Exercises
Exercise 19.1 — Regular Polygon Drawer
- Draw regular polygons (3 to 12 sides) on a PictureBox using a For loop and
Math.Cos/Math.Sin - Formula: vertex i at angle
2π*i/nfrom centre, scaled by radius - Add a NumericUpDown for sides and a TrackBar for rotation angle
- Color the interior using a SolidBrush and
g.FillPolygon - Copilot challenge: Ask Copilot to "animate the polygon rotating 1 degree per Timer Tick using a running angle variable"
Exercise 19.2 — Projectile Simulator
- Build a form with TrackBars for launch speed (10–100 m/s) and angle (1–89°)
- Calculate range, max height, and flight time using
Math.Sin,Math.Cos,Math.Pow - Draw the parabolic trajectory on a PictureBox sampling 100 time steps
- Show that 45° maximises range and add a label when the optimal angle is selected
- Copilot challenge: Ask Copilot to "add air resistance: ax = −k*vx, ay = −g − k*vy, using Euler integration in a loop"
Exercise 19.3 — GPS Distance Calculator
- Create a form with four TextBoxes for lat/lon of two locations
- Implement the Haversine formula using
Math.Sin,Math.Cos,Math.Atan2,Math.Sqrt - Also compute the compass bearing (0–360°) from point 1 to point 2
- Pre-fill with Malaysian cities: KL, Penang, JB, Kota Kinabalu
- Copilot challenge: Ask Copilot to "add a dropdown of preset city pairs and auto-fill the TextBoxes on selection"
Related Resources
← Lesson 18
Math Functions — Abs, Sqrt, Pow, Log, rounding.
Lesson 20 →
Format Function — number and date formatting.
MS Docs — System.Math
Complete reference including all trig functions in .NET 10.
VB Math Functions
VB-specific math and trig function reference page.
Featured Books
Visual Basic 2022 Made Easy
Trig functions applied to graphics, animation, and scientific calculations with complete code examples.
View on Amazon →
VB Programming With Code Examples
Practical programs using trig for clock drawing, polygon graphics, and navigation calculations.
View on Amazon →