Ruotare un oloide in una DrawingArea: differenze tra le versioni

Da Gambas-it.org - Wikipedia.
Vai alla navigazione Vai alla ricerca
(Creata pagina con "Di seguito mostriamo un possibile codice, per disegnare e far ruotare automaticamente un [https://en.wikipedia.org/wiki/Oloid Oloide] su una ''DrawingArea''. Private Const R...")
 
Nessun oggetto della modifica
 
(4 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
Di seguito mostriamo un possibile codice, per disegnare e far ruotare automaticamente un [https://en.wikipedia.org/wiki/Oloid Oloide] su una ''DrawingArea''.
Di seguito mostriamo un possibile codice, per disegnare e far ruotare automaticamente <U>con effetto 3D</u> un [https://en.wikipedia.org/wiki/Oloid Oloide] su una ''DrawingArea''.
Private DrawingArea1 As DrawingArea
Private Timer1 As Timer
  Private Const R As Float = 100.0
  Private Const R As Float = 100.0
  Private Const N As Integer = 100
  Private Const N As Integer = 100
Riga 8: Riga 10:
   
   
   With Me
   With Me
     .Center
     .Move(0, 0, Screen.AvailableWidth, Screen.AvailableHeight)
    .Resizable = False
     .Arrangement = Arrange.Fill
     .Width = 640
    .Height = 640
   End With
   End With
   
   
   With DrawingArea1
   DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
    .X = 0
     .Y = 0
  With Timer1 = New Timer As "Timer1"
    .W = Me.W
     .Delay = 30  <FONT Color=gray>' ''30 ms = ~33 FPS''</font>
     .H = Me.H
     .Start
   End With
   End With
<FONT Color=gray>' ''30 ms = ~33 FPS''</font>
  Timer1.Delay = 30
  Timer1.Start
   
   
  End
  End
Riga 51: Riga 47:
   cx = DrawingArea1.Width / 2
   cx = DrawingArea1.Width / 2
   cy = DrawingArea1.Height / 2
   cy = DrawingArea1.Height / 2
<FONT Color=gray>' ''L'oloide verrà creato da una molteplicità di piccoli punti a forma di rettangolo:''</font>
  Paint.FillRect(0, 0, DrawingArea1.Width, DrawingArea1.Height, Color.White)
   
   
   Paint.AntiAlias = True
   Paint.AntiAlias = True
Riga 80: Riga 73:
       depthColor = Clamp(100 + Int(zRot), 0, 255)
       depthColor = Clamp(100 + Int(zRot), 0, 255)
   
   
  <FONT Color=gray>' ''Disegna un punto (piccolo rettangolo):''</font>
  <FONT Color=gray>' ''Disegna un punto (piccolo rettangolo).''
' ''L'oloide verrà creato da una molteplicità di piccoli punti a forma di rettangolo:''</font>
       Paint.FillRect(px, py, 2, 2, Color.RGB(depthColor, depthColor, 200))
       Paint.FillRect(px, py, 2, 2, Color.RGB(depthColor, depthColor, 200))
     Next
     Next
   Next
   Next
  Paint.End
   
   
  End
  End

Versione attuale delle 18:43, 10 ago 2025

Di seguito mostriamo un possibile codice, per disegnare e far ruotare automaticamente con effetto 3D un Oloide su una DrawingArea.

Private DrawingArea1 As DrawingArea
Private Timer1 As Timer
Private Const R As Float = 100.0
Private Const N As Integer = 100
Private angle As Float    ' Angolo di rotazione (in radianti)


Public Sub Form_Open()

 With Me
   .Move(0, 0, Screen.AvailableWidth, Screen.AvailableHeight)
   .Arrangement = Arrange.Fill
 End With

 DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"

 With Timer1 = New Timer As "Timer1"
   .Delay = 30   ' 30 ms = ~33 FPS
   .Start
 End With

End


Public Sub Timer1_Timer()

' Incrementa l’angolo:
 angle += 0.02 ' Incrementa l’angolo

 If angle > 2 * Pi Then angle -= 2 * Pi
 DrawingArea1.Refresh

End


Public Sub DrawingArea1_Draw()

 Dim u, v As Float
 Dim x, y, z As Float
 Dim px, py As Float
 Dim cx, cy As Float
 Dim i, j As Integer
 Dim depthColor As Integer

' Centra il disegno dell'Oloide:
 cx = DrawingArea1.Width / 2
 cy = DrawingArea1.Height / 2

 Paint.AntiAlias = True
 Paint.LineWidth = 1

 For i = 0 To N
   u = i / N
   For j = 0 To N
     v = j / N

' Parametrizzazione dell’oloide:
     x = (1 - 2 * u) * R * Cos(Pi * v)
     y = (1 - 2 * u) * R * Sin(Pi * v)
     z = 2 * R * u * Sqr(1 - (Sin(Pi * v)) ^ 2)

' Rotazione attorno all’asse Y (animazione):
     Dim xRot, zRot As Float
     xRot = x * Cos(angle) + z * Sin(angle)
     zRot = -x * Sin(angle) + z * Cos(angle)

' Proiezione isometrica:
     px = cx + (xRot - y) * 0.5
     py = cy - (xRot + y) * 0.3 + zRot * 0.7

' Colore sfumato in base alla profondità:
     depthColor = Clamp(100 + Int(zRot), 0, 255)

' Disegna un punto (piccolo rettangolo).
' L'oloide verrà creato da una molteplicità di piccoli punti a forma di rettangolo:
     Paint.FillRect(px, py, 2, 2, Color.RGB(depthColor, depthColor, 200))
   Next
 Next

End


Public Sub Form_Resize()

 DrawingArea1.Refresh

End


Private Function Clamp(Value As Integer, MinVal As Integer, MaxVal As Integer) As Integer

 If Value < MinVal Then Return MinVal
 If Value > MaxVal Then Return MaxVal
 Return Value

End