Ruotare un ottagono in una DrawingArea

Da Gambas-it.org - Wikipedia.

Di seguito mostreremo un possibile codice per ruotare un Ottagono regolare disegnato in una DrawingArea. [Nota 1]

Gli 8 vertici dell'Ottagono regolare sono stati correttamente individuati sulla circonferenza ideale, alla quale detti vertici appartengono, dividendo l'angolo di 360° per il numero dei vertici del Ottagono (360° / 8 = 45°). Ciascun vertice si troverà sulla circonferenza ogni 45 gradi.
E' possibile variare la dimensione del'Ottagono, modificando il raggio della circonferenza, sulla quale i suoi vertici insistono.
La rotazione dell'Ottagono si avvia semplicemente cliccando con il tasto sinistro del mouse sulla superficie della DrawingArea.
Il fulcro del Metodo ".Translate()" agisce da vortice, ossia da centro di rotazione intorno a una ipotetica circonferenza.

Private DrawingArea1 As DrawingArea
Private Const RAGGIO As Single = 100.0
Private Const OTTAGONO As Byte = 8
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]
 
 For t As Short = 0 To 360 Step (360 / OTTAGONO)
   pol.Push(RAGGIO * Cos(Rad(t)))
   pol.Push(RAGGIO * Sin(Rad(t)))
 Next

 xs = (coord[0] + coord[0] - pol[2] + coord[0] - Abs(pol[4]) + coord[0] - Abs(pol[6]) + coord[0] - pol[8] + coord[0] - pol[10] + coord[0] - pol[12] + coord[0] - pol[14] + ((40 * RAGGIO) / 100)) / OTTAGONO
 ys = (coord[1] + coord[1] + pol[3] + coord[1] + pol[5] + coord[1] + pol[7] + coord[1] + pol[9] + coord[1] + pol[11] + coord[1] + pol[13] + coord[1] + pol[15]) / OTTAGONO

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, (coord[1] + pol[1]) - ys, (coord[0] + pol[2]) - xs, (coord[1] + pol[3]) - ys, (coord[0] + pol[4]) - xs, (coord[1] + pol[5]) - ys, (coord[0] + pol[6]) - xs, (coord[1] + pol[7]) - ys, (coord[0] + pol[8]) - xs, (coord[1] + pol[9]) - ys, (coord[0] + pol[10]) - xs, (coord[1] + pol[11]) - ys, (coord[0] + pol[12]) - xs, (coord[1] + pol[13]) - ys, (coord[0] + pol[14]) - xs, (coord[1] + pol[15]) - ys])
   .Stroke
   .End
 End With

End


Note

[1] Vedere anche la seguente pagina: Ruotare un esagono in una DrawingArea