none
How to draw Sin Waves for a Happy New Year! RRS feed

  • General discussion

  • Happy New Year Everyone!

    :)

    'sin wave wave animation v2
    Imports System.Drawing.Drawing2D
    Public Class Form3
        Private WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 30}
        Private Waves As New List(Of WaveData)
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ClientSize = New Size(500, 500)
            DoubleBuffered = True
            Text = "Waves"
    
            Dim wv0 As New WaveData
            With wv0
                .Amplitude = 60
                .WaveLength = 800
                .PhaseStep = 0.01
                .YOffset = 0
                .Rect = ClientRectangle
                .Fill = True
            End With
            Waves.Add(wv0)
    
            Dim wv As New WaveData
            With wv
                .Amplitude = 80
                .WaveLength = 600
                .PhaseStep = 0.01
                .YOffset = 50
                .Rect = ClientRectangle
                .Fill = True
            End With
            Waves.Add(wv)
    
            Dim wv1 As New WaveData
            With wv1
                .Amplitude = 100
                .WaveLength = 500
                .PhaseStep = 0.02
                .YOffset = 100
                .Rect = ClientRectangle
                .Fill = True
            End With
            Waves.Add(wv1)
    
            Form7_Resize(0, Nothing)
            Timer1.Start()
        End Sub
    
        Private Sub Form7_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                Dim h As Single = CSng(.VisibleClipBounds.Height)
                Dim h2 As Single = CSng(.VisibleClipBounds.Height / 2)
                Dim pth As GraphicsPath
                Dim br2 As New SolidBrush(Color.Blue)
    
                Using p1 As New Pen(Color.Blue, 2),
                            brw As New SolidBrush(Color.Blue),
                            f As New Font("arial", 11)
    
                    Dim brlb As New LinearGradientBrush(.VisibleClipBounds, Color.FromArgb(255, Color.MidnightBlue), Color.FromArgb(255, Color.Blue), 90)
                    Dim brbg As New LinearGradientBrush(.VisibleClipBounds, Color.FromArgb(100, Color.SkyBlue), Color.FromArgb(255, Color.Blue), 90)
                    'draw background 
                    .FillRectangle(brbg, .VisibleClipBounds)
    
                    .FillRectangle(brbg, New Rectangle(0, h2, .VisibleClipBounds.Width, h2))
    
                    For i As Integer = 0 To Waves.Count - 1
                        pth = Waves(i).GetWavePath()
    
                        If Waves(i).Fill Then
                            If i = Waves.Count - 1 Then
                                brlb = New LinearGradientBrush(.VisibleClipBounds,
                                                           Color.FromArgb(255, Color.SkyBlue),
                                                           Color.FromArgb(255, Color.Blue), 90)
                                .FillPath(brlb, pth)
                            Else
                                br2 = New SolidBrush(Color.FromArgb(255, 25 + (i * 20), 100 + (i * 20), 250))
                                .FillPath(br2, pth)
                            End If
                        Else
                            .DrawPath(p1, pth)
                        End If
    
                        Dim y As Integer
                        If i = 0 Then
                            y = 0.93 * pth.PathData.Points(pth.PathData.Points.Count / 2).Y
                            .DrawString("Happy New Year", New Font("tahoma", 12, FontStyle.Bold), Brushes.AntiqueWhite, .VisibleClipBounds.Width / 2, y)
                        End If
    
                        If i = Waves.Count - 1 Then
                            y = 1.4 * pth.PathData.Points(pth.PathData.Points.Count / 2).Y
                            .DrawString("tommytwotrain", New Font("tahoma", 12), Brushes.LightBlue, 10, y)
                        End If
                    Next
    
                    pth.Dispose()
                    brlb.Dispose()
                    brbg.Dispose()
                End Using
                br2.Dispose()
            End With
        End Sub
    
        Private Sub Form7_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            For Each wv As WaveData In Waves
                wv.Rect = New Rectangle(0, 0, ClientSize.Width, ClientSize.Height)
            Next
            Invalidate()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            For Each wv As WaveData In Waves
                wv.PhaseShift += wv.PhaseStep
                If wv.PhaseShift > Math.PI * 2 Then wv.PhaseShift -= Math.PI * 2
            Next
            Invalidate()
        End Sub
    
        Public Class WaveData
            Public Amplitude As Double = 10
            Public WaveLength As Double = 100
            Public PhaseStep As Double = 5
            Public PhaseShift As Double = 0
            Public YOffset As Double
            Public Rect As Rectangle
            Public Fill As Boolean
    
            Public Function GetWavePath() As GraphicsPath
                Dim x1, y1, x2, y2 As Single
                Dim pi2 As Double = 2 * Math.PI
                Dim pts As New List(Of PointF)
                Dim thisStep As Double = pi2 / 100
                Dim testRect As RectangleF = Rect
                Dim x3 As Single = testRect.X - CSng(YOffset)
                Dim maxX As Single = testRect.Right
                Dim a As Double
                Dim yMiddle As Double = YOffset + Rect.Y + (Rect.Height / 2)
    
                Do While x3 < maxX
                    x2 = CSng(x3)
                    y2 = CSng(Amplitude * Math.Sin(-pi2))
    
                    For a = PhaseShift To pi2 + PhaseShift Step thisStep
                        x1 = CSng(x3 + (WaveLength * (a - PhaseShift)))
                        y1 = CSng(yMiddle + (Amplitude * Math.Sin(a)))
    
                        If testRect.Contains(New PointF(x1, y1)) Then
                            pts.Add(New PointF(x1, y1))
                        End If
    
                        x2 = x1
                        y2 = y1
                    Next
                    x3 = x1
                Loop
    
                If pts.Count > 0 Then pts.Add(New PointF(maxX, pts(pts.Count - 1).Y))
    
                If Fill Then
                    pts.Add(New PointF(maxX, testRect.Bottom))
                    pts.Add(New PointF(testRect.X, testRect.Bottom))
                    pts.Add(New PointF(testRect.X, pts(0).Y))
                End If
    
                Dim pth As New GraphicsPath
                pth.AddLines(pts.ToArray)
                GetWavePath = CType(pth.Clone, GraphicsPath)
                pth.Dispose()
            End Function
        End Class
    End Class


    Monday, January 1, 2018 1:02 PM

All replies

  • You too, and everybody else

     Cor


    Monday, January 1, 2018 1:21 PM
  • As always, good work Tommy. :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, January 1, 2018 1:56 PM
  •  Very cool sine waves Tom.  8)

     Happy new year to all of our members.

    Here is a little animation i made with my Gif Creator 2.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, January 1, 2018 3:08 PM
    Monday, January 1, 2018 3:00 PM