;26.07.2024 14:02-14:14
Procedure Triangle1 (x1, y1, x2, y2, x3, y3,colr)
If y1 > y2
;THEN Swap y1%, y2%: Swap x1%, x2%
t=y1
y1=y2
y2=t
t=x1
x1=x2
x2=t
EndIf
If y1 > y3
; THEN Swap y1%, y3%: Swap x1%, x3%
t=y1
y1=y3
y3=t
t=x1
x1=x3
x3=t
EndIf
If y2 > y3
; THEN Swap y2%, y3%: Swap x2%, x3%
t=y2
y2=y3
y3=t
t=x2
x2=x3
x3=t
EndIf
DeltaY1 = y2 - y1
DeltaY2 = y3 - y2
DeltaY3 = y3 - y1
d1.f=0
d2.f=0
d3.f=0
If DeltaY1
d1.f = (x2 - x1) / DeltaY1
EndIf
If DeltaY2
d2.f = (x3 - x2) / DeltaY2
EndIf
If DeltaY3
d3.f = (x3 - x1) / DeltaY3
EndIf
lx.f = x1
rx.f = lx
For y = y1 To y2 - 1
LineXY(lx,y,rx,y,colr)
lx = lx + d1
rx = rx + d3
Next y
lx = x2
For y = y2 To y3
LineXY(lx,y,rx,y,colr)
lx = lx + d2
rx = rx + d3
Next y
EndProcedure
Procedure Triangle1 (x1, y1, x2, y2, x3, y3,colr)
If y1 > y2
;THEN Swap y1%, y2%: Swap x1%, x2%
t=y1
y1=y2
y2=t
t=x1
x1=x2
x2=t
EndIf
If y1 > y3
; THEN Swap y1%, y3%: Swap x1%, x3%
t=y1
y1=y3
y3=t
t=x1
x1=x3
x3=t
EndIf
If y2 > y3
; THEN Swap y2%, y3%: Swap x2%, x3%
t=y2
y2=y3
y3=t
t=x2
x2=x3
x3=t
EndIf
DeltaY1 = y2 - y1
DeltaY2 = y3 - y2
DeltaY3 = y3 - y1
d1.f=0
d2.f=0
d3.f=0
If DeltaY1
d1.f = (x2 - x1) / DeltaY1
EndIf
If DeltaY2
d2.f = (x3 - x2) / DeltaY2
EndIf
If DeltaY3
d3.f = (x3 - x1) / DeltaY3
EndIf
lx.f = x1
rx.f = lx
For y = y1 To y2 - 1
LineXY(lx,y,rx,y,colr)
lx = lx + d1
rx = rx + d3
Next y
lx = x2
For y = y2 To y3
LineXY(lx,y,rx,y,colr)
lx = lx + d2
rx = rx + d3
Next y
EndProcedure
Procedure Triangle2 (x1, y1, x2, y2, x3, y3,colr)
;'This one uses some tricks with integer math to avoid
;'any floating point math
If y1 > y2
Swap y1, y2
Swap x1, x2
EndIf
If y1 > y3
Swap y1, y3
Swap x1, x3
EndIf
If y2 > y3
Swap y2, y3
Swap x2, x3
EndIf
DeltaY1 = y2 - y1
DeltaY2 = y3 - y2
DeltaY3 = y3 - y1
d1=0
d2=0
d3=0
If DeltaY1
d1 = Int((x2 - x1) * $10000 / DeltaY1);&-long integere
EndIf
If DeltaY2
d2 = Int((x3 - x2) * $10000 / DeltaY2)
EndIf
If DeltaY3
d3 = Int((x3 - x1) * $10000 / DeltaY3)
EndIf
lx = x1 * $10000
rx = lx
For y = y1 To y2 - 1; 'Draw from high point to mid point
lx_ = Int(lx / $10000)
rx_ = Int(rx / $10000)
LineXY(lx_,y,rx_,y,colr);Line (lx%, y%)-(rx%, y%), c%
lx = lx + d1
rx = rx + d3
Next y
lx = x2 * $10000
For y = y2 To y3; 'Draw from mid point to low point
lx_ = Int(lx / $10000)
rx_ = Int(rx / $10000)
LineXY(lx_,y,rx_,y,colr);Line (lx%, y%)-(rx%, y%), c%
lx = lx + d2
rx = rx + d3
Next y
EndProcedure
an=0
If InitSprite() And OpenWindow(0,0,0,640,480,"Triangle Filler II",#PB_Window_SystemMenu) And OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
Repeat
StartDrawing(ScreenOutput())
Box(0,0,640,480,0)
x1=200+Int(80*Cos((an+0)*#PI/180))
y1=200+Int(80*Sin((an+0)*#PI/180))
x2=200+Int(80*Cos((an+120)*#PI/180))
y2=200+Int(80*Sin((an+120)*#PI/180))
x3=200+Int(80*Cos((an+240)*#PI/180))
y3=200+Int(80*Sin((an+240)*#PI/180))
Triangle1(x1+200,y1,x2+200,y2,x3+200,y3,$FFFFFF)
Triangle2(x1,y1,x2,y2,x3,y3,255)
an=(an+2)%360
StopDrawing()
FlipBuffers()
;Repeat
Until WindowEvent()=#PB_Event_CloseWindow
EndIf
;'This one uses some tricks with integer math to avoid
;'any floating point math
If y1 > y2
Swap y1, y2
Swap x1, x2
EndIf
If y1 > y3
Swap y1, y3
Swap x1, x3
EndIf
If y2 > y3
Swap y2, y3
Swap x2, x3
EndIf
DeltaY1 = y2 - y1
DeltaY2 = y3 - y2
DeltaY3 = y3 - y1
d1=0
d2=0
d3=0
If DeltaY1
d1 = Int((x2 - x1) * $10000 / DeltaY1);&-long integere
EndIf
If DeltaY2
d2 = Int((x3 - x2) * $10000 / DeltaY2)
EndIf
If DeltaY3
d3 = Int((x3 - x1) * $10000 / DeltaY3)
EndIf
lx = x1 * $10000
rx = lx
For y = y1 To y2 - 1; 'Draw from high point to mid point
lx_ = Int(lx / $10000)
rx_ = Int(rx / $10000)
LineXY(lx_,y,rx_,y,colr);Line (lx%, y%)-(rx%, y%), c%
lx = lx + d1
rx = rx + d3
Next y
lx = x2 * $10000
For y = y2 To y3; 'Draw from mid point to low point
lx_ = Int(lx / $10000)
rx_ = Int(rx / $10000)
LineXY(lx_,y,rx_,y,colr);Line (lx%, y%)-(rx%, y%), c%
lx = lx + d2
rx = rx + d3
Next y
EndProcedure
an=0
If InitSprite() And OpenWindow(0,0,0,640,480,"Triangle Filler II",#PB_Window_SystemMenu) And OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
Repeat
StartDrawing(ScreenOutput())
Box(0,0,640,480,0)
x1=200+Int(80*Cos((an+0)*#PI/180))
y1=200+Int(80*Sin((an+0)*#PI/180))
x2=200+Int(80*Cos((an+120)*#PI/180))
y2=200+Int(80*Sin((an+120)*#PI/180))
x3=200+Int(80*Cos((an+240)*#PI/180))
y3=200+Int(80*Sin((an+240)*#PI/180))
Triangle1(x1+200,y1,x2+200,y2,x3+200,y3,$FFFFFF)
Triangle2(x1,y1,x2,y2,x3,y3,255)
an=(an+2)%360
StopDrawing()
FlipBuffers()
;Repeat
Until WindowEvent()=#PB_Event_CloseWindow
EndIf
Вторая часть не заработала - автор использовал одни и те же переменные но с разными типами.
Комментарии
Отправить комментарий