QB64 Phoenix Edition
FillTriangle and FillQuad - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Prolific Programmers (https://qb64phoenix.com/forum/forumdisplay.php?fid=26)
+---- Forum: SMcNeill (https://qb64phoenix.com/forum/forumdisplay.php?fid=29)
+---- Thread: FillTriangle and FillQuad (/showthread.php?tid=2030)



FillTriangle and FillQuad - SMcNeill - 09-23-2023

Two simple little routines to quickly and efficiently fill triangles and quadrilaterals:

Code: (Select All)
SUB FillTriangle (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
    $CHECKING:OFF
    STATIC a&, m AS _MEM
    IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32): m = _MEMIMAGE(a&)
    _MEMPUT m, m.OFFSET, K
    _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
    $CHECKING:ON
END SUB

SUB FillQuad (x1, y1, x2, y2, x3, y3, x4, y4, K AS _UNSIGNED LONG)
    FillTriangle x1, y1, x2, y2, x3, y3, K
    FillTriangle x3, y3, x4, y4, x1, y1, K
END SUB



RE: FillTriangle and FillQuad - SMcNeill - 09-23-2023

@bplus You might like these routines.  I know you have similar ones, but these might be faster and less lines of code overall for you, which I know you like to keep LOC as low as possible.  Wink


RE: FillTriangle and FillQuad - bplus - 09-23-2023

(09-23-2023, 11:59 AM)SMcNeill Wrote: @bplus You might like these routines.  I know you have similar ones, but these might be faster and less lines of code overall for you, which I know you like to keep LOC as low as possible.  Wink

Thanks for update Steve!

Our last fix had to repair Blending for some reason I have forgotten:
Code: (Select All)
''   BEST saves dest and optimized with Static a& and alpha colors work better
'2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
Sub ftri (x1, y1, x2, y2, x3, y3, K As _Unsigned Long)
    Dim D As Long
    Static a&
    D = _Dest
    If a& = 0 Then a& = _NewImage(1, 1, 32)
    _Dest a&
    _DontBlend a& '  '<<<< new 2019-12-16 fix
    PSet (0, 0), K
    _Blend a& '<<<< new 2019-12-16 fix
    _Dest D
    _MapTriangle _Seamless(0, 0)-(0, 0)-(0, 0), a& To(x1, y1)-(x2, y2)-(x3, y3)
End Sub

'update 2019-12-16 needs updated fTri 2019-12-16
'need 4 non linear points (not all on 1 line) list them clockwise so x2, y2 is opposite of x4, y4
Sub fquad (x1, y1, x2, y2, x3, y3, x4, y4, K As _Unsigned Long)
    ftri x1, y1, x2, y2, x3, y3, K
    ftri x3, y3, x4, y4, x1, y1, K
End Sub

So using memory covers that old blending problem still?


RE: FillTriangle and FillQuad - Dav - 09-23-2023

Woah these are really fast!  Here's a simple usage.

- Dav

Code: (Select All)

Screen _NewImage(800, 600, 32)

Do
    x1 = Rnd * _Width: y1 = Rnd * _Height 'point 1
    x2 = x1 + Rnd * 400: y2 = y1 + Rnd * 300 'point 2
    x3 = x2 / 1.5: y3 = y2 * 1.5 'point 3
    FillTriangle x1, y1, x2, y2, x3, y3, _RGB(Rnd * 255, Rnd * 255, Rnd * 255)
    _Limit 1000
Loop Until _KeyHit


Sub FillTriangle (x1, y1, x2, y2, x3, y3, K As _Unsigned Long)
    $Checking:Off
    Static a&, m As _MEM
    If a& = 0 Then a& = _NewImage(1, 1, 32): m = _MemImage(a&)
    _MemPut m, m.OFFSET, K
    _MapTriangle _Seamless(0, 0)-(0, 0)-(0, 0), a& To(x1, y1)-(x2, y2)-(x3, y3)
    $Checking:On
End Sub

Sub FillQuad (x1, y1, x2, y2, x3, y3, x4, y4, K As _Unsigned Long)
    FillTriangle x1, y1, x2, y2, x3, y3, K
    FillTriangle x3, y3, x4, y4, x1, y1, K
End Sub



RE: FillTriangle and FillQuad - SMcNeill - 09-23-2023

(09-23-2023, 01:37 PM)bplus Wrote:
(09-23-2023, 11:59 AM)SMcNeill Wrote: @bplus You might like these routines.  I know you have similar ones, but these might be faster and less lines of code overall for you, which I know you like to keep LOC as low as possible.  Wink

Thanks for update Steve!

Our last fix had to repair Blending for some reason I have forgotten:
Code: (Select All)
''   BEST saves dest and optimized with Static a& and alpha colors work better
'2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
Sub ftri (x1, y1, x2, y2, x3, y3, K As _Unsigned Long)
    Dim D As Long
    Static a&
    D = _Dest
    If a& = 0 Then a& = _NewImage(1, 1, 32)
    _Dest a&
    _DontBlend a& '  '<<<< new 2019-12-16 fix
    PSet (0, 0), K
    _Blend a& '<<<< new 2019-12-16 fix
    _Dest D
    _MapTriangle _Seamless(0, 0)-(0, 0)-(0, 0), a& To(x1, y1)-(x2, y2)-(x3, y3)
End Sub

'update 2019-12-16 needs updated fTri 2019-12-16
'need 4 non linear points (not all on 1 line) list them clockwise so x2, y2 is opposite of x4, y4
Sub fquad (x1, y1, x2, y2, x3, y3, x4, y4, K As _Unsigned Long)
    ftri x1, y1, x2, y2, x3, y3, K
    ftri x3, y3, x4, y4, x1, y1, K
End Sub

So using memory covers that old blending problem still?


It should.  _MEMPUT puts the data directly for us.  There's no blending going on there!