Ruotare un prisma in una DrawingArea
Vai alla navigazione
Vai alla ricerca
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