Page 1 sur 1

Rotation rectangle

Publié : jeu. 18/sept./2014 13:48
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

Re: Rotation rectangle

Publié : jeu. 18/sept./2014 20:17
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

Re: Rotation rectangle

Publié : jeu. 18/sept./2014 22:10
par falsam
Pas mal ce code Demivec :)

Re: Rotation rectangle

Publié : ven. 19/sept./2014 9:45
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". :)

Re: Rotation rectangle

Publié : ven. 19/sept./2014 10:02
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



Re: Rotation rectangle

Publié : sam. 20/sept./2014 10:02
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+

Re: Rotation rectangle

Publié : sam. 20/sept./2014 11:04
par microdevweb
Merci à tous pour ce coup de pouce...