forme géométrique : astroide

Programmation d'applications complexes
manababel
Messages : 136
Inscription : jeu. 14/mai/2020 7:40

forme géométrique : astroide

Message par manababel »

dessine une forme du type 'astroîde' et un peu plus.

ref : https://mathcurve.com/courbes2d/astroid/astroid.shtml

Code : Tout sélectionner

Procedure astroide(cx,cy,rayon,ang.f,nb,col=$ffffff)
   
  d=rayon>>2
  t.f=0
  z2.f=(nb*t)
  x1=d*(3*Cos(Radian(t+ang))+(Cos(Radian(z2+ang))))+cx
  y1=d*(3*Sin(Radian(t+ang))-(Sin(Radian(z2+ang))))+cy
  x0=x1
  y0=y1
  compt.f=(360/rayon/2)
  If compt<0.5:compt=0.5:EndIf
  
    While t<=360
      z2.f=(nb*t)
      x2.f=Cos(Radian(t+ang))
      y2.f=Sin(Radian(t+ang))
      x=d*(3*x2+Cos(Radian(z2+ang)))+cx
      y=d*(3*y2-Sin(Radian(z2+ang)))+cy
      LineXY(x,y,x1,y1,col)
      x1=x
      y1=y
      t=t+compt
    Wend
    LineXY(x,y,x0,y0,col)
  
EndProcedure

If OpenWindow(0, 0, 0, 1000, 800, "astroide", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  
  ii=0
  Repeat
    
   Event = WindowEvent()   

    a.f=ii/1
    
    StartDrawing(WindowOutput(0))
    Box(0,0,999,799,0)
    astroide(500,400,400,-a,3,$ff)   
    nb=0
    For y=0 To 800 Step 150
      For x=0 To 1000 Step 150
        astroide(x+50,y,50,a,nb,$ffffff)
        nb=Mod(nb+1, 10)
      Next
    Next

    StopDrawing()
   Delay(15)
    ii=ii+1 
    ii=Mod(ii,3600)
  Until Event = #PB_Event_CloseWindow Or quit=1

CloseWindow(0)

EndIf
Avatar de l’utilisateur
kernadec
Messages : 1594
Inscription : ven. 25/avr./2008 11:14

Re: forme géométrique : astroide

Message par kernadec »

bjr manababel
Excellent, j'adore merci pour le partage :D
merci pour le lien il est top :D
cordialement
vurvur
Messages : 60
Inscription : ven. 29/août/2014 19:52

Re: forme géométrique : astroide

Message par vurvur »

Merci pour ton code.
Pour plus de fluidité:

Code : Tout sélectionner

InitSprite()

Procedure astroide(cx,cy,rayon,ang.f,nb,col=$ffffff)
  
  d=rayon>>2
  t.f=0
  z2.f=(nb*t)
  x1=d*(3*Cos(Radian(t+ang))+(Cos(Radian(z2+ang))))+cx
  y1=d*(3*Sin(Radian(t+ang))-(Sin(Radian(z2+ang))))+cy
  x0=x1
  y0=y1
  compt.f=(360/rayon/2)
  If compt<0.5:compt=0.5:EndIf
  
  While t<=360
    z2.f=(nb*t)
    x2.f=Cos(Radian(t+ang))
    y2.f=Sin(Radian(t+ang))
    x=d*(3*x2+Cos(Radian(z2+ang)))+cx
    y=d*(3*y2-Sin(Radian(z2+ang)))+cy
    LineXY(x,y,x1,y1,col)
    x1=x
    y1=y
    t=t+compt
  Wend
  LineXY(x,y,x0,y0,col)
  
EndProcedure

OpenWindow(0, 0, 0, 1000, 800, "astroide", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0)+1,WindowHeight(0)+1,1,0,0) 


ii=0
SetFrameRate(30)
Repeat
  
  Repeat : e = WindowEvent() : if e = #PB_Event_CloseWindow : end : endif : Until e = 0
  
  a.f=ii/1
  StartDrawing(ScreenOutput())
  Box(0,0,999,799,0)
  astroide(500,400,400,-a,3,$ff)   
  nb=0
  For y=0 To 800 Step 150
    For x=0 To 1000 Step 150
      astroide(x+50,y,50,a,nb,$ffffff)
      nb=Mod(nb+1, 10)
    Next
  Next
  
  StopDrawing()
  Delay(20)
  ii=ii+1 
  ii=Mod(ii,3600)
  
  FlipBuffers()
ForEver
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: forme géométrique : astroide

Message par falsam »

@manababel : Merci pour ce partage. Par contre ca clignote pas mal.

Une solution pour palier à ce phénomène consiste à créer l'image du résultat puis de l'afficher.

j'ai appliquer une petite correction en ce sens à ton code.

Code : Tout sélectionner

