Je vous fourni la mienne, pour ceux qui en ont besoin. La première version est semie optimisée, mais surtout, très compacte, claire et lisible. Facile à adapter et à passer en procédure. Il suffit de rempli les tableaux X() et Y() avec les points qu'on veux.
Code : Tout sélectionner
#Window = 0
#Width = 640
#Height = 480
#Timer = 0
;* Initialisation de DirectX *;
If InitSprite() = 0 Or InitKeyboard() = 0
MessageRequester("Erreur", "Impossible d'initialiser DirectX", 0)
CloseWindow(#Window) : End
EndIf
;* Ouverture de la fenêtre et de l'écran *;
hwnd = OpenWindow(#Window, 0, 0, #Width, #Height, #PB_Window_TitleBar | #PB_Window_ScreenCentered, "")
OpenWindowedScreen(hwnd, 0, 0, #Width, #Height, 0, 0, 0)
ClearScreen(0, 0, 0)
;*********************************************;
;* 3 pixels au pif *;
Dim X(2) : Dim Y(2)
For i=0 To 2: X(i)=Random(#Width-1) : Y(i)=Random(#Height-1) : Next
;* On tri les points (cette partie n'est pas optimale, mais ça suffira bien) *;
T=65535 : B=0 : BI=0
For i=0 To 2 : Y=Y(i) : If Y<T : T=Y : BI=i : EndIf : If Y>B : B=Y : EndIf : Next
XA.f=X(BI) : Y1=Y(BI) : BI=BI+1 : If BI=3 : BI=0 : EndIf
X2.f=X(BI) : Y2=Y(BI) : BI=BI+1 : If BI=3 : BI=0 : EndIf
X3.f=X(BI) : Y3=Y(BI) : XB.f=XA
;* On trace *;
PA1.f = (X2-XA)/((Y2-Y1)) : PB1.f = (X3-XA)/((Y3-Y1)) : PC.f = (X3-X2)/((Y3-Y2))
If Y2<Y3 : HALF=Y2 : PA2.f=PC : PB2.f=PB1 : Else : HALF=Y3: PA2.f=PA1 : PB2.f=PC : EndIf
If Y1=Y2 : XA = X2: EndIf ;* cas particulier à ne pas oublier *;
If Y1=Y3 : XB = X3: EndIf ;* cas particulier à ne pas oublier *;
StartDrawing(ScreenOutput())
For i=T To HALF-1 : LineXY(XA, i, XB, i, 16777215)
XA=XA+PA1 : XB=XB+PB1 : Next
For i=HALF To B : LineXY(XA, i, XB, i, 16777215)
XA=XA+PA2 : XB=XB+PB2 : Next
StopDrawing()
FlipBuffers()
;*********************************************;
;* On quite ! *;
;End
Repeat
ExamineKeyboard()
While WindowEvent() : Wend
If KeyboardPushed(#PB_Key_Escape) : quit = 1 : EndIf
Until quit = 1
End
Et voici la deuxième version, qui est en fait la meme, mais optimisée, sans les DIM que PB ne semble pas trop apprécier. Elle est plus longue mais nettement plus rapide, et encore un peu optimisable, mais j'ai préféré ne pas aller plus loin sinon après ça ne ressemble plus à rien
Code : Tout sélectionner
#Window = 0
#Width = 640
#Height = 480
#Timer = 0
;* Initialisation de DirectX *;
If InitSprite() = 0 Or InitKeyboard() = 0
MessageRequester("Erreur", "Impossible d'initialiser DirectX", 0)
CloseWindow(#Window) : End
EndIf
;* Ouverture de la fenêtre et de l'écran *;
hwnd = OpenWindow(#Window, 0, 0, #Width, #Height, #PB_Window_TitleBar | #PB_Window_ScreenCentered, "")
OpenWindowedScreen(hwnd, 0, 0, #Width, #Height, 0, 0, 0)
ClearScreen(0, 0, 0)
StartDrawing(ScreenOutput())
;********************************;
;* 3 pixels au pif *;
X0=Random(#Width-1)
Y0=Random(#Height-1)
X1=Random(#Width-1)
Y1=Random(#Height-1)
X2=Random(#Width-1)
Y2=Random(#Height-1)
;* Recherche du point le plus haut et le plus bat *;
T=Y0 : B=Y0 : CAS=0
If Y1<T : T=Y1 : CAS=1 : EndIf : If Y1>B : B=Y1 : EndIf
If Y2<T : T=Y2 : CAS=2 : EndIf : If Y2>B : B=Y2 : EndIf
;* Calcul des pentes dans les 3 cas possibles *;
If CAS=0
XA.f=X0 : XB.f=X0
PA1.f = (X1-X0)/(Y1-Y0) : PB1.f = (X2-X0)/(Y2-Y0) : PC.f = (X2-X1)/(Y2-Y1)
If Y1<Y2 : HALF=Y1 : PA2.f=PC : PB2.f=PB1 : Else : HALF=Y2: PA2.f=PA1 : PB2.f=PC : EndIf
If Y0=Y1 : XA = X1: EndIf ;* cas particulier à ne pas oublier *;
If Y0=Y2 : XB = X2: EndIf ;* cas particulier à ne pas oublier *;
EndIf
If CAS=1
XA.f=X1 : XB.f=X1
PA1.f = (X2-X1)/(Y2-Y1) : PB1.f = (X0-X1)/(Y0-Y1) : PC.f = (X0-X2)/(Y0-Y2)
If Y2<Y0 : HALF=Y2 : PA2.f=PC : PB2.f=PB1 : Else : HALF=Y0: PA2.f=PA1 : PB2.f=PC : EndIf
If Y1=Y2 : XA = X2: EndIf ;* cas particulier à ne pas oublier *;
If Y1=Y0 : XB = X0: EndIf ;* cas particulier à ne pas oublier *;
EndIf
If CAS=2
XA.f=X2 : XB.f=X2
PA1.f = (X0-X2)/(Y0-Y2) : PB1.f = (X1-X2)/(Y1-Y2) : PC.f = (X1-X0)/(Y1-Y0)
If Y0<Y1 : HALF=Y0 : PA2.f=PC : PB2.f=PB1 : Else : HALF=Y1: PA2.f=PA1 : PB2.f=PC : EndIf
If Y2=Y0 : XA = X0: EndIf ;* cas particulier à ne pas oublier *;
If Y2=Y1 : XB = X1: EndIf ;* cas particulier à ne pas oublier *;
EndIf
;* Traçage *;
For i=T To HALF-1 : LineXY(XA, i, XB, i, 16777215)
XA=XA+PA1 : XB=XB+PB1 : Next
For i=HALF To B : LineXY(XA, i, XB, i, 16777215)
XA=XA+PA2 : XB=XB+PB2 : Next
;********************************;
;* On quite ! *;
StopDrawing()
FlipBuffers()
Repeat
ExamineKeyboard()
While WindowEvent() : Wend
If KeyboardPushed(#PB_Key_Escape) : quit = 1 : EndIf
Until quit = 1
End
En fait j'ai fais ça en 20 minutes suite à une mise au défit par Psycode... A vous de juger

C'est mon premier programme en PB qu'hier soir encore je ne connaissais pas, habituellement je programme pas en basic, mais en tout cas celui ci est prometeur
