Ruotare un rombo in una DrawingArea

Da Gambas-it.org - Wikipedia.
Versione del 5 apr 2025 alle 16:36 di Vuott (Discussione | contributi)

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

Di seguito mostreremo un possibile codice per ruotare un Rombo disegnato in una DrawingArea.

E' possibile variare la dimensione del Rombo, modificando il raggio della circonferenza, sulla quale i suoi 4 vertici insistono.
Il fulcro del Metodo ".Translate()" agisce da vortice, ossia da centro di rotazione intorno a una ipotetica circonferenza.
La rotazione del Rombo si avvia semplicemente cliccando con il tasto sinistro del mouse sulla superficie della DrawingArea.

Private DrawingArea1 As DrawingArea
Private Const RAGGIO As Single = 100.0
Private Const ROMBO As Byte = 4
Private ANGOLO As Single = 0.8
Private an As Single
Private xs As Single
Private ys As Single
Private coord As Single[]
Private pol As New Single[]
Private c As Integer[] = [Color.Blue, Color.Green, Color.Yellow, Color.Red]
Private f As Float[] = [0, 0.34, 0.67, 1]


Public Sub Form_Open()

 With Me
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight
   .Arrangement = Arrange.Fill
 End With
 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .Background = Color.Lighter(Color.Yellow)
 End With

End


Public Sub Form_Arrange()

 coord = [DrawingArea1.W / 2, DrawingArea1.H / 2]

 Dim gradi As Single[] = [0.0, 90.0, 180.0, 270.0]
 For t As Byte = 0 To ROMBO - 1
   pol.Push(RAGGIO * Cos(Rad(gradi[t])))
   pol.Push(RAGGIO * Sin(Rad(gradi[t])))
 Next

' Il risultato di ((25 * RAGGIO) / 100) aggiusta in orizzontale il punto di rotazione del Rombo:
 xs = (coord[0] + coord[0] + pol[2] + coord[0] + Abs(pol[4]) + coord[0] - Abs(pol[6])) / ROMBO - ((25 * RAGGIO) / 100)
 ys = (coord[1] + coord[1] + pol[3] + coord[1] + pol[5] + coord[1] + pol[7]) / ROMBO

End


Public Sub DrawingArea1_MouseUp()

 While Object.IsValid(DrawingArea1)
   an += ANGOLO
   DrawingArea1.Refresh
   Wait 0.01
 Wend

End


Public Sub DrawingArea1_Draw()

 With Paint
   .Arc(coord[0], coord[1], 2, Rad(0), Rad(360), False)
   .Fill
   .Brush = .LinearGradient(0, 20, 15, 0, c, f)
   .Translate(coord[0], coord[1])
   .Rotate(Rad(-an))
   .Polygon([((coord[0] + pol[0]) - xs) - 50, (coord[1] + pol[1]) - ys, (coord[0] + pol[2]) - xs, (coord[1] + pol[3]) - ys, ((coord[0] + pol[4]) - xs) + 50, (coord[1] + pol[5]) - ys, (coord[0] + pol[6]) - xs, (coord[1] + pol[7]) - ys])
   .Stroke
   .End
 End With

End