Liste
Enoncé

Visual Basic - Solution 6 ( Projectiles )

 

Interface graphique :

Il y a très peu à dire au sujet de l'interface, puisqu'il s'agit de dessiner directement sur le fond de la feuille (Form1).

Code de la feuille Form1 :

Dim i As Integer, j As Integer, k As Integer
Dim R As Double 'Force de propulsion du projectile
Dim A As Double 'Angle du lancement
Const Projectiles = 60
Dim F(1 To Projectiles, 1 To 4) As XYPoint
Dim FP(1 To 4) As XYPoint
Dim V As XYPoint
Dim Vh(1 To Projectiles) As XYPoint
Dim T As Double
Const Pi = 3.14159265
Const Tmax = 100

Private Sub Form_Activate()
     Form1.Scale (0, 50)-(100, -50)
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

     Randomize

    'Paramétrage des trajectoires des projectiles
     For i = 1 To Projectiles
          F(i, 1).X = X
          F(i, 1).Y = Y
          R = Rnd * 20
          A = Rnd * 2 * Pi
          F(i, 2).X = X + Cos(A) * R
          F(i, 2).Y = Y + Sin(A) * R
          F(i, 3).X = X + (F(i, 2).X - X) * 1.5
          F(i, 3).Y = Y + (F(i, 2).Y - Y) * 1.5
          F(i, 4).X = F(i, 3).X
          F(i, 4).Y = F(i, 3).Y - R
          Vh(i) = F(i, 1)
     Next i

    'lancement des projectiles
     For i = 1 To Tmax
          T = i / Tmax
          For j = 1 To Projectiles
               For k = 1 To 4
                    FP(k) = F(j, k)
               Next k
               V = bezier(FP, T)
               Form1.Line (Vh(j).X, Vh(j).Y)-(V.X, V.Y)
               Vh(j) = V
          Next j
     Next i
End Sub

Code à placer dans un module :

Public Type XY
     X As Integer
     Y As Integer
End Type

Public Function bezier(P() As XYPoint, T As Double) As XYPoint
     bezier.X = P(1).X * (1 - T) ^ 3 _
          + P(2).X * (1 - T) ^ 2 * 3 * T _
          + P(3).X * (1 - T) * 3 * T ^ 2 _
          + P(4).X * T ^ 3
     bezier.Y = P(1).Y * (1 - T) ^ 3 _
          + P(2).Y * (1 - T) ^ 2 * 3 * T _
          + P(3).Y * (1 - T) * 3 * T ^ 2 _
          + P(4).Y * T ^ 3
End Function

    ( retour à l'énoncé )