je viens d'adapter pour mes besoins un code VB de dessin d'un cube en rotation avec matrice
voici le code pour le Fun, Avec un ptit clin d' œil à la fête de noël qui approche
Cordialement
Code : Tout sélectionner
;##################################################################################################
; http://codes-sources.commentcamarche.net/source/31525-rotation-dans-l-espace-3d-2d
; Code Visual basic 5 en 2005 , auteur: cs_Stephane
; Adapté en PB 5.00 par kernadec - Nov 2014
;##################################################################################################
Structure Point3D
X.d
Y.d
Z.d
EndStructure
Global Dim cube.Point3D(8)
Global Dim trace.Point3D(8)
Global Dim col(8)
Global Coordonnee.Point3D
Global R.Point3D
Global Rot.Point3D
Global Zoom.d = 100
Pos1.Point3D
Pos2.Point3D
Procedure Pos_Point(X.d, Y.d, Z.d)
Coordonnee\X = (Zoom + 50 * X)
Coordonnee\Y = (Zoom * Y)
Coordonnee\Z = (Zoom * Z)
EndProcedure
Procedure Rotation()
Rot\X = Coordonnee\X * ( Cos(R\Z) * Cos(R\Y)) + Coordonnee\Y * ( Sin(R\Z) * Cos(R\X) - Cos(R\Z) * Sin(R\Y) * Sin(R\X)) + Coordonnee\Z * (Sin(R\Z) * Sin(R\X) + Cos(R\Z) * Sin(R\Y) * Cos(R\X))
Rot\Y = Coordonnee\X * (-Sin(R\Z) * Cos(R\Y)) + Coordonnee\Y * ( Cos(R\Z) * Cos(R\X) + Sin(R\Z) * Sin(R\Y) * Sin(R\X)) + Coordonnee\Z * (Cos(R\Z) * Sin(R\X) + Sin(R\Z) * Sin(R\Y) * Cos(R\X))
Rot\Z = Coordonnee\X * (-Sin(R\Y)) + Coordonnee\Y * (-Cos(R\Y) * Sin(R\X)) + Coordonnee\Z * (Cos(R\Y) * Cos(R\X))
EndProcedure
Procedure tableau()
Pos_Point(1, 1, 1)
cube(1) = Coordonnee
Pos_Point(1, -1, 1)
cube(2) = Coordonnee
Pos_Point(-1, -1, 1)
cube(3) = Coordonnee
Pos_Point(-1, 1, 1)
cube(4) = Coordonnee
Pos_Point(-1, 1, -1)
cube(5) = Coordonnee
Pos_Point(-1, -1, -1)
cube(6) = Coordonnee
Pos_Point(1, -1, -1)
cube(7) = Coordonnee
Pos_Point(1, 1, -1)
cube(8) = Coordonnee
EndProcedure
If OpenWindow(0, 0, 0, 500,500, "Cube 3D Rotation", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(0, 10, 10, 480, 480,#PB_Canvas_DrawFocus|#PB_Canvas_Keyboard)
AxeX.l = 480 / 2
AxeY.l = 480 / 2
AxeZ.l = 480 / 2
Angle.d = 0
Pas.d = Radian(10)
SetActiveGadget(0)
Repeat
event = WindowEvent()
Delay(120) ; timer
StartDrawing(CanvasOutput(0))
Box(0, 0, 480, 480,RGB(255, 255, 255))
Angle = Mod(Angle + Pas, 360)
R\Z = Angle
R\Y = Angle / 2
R\X = Angle / 3
;Axe X -----------------------------------------------
Pos_Point(1, 0, 0) : Rotation()
Pos1\X = AxeX + Rot\X
Pos1\Z = AxeZ + Rot\Z
Pos_Point(-1, 0, 0) : Rotation()
Pos2\X = AxeX + Rot\X
Pos2\Z = AxeZ + Rot\Z
LineXY(Pos1\X, Pos1\Z, Pos2\X, Pos2\Z, #Blue)
DrawText(Pos2\X, Pos2\Z, "X", #Blue, #White)
;Axe Y ------------------------------------------------
Pos_Point(0, 1, 0) : Rotation()
Pos1\X = AxeX + Rot\X
Pos1\Z = AxeZ + Rot\Z
Pos_Point(0, -1, 0) : Rotation()
Pos2\X = AxeX + Rot\X
Pos2\Z = AxeZ + Rot\Z
LineXY(Pos1\X, Pos1\Z, Pos2\X, Pos2\Z, #Red)
DrawText(Pos2\X, Pos2\Z, "Y", #Red, #White)
;Axe Z ------------------------------------------------
Pos_Point(0, 0, 1) : Rotation()
Pos1\X = AxeX + Rot\X
Pos1\Z = AxeZ + Rot\Z
Pos_Point(0, 0, -1) : Rotation()
Pos2\X = AxeX + Rot\X
Pos2\Z = AxeZ + Rot\Z
LineXY(Pos1\X, Pos1\Z, Pos2\X, Pos2\Z, #Green)
DrawText(Pos2\X, Pos2\Z, "Z", #Green, #White)
;------------------------------------------------
tableau()
For i = 1 To 8
Coordonnee = cube(i)
Rotation()
Pos1\X = AxeX + Rot\X
Pos1\Z = AxeZ + Rot\Z
col(i) = RGB(Random(255),Random(255),Random(255))
Circle(Pos1\X, Pos1\Z, 10, col(i))
trace(i)\X = Pos1\X
trace(i)\Y = Pos1\Y
trace(i)\Z = Pos1\Z
Next
;Aretes du Cube
LineXY(trace(1)\X, trace(1)\Z, trace(2)\X, trace(2)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(2)\X, trace(2)\Z, trace(3)\X, trace(3)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(3)\X, trace(3)\Z, trace(4)\X, trace(4)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(4)\X, trace(4)\Z, trace(1)\X, trace(1)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(5)\X, trace(5)\Z, trace(6)\X, trace(6)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(6)\X, trace(6)\Z, trace(7)\X, trace(7)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(7)\X, trace(7)\Z, trace(8)\X, trace(8)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(8)\X, trace(8)\Z, trace(5)\X, trace(5)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(1)\X, trace(1)\Z, trace(8)\X, trace(8)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(2)\X, trace(2)\Z, trace(7)\X, trace(7)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(3)\X, trace(3)\Z, trace(6)\X, trace(6)\Z, RGB(Random(255),Random(255),Random(255)))
LineXY(trace(4)\X, trace(4)\Z, trace(5)\X, trace(5)\Z, RGB(Random(255),Random(255),Random(255)))
DrawText(trace(1)\X - 4, trace(1)\Z - 8, "1", #White, col(1))
DrawText(trace(2)\X - 4, trace(2)\Z - 8, "2", #White, col(2))
DrawText(trace(3)\X - 4, trace(3)\Z - 8, "3", #White, col(3))
DrawText(trace(4)\X - 4, trace(4)\Z - 8, "4", #White, col(4))
DrawText(trace(5)\X - 4, trace(5)\Z - 8, "5", #White, col(5))
DrawText(trace(6)\X - 4, trace(6)\Z - 8, "6", #White, col(6))
DrawText(trace(7)\X - 4, trace(7)\Z - 8, "7", #White, col(7))
DrawText(trace(8)\X - 4, trace(8)\Z - 8, "8", #White, col(8))
StopDrawing()
Select EventType() ; zoom souris
Case #PB_EventType_MouseWheel
Zoom = Zoom - GetGadgetAttribute(0,#PB_Canvas_WheelDelta)*5
EndSelect
Until event = #PB_Event_CloseWindow
EndIf