Differenze tra le versioni di "Ruotare un pentagono in una DrawingArea"
(2 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
− | Di seguito mostreremo un possibile codice per ruotare un Pentagono regolare disegnato in una ''DrawingArea''. <SUP>[[[#Note| | + | Di seguito mostreremo un possibile codice per ruotare un Pentagono regolare disegnato in una ''DrawingArea''. <SUP>[[[#Note|nota 1]]]</sup> |
I 5 vertici del Pentagono regolare sono stati correttamente individuati sulla circonferenza ideale, alla quale detti vertici appartengono, dividendo l'angolo di 360° per il numero dei vertici del Pentagono (360° / 5 = 72°). Ogni vertice si troverà sulla circonferenza ogni 72 gradi. | I 5 vertici del Pentagono regolare sono stati correttamente individuati sulla circonferenza ideale, alla quale detti vertici appartengono, dividendo l'angolo di 360° per il numero dei vertici del Pentagono (360° / 5 = 72°). Ogni vertice si troverà sulla circonferenza ogni 72 gradi. | ||
Riga 14: | Riga 14: | ||
Private coord As Single[] | Private coord As Single[] | ||
Private pol As New 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] | ||
Riga 33: | Riga 35: | ||
coord = [DrawingArea1.W / 2, DrawingArea1.H / 2] | coord = [DrawingArea1.W / 2, DrawingArea1.H / 2] | ||
− | For t As Short = | + | For t As Short = -20 To 340 Step 360 / PENTAGONO |
pol.Push(RAGGIO * Cos(Rad(t))) | pol.Push(RAGGIO * Cos(Rad(t))) | ||
pol.Push(RAGGIO * Sin(Rad(t))) | pol.Push(RAGGIO * Sin(Rad(t))) | ||
Riga 39: | Riga 41: | ||
<FONT Color=gray>' ''I valori "- 6" e "- 8" aggiustano rispettivamente in orizzontale e in verticale il punto di rotazione del Pentagono:''</font> | <FONT Color=gray>' ''I valori "- 6" e "- 8" aggiustano rispettivamente in orizzontale e in verticale il punto di rotazione del Pentagono:''</font> | ||
− | xs = (coord[0] + coord[0] + pol[2] + coord[0] + Abs(pol[4]) + coord[0] - Abs(pol[6]) + coord[0] - pol[8]) / PENTAGONO | + | xs = (coord[0] + coord[0] + pol[2] + coord[0] + Abs(pol[4]) + coord[0] - Abs(pol[6]) + coord[0] - pol[8] - ((30 * RAGGIO) / 100)) / PENTAGONO |
− | ys = (coord[1] + coord[1] + pol[3] + coord[1] + pol[5] + coord[1] + pol[7] + coord[1] + pol[9]) / PENTAGONO | + | ys = (coord[1] + coord[1] + pol[3] + coord[1] + pol[5] + coord[1] + pol[7] + coord[1] + pol[9] - ((26.66 * RAGGIO) / 100)) / PENTAGONO |
End | End | ||
Riga 59: | Riga 61: | ||
.Arc(coord[0], coord[1], 2, Rad(0), Rad(360), False) | .Arc(coord[0], coord[1], 2, Rad(0), Rad(360), False) | ||
.Fill | .Fill | ||
+ | .Brush = .LinearGradient(0, 20, 15, 0, c, f) | ||
.Translate(coord[0], coord[1]) | .Translate(coord[0], coord[1]) | ||
− | .Rotate(Rad(an)) | + | .Rotate(Rad(<FONT Color=#B22222><B>-</b></font>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]) | .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]) | ||
.Stroke | .Stroke |
Versione delle 05:07, 22 ott 2023
Di seguito mostreremo un possibile codice per ruotare un Pentagono regolare disegnato in una DrawingArea. [nota 1]
I 5 vertici del Pentagono regolare sono stati correttamente individuati sulla circonferenza ideale, alla quale detti vertici appartengono, dividendo l'angolo di 360° per il numero dei vertici del Pentagono (360° / 5 = 72°). Ogni vertice si troverà sulla circonferenza ogni 72 gradi.
E' possibile variare la dimensione del Pentagono, modificando il raggio della circonferenza, sulla quale i suoi vertici insistono.
La rotazione del pentagono 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 PENTAGONO As Byte = 5 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 = -20 To 340 Step 360 / PENTAGONO pol.Push(RAGGIO * Cos(Rad(t))) pol.Push(RAGGIO * Sin(Rad(t))) Next ' I valori "- 6" e "- 8" aggiustano rispettivamente in orizzontale e in verticale il punto di rotazione del Pentagono: xs = (coord[0] + coord[0] + pol[2] + coord[0] + Abs(pol[4]) + coord[0] - Abs(pol[6]) + coord[0] - pol[8] - ((30 * RAGGIO) / 100)) / PENTAGONO ys = (coord[1] + coord[1] + pol[3] + coord[1] + pol[5] + coord[1] + pol[7] + coord[1] + pol[9] - ((26.66 * RAGGIO) / 100)) / PENTAGONO 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]) .Stroke .End End With End
Note
[1] Vedere anche la seguente pagina: Ruotare un triangolo in una DrawingArea