Ruotare un esagono in una DrawingArea: differenze tra le versioni
Vai alla navigazione
Vai alla ricerca
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| (2 versioni intermedie di uno stesso utente non sono mostrate) | |||
| Riga 1: | Riga 1: | ||
Di seguito mostreremo un possibile codice per ruotare un Esagono regolare disegnato in una ''DrawingArea''. <SUP>[[[#Note|Nota 1]]]</sup> | Di seguito mostreremo un possibile codice per ruotare un Esagono regolare disegnato in una ''DrawingArea''. <SUP>[[[#Note|Nota 1]]]</sup> | ||
Private DrawingArea1 As DrawingArea | Private DrawingArea1 As DrawingArea | ||
Private Const <FONT Color= | Private Tmr As Timer | ||
Private | Private Const ScalaRaggio As Float = 0.2 <FONT Color=gray>' ''Scala del raggio (dimensione dell'esagono)''</font> | ||
Private Rotazione As Float <FONT Color=gray>' ''Angolo di rotazione in radianti''</font> | |||
| Riga 21: | Riga 9: | ||
With Me | With Me | ||
. | .Move(0, 0, Screen.AvailableWidth, Screen.AvailableHeight) | ||
.Arrangement = Arrange.Fill | .Arrangement = Arrange.Fill | ||
End With | End With | ||
With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1" | With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1" | ||
.Background = Color.Lighter(Color.Yellow) | .Background = Color.Lighter(Color.Yellow) | ||
End With | End With | ||
Tmr = New Timer As "Tmr" | |||
Tmr.Delay = 30 <FONT Color=gray>' ''circa 33 FPS''</font> | |||
Tmr.Start() | |||
End | End | ||
Public Sub Tmr_Timer() | |||
Rotazione += Rad(1.5) <FONT Color=gray>' ''Rotazione di 1.5° per frame''</font> | |||
If Rotazione >= 2 * Pi Then | |||
Rotazione -= 2 * Pi | |||
End If | |||
DrawingArea1.Refresh() | |||
End | End | ||
Public Sub DrawingArea1_Draw() | |||
Dim cx As Float = DrawingArea1.W / 2 | |||
Dim cy As Float = DrawingArea1.H / 2 | |||
Dim r As Float = Min(DrawingArea1.W, DrawingArea1.H) * ScalaRaggio | |||
Dim numSides As Integer = 6 | |||
Dim angleStep As Float = 2 * Pi / numSides | |||
Dim points As New PointF[numSides] | |||
Dim i As Integer | |||
For i = 0 To numSides - 1 | |||
Dim angle As Float = angleStep * i - Pi / 2 + Rotazione | |||
points[i] = New PointF(cx + r * Cos(angle), cy + r * Sin(angle)) | |||
Next | |||
With Paint | With Paint | ||
.Brush = Paint.Color(Color.Black) | |||
.LineWidth = 2 | |||
.Brush = . | |||
. | .MoveTo(points[0].X, points[0].Y) | ||
For i = 1 To numSides - 1 | |||
. | .LineTo(points[i].X, points[i].Y) | ||
Next | |||
.LineTo(points[0].X, points[0].Y) | |||
.Stroke | .Stroke | ||
End With | End With | ||
End | End | ||
Versione attuale delle 21:41, 27 lug 2025
Di seguito mostreremo un possibile codice per ruotare un Esagono regolare disegnato in una DrawingArea. [Nota 1]
Private DrawingArea1 As DrawingArea
Private Tmr As Timer
Private Const ScalaRaggio As Float = 0.2 ' Scala del raggio (dimensione dell'esagono)
Private Rotazione 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
With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
.Background = Color.Lighter(Color.Yellow)
End With
Tmr = New Timer As "Tmr"
Tmr.Delay = 30 ' circa 33 FPS
Tmr.Start()
End
Public Sub Tmr_Timer()
Rotazione += Rad(1.5) ' Rotazione di 1.5° per frame
If Rotazione >= 2 * Pi Then
Rotazione -= 2 * Pi
End If
DrawingArea1.Refresh()
End
Public Sub DrawingArea1_Draw()
Dim cx As Float = DrawingArea1.W / 2
Dim cy As Float = DrawingArea1.H / 2
Dim r As Float = Min(DrawingArea1.W, DrawingArea1.H) * ScalaRaggio
Dim numSides As Integer = 6
Dim angleStep As Float = 2 * Pi / numSides
Dim points As New PointF[numSides]
Dim i As Integer
For i = 0 To numSides - 1
Dim angle As Float = angleStep * i - Pi / 2 + Rotazione
points[i] = New PointF(cx + r * Cos(angle), cy + r * Sin(angle))
Next
With Paint
.Brush = Paint.Color(Color.Black)
.LineWidth = 2
.MoveTo(points[0].X, points[0].Y)
For i = 1 To numSides - 1
.LineTo(points[i].X, points[i].Y)
Next
.LineTo(points[0].X, points[0].Y)
.Stroke
End With
End
Note
[1] Vedere anche la seguente pagina: Ruotare un pentagono in una DrawingArea