Define Image
Procedure astroide(cx,cy,rayon,ang.f,nb,col=$ffffff)
  
  d=rayon>>2
  t.f=0
  z2.f=(nb*t)
  x1=d*(3*Cos(Radian(t+ang))+(Cos(Radian(z2+ang))))+cx
  y1=d*(3*Sin(Radian(t+ang))-(Sin(Radian(z2+ang))))+cy
  x0=x1
  y0=y1
  compt.f=(360/rayon/2)
  If compt<0.5:compt=0.5:EndIf
  
  While t<=360
    z2.f=(nb*t)
    x2.f=Cos(Radian(t+ang))
    y2.f=Sin(Radian(t+ang))
    x=d*(3*x2+Cos(Radian(z2+ang)))+cx
    y=d*(3*y2-Sin(Radian(z2+ang)))+cy
    LineXY(x,y,x1,y1,col)
    x1=x
    y1=y
    t=t+compt
  Wend
  LineXY(x,y,x0,y0,col)
  
EndProcedure

If OpenWindow(0, 0, 0, 1000, 800, "astroide", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  Image = CreateImage(#PB_Any, 1000, 800)
  
  ii=0
  Repeat
    
    Event = WindowEvent()  
    
    a.f=ii/1
    
    ; Creation de l'image du résultat
    StartDrawing(ImageOutput(Image))
    Box(0,0,999,799,0)
    astroide(500,400,400,-a,3,$ff)   
    nb=0
    For y=0 To 800 Step 150
      For x=0 To 1000 Step 150
        astroide(x+50,y,50,a,nb,$ffffff)
        nb=Mod(nb+1, 10)
      Next
    Next
    
    StopDrawing()
    ;Delay(15)
    ii=ii+1
    ii=Mod(ii,3600)
    
    ; Affichage de l'image
    StartDrawing(WindowOutput(0))
    DrawImage(ImageID(Image), 0, 0)
    StopDrawing()
    
  Until Event = #PB_Event_CloseWindow Or quit=1
  
  CloseWindow(0)
  
EndIf
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
manababel
Messages : 136
Inscription : jeu. 14/mai/2020 7:40

Re: forme géométrique : astroide

Message par manababel »

Merci pour les modifications.

Je vais poster une autre fonction de ce type mais un peu plus gourmande en ressources.
je dessine tout dans une image comme la version de Falsam, mais l'image cligne encore.
De plus , j'essaie de trouver un moyen pour quitter le programme dès que l'on appuie sur la touche "escape".
Mesa
Messages : 1098
Inscription : mer. 14/sept./2011 16:59

Re: forme géométrique : astroide

Message par Mesa »

Sur mon pc, l'utilisation des sprites prend 2.5 fois moins de ressources processeur, ce qui est normal car les sprites utilisent les ressources de la carte graphique en priorité.

Code : Tout sélectionner

InitSprite() 
Global ppi.f=2*#PI

Procedure astroide(cx,cy,rayon,ang.f,nb,col=$ffffff, Stepp.f=0.08) 
  
  ;stepp=0.5/ppi
  
  d=rayon>>2 
  t.f=0 
  z2.f=(nb*t) 
  x1=d*(3*Cos(t+ang)+(Cos(z2+ang)))+cx 
  y1=d*(3*Sin(t+ang)-(Sin(z2+ang)))+cy 
  x0=x1 
  y0=y1 
  compt.f=(#PI/rayon) 
  If compt<Stepp:compt=stepp:EndIf 
  
  While t<=ppi 
    z2.f=(nb*t) 
    x2.f=Cos(t+ang) 
    y2.f=Sin(t+ang) 
    x=d*(3*x2+Cos(z2+ang))+cx 
    y=d*(3*y2-Sin(z2+ang))+cy 
    LineXY(x,y,x1,y1,col) 
    x1=x 
    y1=y 
    t=t+compt 
  Wend 
  LineXY(x,y,x0,y0,col) 
  
EndProcedure 

OpenWindow(0, 0, 0, 1000, 800, "astroide", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget) 
OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0)+1,WindowHeight(0)+1,1,0,0)  

Image = CreateSprite(#PB_Any, 1000, 800)
ii=0 
; 	SetFrameRate(60) 
Repeat 
  
  Repeat : e = WindowEvent() : If e = #PB_Event_CloseWindow : End : EndIf : Until e = 0 
  
  ; 		a.f=ii/ppi 
  a.f=ii/1; plus rapide
  StartDrawing(SpriteOutput(Image)) 
  Box(0,0,999,799,0) 
  astroide(500,400,400,-a,3,$ff)    
  nb=0 
  For y=0 To 800 Step 150 
    For x=0 To 1000 Step 150 
      astroide(x+50,y,50,a,nb,$ffffff) 
      nb=Mod(nb+1, 10) 
    Next 
  Next 
  
  StopDrawing() 
  DisplaySprite(Image, 0, 0)
  
  
  ; 		Delay(50) 
  ii=ii+1  
  ii=Mod(ii,3600) 
  
  FlipBuffers() 
ForEver 
Mesa.
manababel
Messages : 136
Inscription : jeu. 14/mai/2020 7:40

Re: forme géométrique : astroide

Message par manababel »

Effectivement , l'utilisation des "sprites" est nettement plus rapide.
j'essaierais d'utiliser plus souvent cette méthode.

Merci.
Répondre