Rotation rectangle

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
microdevweb
Messages : 1802
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Rotation rectangle

Message par microdevweb »

Bonjour à tous,

J'essaye de dessiner un rectangle avec une rotation (l'idéal serait même de pouvoir changer le centre de rotation) ci-dessous ce que j'ai tenté, mais ce n'est pas tout à fait cela

Merci de votre aide

Code : Tout sélectionner

OpenWindow(0,0,0,800,600,"Teste")
CanvasGadget(0,0,0,800,600)
StartDrawing(CanvasOutput(0))
angle=40
ax=100
W=100
H=100
ay=100
bx=ax+(Cos(Radian(angle))*W)
by=ay+(Sin(Radian(angle))*H)
cx=ax-(Sin(Radian(angle))*H)
dx=bx-(Sin(Radian(angle))*H)
cy=(ay+H)-(Cos(Radian(angle))*W)
dy=(by+H)-(Cos(Radian(angle))*W)
Box(ax,ay,W,H,RGB(255,255,139))
LineXY(ax,ay,bx,by,RGB(0,0,0))
LineXY(cx,cy,dx,dy,RGB(0,0,0))
LineXY(ax,ay,cx,cy,RGB(0,0,0))
LineXY(bx,by,dx,dy,RGB(0,0,0))
StopDrawing()
Repeat
      
Until WaitWindowEvent()=#PB_Event_CloseWindow
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Demivec
Messages : 91
Inscription : sam. 18/sept./2010 18:13

Re: Rotation rectangle

Message par Demivec »

Voilà:

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


Define ax, ay, w, h, cx, cy

OpenWindow(0, 0, 0, 800, 600, "Teste") 
CanvasGadget(0, 0, 0,800, 600) 
StartDrawing(CanvasOutput(0)) 
  ax=300: ay=200 
  W=100: H=100 
  Box(ax, ay, W, H, RGB(255, 255, 139)) 

  ;tourner autour du centre
  cx = ax + w / 2: cy = ay + h / 2
  drawRotatedSquare(ax, ay, W, H, cx, cy, -30, RGB(0, 0, 0))
  drawRotatedSquare(ax, ay, W, H, cx, cy, -60, RGB(0, 0, 0))
  drawRotatedSquare(ax, ay, W, H, cx, cy, -90, RGB(0, 0, 0))
  
  ;tourner autour du coin supérieur gauche
  cx = ax: cy = ay
  drawRotatedSquare(ax, ay, W, H, cx, cy, -30, RGB(0, 255, 0))
  drawRotatedSquare(ax, ay, W, H, cx, cy, -60, RGB(0, 255, 0))
  drawRotatedSquare(ax, ay, W, H, cx, cy, -90, RGB(0, 255, 0))
  
  ;tourner autour du coin en haut à droite
  cx = ax + w: cy = ay
  drawRotatedSquare(ax, ay, W, H, cx, cy, -30, RGB(255, 0, 0))
  drawRotatedSquare(ax, ay, W, H, cx, cy, -60, RGB(255, 0, 0))
  drawRotatedSquare(ax, ay, W, H, cx, cy, -90, RGB(255, 0, 0))
  
  ;tourner autour du coin en bas à droite
  cx = ax + w: cy = ay + h
  drawRotatedSquare(ax, ay, W, H, cx, cy, -30, RGB(0, 0, 255))
  drawRotatedSquare(ax, ay, W, H, cx, cy, -60, RGB(0, 0, 255))
  drawRotatedSquare(ax, ay, W, H, cx, cy, -90, RGB(0, 0, 255))
  
  ;tourner autour du coin en bas à gauche
  cx = ax: cy = ay + h
  drawRotatedSquare(ax, ay, W, H, cx, cy, -30, RGB(255, 0, 255))
  drawRotatedSquare(ax, ay, W, H, cx, cy, -60, RGB(255, 0, 255))
  drawRotatedSquare(ax, ay, W, H, cx, cy, -90, RGB(255, 0, 255))
StopDrawing() 
  
Repeat: Until WaitWindowEvent () = # PB_Event_CloseWindow
Avatar de l’utilisateur
falsam
Messages : 7324
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Rotation rectangle

Message par falsam »

Pas mal ce code Demivec :)
Configuration : Windows 11 Famille 64-bit - PB 6.20 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Re: Rotation rectangle

Message par djes »

Juste un bémol, surtout pour cx et cy, il vaut mieux travailler avec des flottants plutôt que des entiers, le résultat sera plus "smooth". :)
PAPIPP
Messages : 534
Inscription : sam. 23/févr./2008 17:58

Re: Rotation rectangle

Message par PAPIPP »

Bonjour à tous

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


Il est fort peu probable que les mêmes causes ne produisent pas les mêmes effets.(Einstein)
Et en logique positive cela donne.
Il est très fortement probable que les mêmes causes produisent les mêmes effets.
PAPIPP
Messages : 534
Inscription : sam. 23/févr./2008 17:58

Re: Rotation rectangle

Message par PAPIPP »

Bonjour à tous

J'ai un peu modifié le prg précédent.
J'ai matérialisé le centre de rotation par un cercle de 5 pixels de rayon et de couleur rouge.
A+
Il est fort peu probable que les mêmes causes ne produisent pas les mêmes effets.(Einstein)
Et en logique positive cela donne.
Il est très fortement probable que les mêmes causes produisent les mêmes effets.
Avatar de l’utilisateur
microdevweb
Messages : 1802
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: Rotation rectangle

Message par microdevweb »

Merci à tous pour ce coup de pouce...
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Répondre