Sollte auch unter Linux und MacOS laufen.
Code: Alles auswählen
Structure clip
X1.l
Y1.l
X2.l
Y2.l
EndStructure
Global Clip.clip
Procedure fill_triangle(X1.l, Y1.l, X2.l, Y2.l, X3.l, Y3.l, C.l)
Define.l X, Y, XA, XB
If Y1 > Y2
fill_triangle(X2, Y2, X1, Y1, X3, Y3, C)
ProcedureReturn
EndIf
If Y2 > Y3
fill_triangle(X1, Y1, X3, Y3, X2, Y2, C)
ProcedureReturn
EndIf
X1 << 3
X2 << 3
X3 << 3
If Y1 < Clip\Y1:Y1 = Clip\Y1:EndIf
If Y3 > Clip\Y2:Y3 = Clip\Y2:EndIf
For Y = Y1 To Y3
If Y <= Y2
If (Y1 = Y2): XA = X2 : Else :XA = X1 + (X2 - X1) * (Y - Y1) / (Y2 - Y1):EndIf
If (Y1 = Y3): XB = X3 : Else :XB = X1 + (X3 - X1) * (Y - Y1) / (Y3 - Y1):EndIf
Else
If (Y3 = Y2) :XA = X2 :Else : XA = X3 + (X2 - X3) * (Y - Y3) / (Y2 - Y3):EndIf
If (Y3 = Y1) :XB = X1 :Else : XB = X3 + (X1 - X3) * (Y - Y3) / (Y1 - Y3):EndIf
EndIf
If(XB < XA)
Swap XB, XA
EndIf
XA = (XA + 2) >> 3
XB = (XB - 2) >> 3
If(XB < Clip\X1) Or (XA > Clip\X2): Continue:EndIf
If(XA < Clip\X1): XA = Clip\X1:EndIf
If(XB > Clip\X2): XB = Clip\X2:EndIf
For X = XA To XB
Plot(X, Y, C)
Next
Next
EndProcedure
OpenWindow(0, 0, 0, 640, 480, "fill_triangle", #PB_Window_SystemMenu)
CreateImage(0, 640, 480)
ImageGadget(0, 0, 0, 640, 480, ImageID(0))
Clip\X1 = 50
Clip\Y1 = 50
Clip\X2 = 589
Clip\Y2 = 429
Ende = #False
Repeat
Event = WaitWindowEvent(20)
StartDrawing(ImageOutput(0))
fill_triangle(Random(840) - 100, Random(680) - 100, Random(840) - 100, Random(680) - 100, Random(840) - 100, Random(680) - 100, Random(#White))
StopDrawing()
SetGadgetState(0, ImageID(0))
Select Event
Case #PB_Event_CloseWindow
Ende = #True
EndSelect
Until Ende