Pour montrer la technique de Demivec voici une rotation dynamique des rectangles.
J'ai matérialisé le centre de rotation par un cercle de 5 pixels de rayon et de couleur rouge.
Code : Tout sélectionner
Procedure rotatePoint(x,y,cx,cy,angle,*point.Point) ;(cx, cy) = pointer à tourner autour (au centre)
*point\x=(x-cx)*Cos(Radian(angle))-(y-cy)*Sin(Radian(angle))+cx
*point\y=(x-cx)*Sin(Radian(angle))+(y-cy)*Cos(Radian(angle))+cy
EndProcedure
Procedure drawRotatedSquare(x,y,w,h,cx,cy,angle,color)
Protected.point point_a,point_b,point_c,point_d
rotatePoint(x,y,cx,cy,angle,point_a)
rotatePoint(x+w,y,cx,cy,angle,point_b)
rotatePoint(x+w,y+h,cx,cy,angle,point_c)
rotatePoint(x,y+h,cx,cy,angle,point_d)
LineXY(point_a\x,point_a\y,point_b\x,point_b\y,color)
LineXY(point_b\x,point_b\y,point_c\x,point_c\y,color)
LineXY(point_c\x,point_c\y,point_d\x,point_d\y,color)
LineXY(point_d\x,point_d\y,point_a\x,point_a\y,color)
EndProcedure
Macro tourne(coul)
For ang=0 To 360
StartDrawing(CanvasOutput(0))
drawRotatedSquare(ax,ay,W,H,cx,cy,ang-1,RGB(255,255,255))
drawRotatedSquare(ax,ay,W,H,cx,cy,ang,coul)
StopDrawing()
Delay(T_AT)
Next
EndMacro
Define ax,ay,w,h,cx,cy
Enumeration
#fen
EndEnumeration
OpenWindow(#fen,0,0,800,600,"Teste",#PB_Window_SystemMenu | #PB_Window_BorderLess)
CanvasGadget(0,0,0,800,600)
StartDrawing(CanvasOutput(0))
ax=325:ay=250
W=150:H=100
T_AT=10
rayon=5
Box(ax,ay,W,H,RGB(255,255,255))
;*******************************************************************************************************************
;tourner autour du centre
cx=ax+w/2:cy=ay+h/2
Circle(CX,CY,rayon,RGB(255,0,0))
DrawText(10,0,"tourne autour du centre "+Space(100),RGB(0,0,0),RGB(255,255,255))
StopDrawing()
tourne(RGB(0,0,0))
;*******************************************************************************************************************
;tourner autour du coin supérieur gauche
StartDrawing(CanvasOutput(0))
; DrawText(cx,cy,"+",RGB(255,255,255),RGB(255,255,255))
Circle(CX,CY,rayon,RGB(255,255,255)) ;efface le cercle précédent
cx=ax:cy=ay
Circle(CX,CY,rayon,RGB(255,0,0)) ; affiche le centre actuel
DrawText(10, 0, "tourner autour du coin supérieur gauche "+Space(100),RGB(0,255,0),RGB(255,255,255))
StopDrawing()
tourne(RGB(0, 255, 0))
;*******************************************************************************************************************
;tourner autour du coin en haut à droite
StartDrawing(CanvasOutput(0))
Circle(CX,CY,rayon,RGB(255,255,255))
cx=ax+w:cy=ay
Circle(CX,CY,rayon,RGB(255,0,0))
DrawText(10,0,"tourner autour du coin en haut à droite "+Space(100),RGB(255,0,0),RGB(255,255,255))
StopDrawing()
tourne(RGB(255,0 , 0))
;*******************************************************************************************************************
;tourner autour du coin en bas à droite
StartDrawing(CanvasOutput(0))
Circle(CX,CY,rayon,RGB(255,255,255))
cx=ax+w:cy=ay+h
Circle(CX,CY,rayon,RGB(255,0,0))
DrawText(10,0,"tourner autour du coin en bas à droite "+Space(100),RGB(0,0,255),RGB(255,255,255))
StopDrawing()
tourne(RGB(0,0 , 255))
;*******************************************************************************************************************
;tourner autour du coin en bas à gauche
StartDrawing(CanvasOutput(0))
Circle(CX,CY,rayon,RGB(255,255,255))
cx=ax:cy=ay+h
Circle(CX,CY,rayon,RGB(255,0,0))
DrawText(10,0,"tourner autour du coin en bas à gauche "+Space(100),RGB(255,0,255),RGB(255,255,255))
StopDrawing()
tourne(RGB(255,0 , 255))
;*******************************************************************************************************************
StartDrawing(CanvasOutput(0))
Circle(CX,CY,rayon,RGB(255,255,255))
cx=Random(600,200):cy=Random(450,150)
Circle(CX,CY,rayon,RGB(255,0,0))
DrawText(10,0,"tourner autour d'un point quelconque x="+Str(cx)+" y="+Str(cy)+Space(100),RGB(0,255,255),RGB(255,255,255))
StopDrawing()
tourne(RGB(0,255, 255))
Repeat: Until WaitWindowEvent () = # PB_Event_CloseWindow