Ruotare un prisma in una DrawingArea
Ruotare un Prisma triangolare
Mostreremo un possibile codice per disegnare e ruotare un Prisma triangolare intorno al suo asse Y in una DrawingArea.
Nell'esempio che segue sarà possibile aumentare o diminuire la dimensione del disegno del Prisma, cliccando rispettivamente sui tasti "+" e "-" della tastiera.
Private angleY As Float ' Angolo di rotazione sull'asse Y
Private Zoom As Float = 150 ' Fattore di ingrandimento della proiezione
Private Vertices As Object[] ' Array dei vertici 3D (ogni vertice è un array [x, y, z])
Private Faces As Object[] ' Array delle facce (ogni faccia è un array di indici dei vertici)
Private PerspectiveFactor As Float = 0.5 ' Interpolazione tra isometria (0) e prospettiva (1)
Public Sub Form_Open()
Me.Title = "Prisma triangolare rotante sul suo asse Y"
Me.Move(0, 0, Screen.AvailableWidth, Screen.AvailableHeight)
Me.Arrangement = Arrange.Fill
Vertices = New Object[]
Faces = New Object[]
' === DEFINIZIONE DEI 6 VERTICI DEL PRISMA ===
' Triangolo superiore (z = +1):
Vertices.Push([0, 1, 1]) ' Vertice centrale in alto
Vertices.Push([-1, -1, 1]) ' Vertice in basso a sinistra
Vertices.Push([1, -1, 1]) ' Vertice in basso a destra
' Triangolo inferiore (z = -1):
Vertices.Push([0, 1, -1]) ' Vertice centrale in alto (opposto)
Vertices.Push([-1, -1, -1]) ' Basso sinistra (opposto)
Vertices.Push([1, -1, -1]) ' Basso destra (opposto)
' === DEFINIZIONE DELLE 5 FACCE ===
' Ogni faccia è una lista ordinata di indici (chiusa automaticamente nel disegno):
Faces.Push([0, 1, 2]) ' Faccia triangolare superiore
Faces.Push([3, 5, 4]) ' Faccia triangolare inferiore (ordine invertito per corretto orientamento)
Faces.Push([0, 2, 5, 3]) ' Faccia laterale 1 (rettangolare)
Faces.Push([2, 1, 4, 5]) ' Faccia laterale 2
Faces.Push([1, 0, 3, 4]) ' Faccia laterale 3
Timer1.Delay = 16
Timer1.Start
End
Public Sub Timer1_Timer()
angleY += 0.03
DrawingArea1.Refresh
End
Public Sub DrawingArea1_Draw()
' Centro della DrawingArea:
Dim w As Integer = DrawingArea1.W
Dim h As Integer = DrawingArea1.H
Dim cx As Float = w / 2
Dim cy As Float = h / 2
Dim points As Object[] = New Object[]
' === PROIEZIONE 3D → 2D PER OGNI VERTICE ===
For Each v As Float[] In Vertices
Dim x, y, z As Float
x = v[0]
y = v[1]
z = v[2]
' Rotazione attorno all'asse Y:
Dim x1 As Float = x * Cos(angleY) + z * Sin(angleY)
Dim z1 As Float = -x * Sin(angleY) + z * Cos(angleY)
x = x1
z = z1
' Proiezione prospettica interpolata:
If (z + 3) < 0.1 Then
points.Push([cx, cy]) ' Punto "di sicurezza"
Continue
Endif
Dim scale As Float = Zoom / (1 + PerspectiveFactor * (z + 3))
Dim sx As Float = cx + x * scale
Dim sy As Float = cy - y * scale
points.Push([sx, sy]) ' Salva le coordinate 2D proiettate
Next
' === DISEGNO DELLE FACCE ===
Paint.Brush = Paint.Color(Color.Black)
For Each f As Integer[] In Faces
Dim i As Integer
For i = 0 To f.Count - 1
Dim p1 As Float[] = points[f[i]]
Dim p2 As Float[] = points[(f[(i + 1) Mod f.Count])]
Paint.MoveTo(p1[0], p1[1])
Paint.LineTo(p2[0], p2[1])
Paint.Stroke
Next
Next
Paint.End
End
Public Sub Form_KeyPress() ' Zoom controllato da tastiera
Select Case Key.Code
Case Key["+"]
Zoom = Min(Zoom + 25, 1000) ' Ingrandisce
Case Key["-"]
Zoom = Max(Zoom - 25, 25) ' Rimpicciolisce
End Select
DrawingArea1.Refresh
End
Far ruotare il Prisma solo sul suo asse Z
Per far ruotare il Prisma solo sul suo asse Z, sostituire queste righe:
' Rotazione attorno all'asse Y: Dim x1 As Float = x * Cos(angleY) + z * Sin(angleY) Dim z1 As Float = -x * Sin(angleY) + z * Cos(angleY) x = x1 z = z1
con le seguenti:
' Rotazione attorno all'asse Z: Dim x1 As Float = x * Cos(angleY) - y * Sin(angleY) Dim y1 As Float = x * Sin(angleY) + y * Cos(angleY) x = x1 y = y1
Ruotare un Prisma pentagonale intorno al suo asse Y
Mostreremo un possibile codice per disegnare e ruotare un Prisma pentagonale intorno al suo asse Y in una DrawingArea.
Nell'esempio che segue sarà possibile aumentare o diminuire la dimensione del disegno del Prisma, cliccando rispettivamente sui tasti "+" e "-" della tastiera.
Private angleY As Float ' Angolo di rotazione sull'asse Y (incrementato nel tempo) Private Zoom As Float = 150 ' Fattore di ingrandimento della scena 3D Private Vertices As Object[] ' Lista dei vertici 3D del prisma Private Faces As Object[] ' Lista delle facce (ogni faccia è una lista di indici dei vertici) Private PerspectiveFactor As Float = 0.5 ' 0 = proiezione isometrica, 1 = prospettiva piena
' === INIZIALIZZAZIONE DELLA FINESTRA E CREAZIONE DEL PRISMA === Public Sub Form_Open()
Me.Title = "Prisma pentagonale rotante" Me.Move(0, 0, Screen.AvailableWidth, Screen.AvailableHeight) Me.Arrangement = Arrange.Fill
Vertices = New Object[] Faces = New Object[]
' === COSTRUZIONE DEI VERTICI DEL PRISMA === Dim i As Integer Dim angle As Float Dim r As Float = 1.0 ' Raggio del pentagono (distanza dal centro) Dim h As Float = 1.5 ' Metà altezza del prisma (in asse Z, da -h a +h)
' Pentagono superiore (z = +h) For i = 0 To 4 angle = i * 2 * Pi / 5 Vertices.Push([r * Cos(angle), r * Sin(angle), h]) Next
' Pentagono inferiore (z = -h) For i = 0 To 4 angle = i * 2 * Pi / 5 Vertices.Push([r * Cos(angle), r * Sin(angle), -h]) Next
' === DEFINIZIONE DELLE FACCE ===
' Faccia superiore (pentagono) → vertici da 0 a 4 Faces.Push([0, 1, 2, 3, 4])
' Faccia inferiore (invertita per orientamento corretto) → vertici da 5 a 9 Faces.Push([9, 8, 7, 6, 5])
' Facce laterali rettangolari: ogni lato collega un vertice superiore a quello inferiore For i = 0 To 4 Dim nextI As Integer = (i + 1) Mod 5 Faces.Push([i, nextI, nextI + 5, i + 5]) Next
' Imposta il timer per animare la rotazione (~60 FPS) Timer1.Delay = 16 Timer1.Start
End
' === TIMER: incrementa l’angolo e forza il ridisegno === Public Sub Timer1_Timer()
angleY += 0.025 DrawingArea1.Refresh
End
' === DISEGNO DELL’OGGETTO NELLA DRAWINGAREA === Public Sub DrawingArea1_Draw()
' Calcola il centro dello schermo Dim w As Integer = DrawingArea1.W Dim h As Integer = DrawingArea1.H Dim cx As Float = w / 2 Dim cy As Float = h / 2
Dim points As Object[] = New Object[] ' Coordinate proiettate 2D
' Sfondo bianco Paint.FillRect(0, 0, w, h, Color.White)
' === TRASFORMAZIONE E PROIEZIONE DEI VERTICI 3D === For Each v As Float[] In Vertices Dim x, y, z As Float x = v[0] y = v[1] z = v[2]
' Rotazione attorno all’asse Y Dim x1 As Float = x * Cos(angleY) + z * Sin(angleY) Dim z1 As Float = -x * Sin(angleY) + z * Cos(angleY) x = x1 z = z1
' Proiezione prospettica interpolata
If (z + 3) < 0.1 Then
points.Push([cx, cy]) ' Punto "di sicurezza" per evitare divisioni per zero
Continue
Endif
Dim scale As Float = Zoom / (1 + PerspectiveFactor * (z + 3)) Dim sx As Float = cx + x * scale Dim sy As Float = cy - y * scale
points.Push([sx, sy]) ' Salva il punto proiettato Next
' === DISEGNA TUTTE LE FACCE COME POLIGONI CHIUSI === Paint.Brush = Paint.Color(Color.Black)
For Each f As Integer[] In Faces
Dim i As Integer
For i = 0 To f.Count - 1
Dim p1 As Float[] = points[f[i]]
Dim p2 As Float[] = points[(f[(i + 1) Mod f.Count])]
Paint.MoveTo(p1[0], p1[1])
Paint.LineTo(p2[0], p2[1])
Paint.Stroke
Next
Next
Paint.End
End
' === GESTIONE DELLA TASTIERA PER ZOOM IN/OUT === Public Sub Form_KeyPress()
Select Case Key.Code
Case Key["+"]
Zoom = Min(Zoom + 25, 1000) ' Ingrandisce l’oggetto
Case Key["-"]
Zoom = Max(Zoom - 25, 25) ' Rimpicciolisce l’oggetto
End Select
DrawingArea1.Refresh
End
Ruotare un Prisma esagonale intorno al suo asse Y
Mostreremo un possibile codice per disegnare e ruotare un Prisma esagonale intorno al suo asse Y in una DrawingArea.
Nell'esempio che segue sarà possibile aumentare o diminuire la dimensione del disegno del Prisma, cliccando rispettivamente sui tasti "+" e "-" della tastiera.
Private angleY As Float ' Angolo di rotazione sull'asse Y (aumenta nel tempo)
Private Zoom As Float = 150 ' Fattore di ingrandimento della scena
Private Vertices As Object[] ' Elenco dei vertici 3D (ogni vertice è un array [x, y, z])
Private Faces As Object[] ' Elenco delle facce (ogni faccia è un array di indici)
Private PerspectiveFactor As Float = 0.5 ' 0 = isometria, 1 = prospettiva completa
Public Sub Form_Open()
Me.Title = "Prisma esagonale rotante"
Me.Move(0, 0, Screen.AvailableWidth, Screen.AvailableHeight)
Me.Arrangement = Arrange.Fill
Vertices = New Object[] ' Inizializza la lista dei vertici
Faces = New Object[] ' Inizializza la lista delle facce
' === COSTRUZIONE VERTICI ===
Dim i As Integer
Dim angle As Float
Dim r As Float = 1.0 ' Raggio dell'esagono (distanza dal centro)
Dim h As Float = 1.5 ' Metà altezza del prisma (da -h a +h)
' Esagono superiore (z = +h):
For i = 0 To 5
angle = i * 2 * Pi / 6
Vertices.Push([r * Cos(angle), r * Sin(angle), h])
Next
' Esagono inferiore (z = -h):
For i = 0 To 5
angle = i * 2 * Pi / 6
Vertices.Push([r * Cos(angle), r * Sin(angle), -h])
Next
' === DEFINIZIONE DELLE FACCE ===
' Faccia superiore (esagono):
Faces.Push([0, 1, 2, 3, 4, 5])
' Faccia inferiore (invertita per corretto orientamento visivo):
Faces.Push([11, 10, 9, 8, 7, 6])
' 6 facce laterali rettangolari tra i vertici superiore e inferiore:
For i = 0 To 5
Dim nextI As Integer = (i + 1) Mod 6
Faces.Push([i, nextI, nextI + 6, i + 6])
Next
' Avvia il timer per animare la rotazione (~60 fotogrammi al secondo):
Timer1.Delay = 16
Timer1.Start
End
Public Sub Timer1_Timer()
angleY += 0.025 ' Rotazione continua sull’asse Y
DrawingArea1.Refresh ' Richiede il ridisegno della scena
End
Public Sub DrawingArea1_Draw()
' Centro della DrawingArea (per centrare la proiezione):
Dim w As Integer = DrawingArea1.W
Dim h As Integer = DrawingArea1.H
Dim cx As Float = w / 2
Dim cy As Float = h / 2
Dim points As Object[] = New Object[] ' Lista dei punti proiettati 2D
' === PROIEZIONE 3D → 2D PER OGNI VERTICE ===
For Each v As Float[] In Vertices
Dim x, y, z As Float
x = v[0]
y = v[1]
z = v[2]
' ROTAZIONE SULL'ASSE Y
Dim x1 As Float = x * Cos(angleY) + z * Sin(angleY)
Dim z1 As Float = -x * Sin(angleY) + z * Cos(angleY)
x = x1
z = z1
' PROIEZIONE PROSPETTICA INTERPOLATA
If (z + 3) < 0.1 Then
points.Push([cx, cy]) ' Punto neutro di sicurezza
Continue
Endif
Dim scale As Float = Zoom / (1 + PerspectiveFactor * (z + 3))
Dim sx As Float = cx + x * scale
Dim sy As Float = cy - y * scale
points.Push([sx, sy]) ' Aggiunge punto proiettato alla lista
Next
' === DISEGNO DELLE FACCE ===
Paint.Brush = Paint.Color(Color.Black)
For Each f As Integer[] In Faces
Dim i As Integer
For i = 0 To f.Count - 1
Dim p1 As Float[] = points[f[i]]
Dim p2 As Float[] = points[(f[(i + 1) Mod f.Count])]
Paint.MoveTo(p1[0], p1[1])
Paint.LineTo(p2[0], p2[1])
Paint.Stroke
Next
Next
Paint.End
End
Public Sub Form_KeyPress() ' ZOOM CON TASTIERA
Select Case Key.Code
Case Key["+"]
Zoom = Min(Zoom + 25, 1000) ' Ingrandisce
Case Key["-"]
Zoom = Max(Zoom - 25, 25) ' Rimpicciolisce
End Select
DrawingArea1.Refresh
End