Android Code Snippet Mathemetics Pendulum

Discussion in 'Code Snippets' started by Erel, Sep 5, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    A not very useful animated pendulum.

    test.gif
    (it is smoother on a real device in release mode)

    Code:
    Sub Activity_Create(FirstTime As Boolean)
       
    Dim cvs As Canvas
       cvs.Initialize(
    Activity)
       DrawPendulum (cvs, 
    50%x20%y40%x, -45Colors.Red)
       DrawPendulum (cvs, 
    50%x60%y30%x45Colors.Blue)
       DrawPendulum (cvs, 
    150dip10dip20%x70Colors.Green)
    End Sub

    Sub DrawPendulum (cvs As Canvas, cx As Float, cy As Float, l As Float, ZeroAngle As Float, clr As Int)
       
    Dim teta0 As Float = ZeroAngle / 180 * cPI
       
    Dim start As Long = DateTime.Now
       
    Dim w As Float = Sqrt(1000 / l) / 1000
       
    Dim x2, y2 As Float
       
    Do While True
         Sleep(
    10)
         
    Dim t As Int = DateTime.Now - start
         
    Dim teta As Float = teta0 * Cos(w * t)
         cvs.DrawLine(cx, cy, x2, y2, 
    Colors.Transparent, 2dip)
         cvs.DrawCircle(x2, y2, 
    20dipColors.Transparent, True1dip)
         x2 = cx + l * 
    Sin(teta)
         y2 = cy + l * 
    Cos(teta)
         cvs.DrawLine(cx, cy, x2, y2, 
    Colors.White, 2dip)
         cvs.DrawCircle(x2, y2, 
    20dip, clr, True1dip)
         
    Activity.Invalidate
       
    Loop
    End Sub
     
  2. GMan

    GMan Well-Known Member Licensed User

    But nice to look @ :D
     
  3. udg

    udg Expert Licensed User

    Command me, master... (I'm hypnothized by the green one..eheh)
     
    GMan, Erel and DonManfred like this.
  4. Javier Donayre

    Javier Donayre Member Licensed User

    Next one, the "inverted" animated pendulum!! ;)
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    I'm afraid that unstable systems are above my physics level...

    Three springs instead:

    test.gif

    Code:
    Sub Activity_Create(FirstTime As Boolean)
       
    Dim cvs As Canvas
       cvs.Initialize(
    Activity)
       DrawSpring(cvs, 
    50%x20%y40%y0.004Colors.Red)
       DrawSpring(cvs, 
    20%x20%y40%y0.002Colors.Green)
       DrawSpring(cvs, 
    80%x20%y40%y0.003Colors.Blue)
    End Sub

    Sub DrawSpring(cvs As Canvas, cx As Float, cy As Float, l As Float, w As Float, clr As Int)
       
    Dim start As Long = DateTime.Now
       
    Dim x As Float
       
    Dim num As Int = 8
       
    Dim width As Int = 20dip
       
    Dim p As Path
       
    Do While True
         Sleep(
    10)
         
    If p.IsInitialized Then
           cvs.DrawPath(p, 
    Colors.Transparent, False2dip)
           cvs.DrawCircle(cx, cy + l + x, 
    40dipColors.Transparent, True1dip)
         
    End If
         
    Dim t As Int = DateTime.Now - start
         x = 
    60dip * Cos(w * t - 0.5)
         
    Dim h As Float = (l + x - 20dip) / num
         p.Initialize(cx, cy + h/
    4)
         
    For i = 0 To num - 1
           p.LineTo(cx - width, cy + h / 
    2 + h * i)
           p.LineTo(cx + width, cy + h + h * i)
         
    Next
         cvs.DrawPath(p, 
    Colors.White, False2dip)
         cvs.DrawCircle(cx, cy + l + x, 
    40dip, clr, True1dip)
         
    Activity.Invalidate
       
    Loop
    End Sub
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice