QB64 Phoenix Edition
polyFT in QBJS - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: QBJS, BAM, and Other BASICs (https://qb64phoenix.com/forum/forumdisplay.php?fid=50)
+--- Thread: polyFT in QBJS (/showthread.php?tid=1983)

Pages: 1 2


polyFT in QBJS - James D Jarvis - 09-09-2023

polyFT almost working in QBJS.  I just haven't figured out how to make it look good with thicker lines using the 2d graphic library functions yet but it is working with lines of one pixel thick.

[qbjs]'polyFT in QBjs
'v 0.5
'haven't quite figure out how to make the this look good with thicker lines but it works fine at one pixel wide
Import G2D From "lib/graphics/2d.bas"

Randomize Timer
For i = 0 to 100
polyFT Rnd * 640, Rnd * 400,rnd*30+30,int(3+rnd*12),int(rnd*360),_rgb32(int(rnd*256),int(rnd*256),int(rnd*256)),_rgb32(int(rnd*256),int(rnd*256),int(rnd*256))
_Limit 200
Next i


Sub polyFT (cx As Long, cy As Long, rad As Long, sides As Integer, rang As Long, klr As _Unsigned Long, lineyes As _Unsigned Long)
    'draw an equilateral polygon using filled triangle for each segment
    'centered at cx,cy to radius rad of sides # of face rotated to angle rang of color klr and lineyes if there is an outline, a value 0 would create no outline
    Dim px(sides)
    Dim py(sides)
    pang = 360 / sides
    ang = 0
    For p = 1 To sides
        px(p) = cx + (rad * Cos(0.01745329 * (ang + rang)))
        py(p) = cy + (rad * Sin(0.01745329 * (ang + rang)))
        ang = ang + pang
    Next p
    For p = 1 To sides - 1
    line (cx,cy)-(px(p),py(p)),klr
    G2D.FillTriangle cx,cy,px(p),py(p),px(p+1),py(p+1),klr
    Next p
    line (cx,cy)-(px(sides),py(sides)),klr
    G2D.FillTriangle cx,cy,px(sides),py(sides),px(1),py(1),klr
  if lineyes>0 then
  for p =1 to sides-1
    Line (px(p), py(p))-(px(p + 1), py(p + 1)), lineyes
  next
  Line (px(sides), py(sides))-(px(1), py(1)), lineyes
  end if
End Sub[/qbjs]


RE: polyFT in QBJS - bplus - 09-09-2023

I added fTri routine and put the 100 polys in a loop:
Jarvis polyFT demo

QBJS tags not working

This won't work in QB64 but is code for above:
Code: (Select All)
Randomize Timer
screen _newimage(640, 400, 32)
do
cls
For i = 0 to 100
polyFT Rnd * 640, Rnd * 400,rnd*30+30,int(3+rnd*12),int(rnd*360),_rgb32(int(rnd*256),int(rnd*256),int(rnd*256)),_rgb32(int(rnd*256),int(rnd*256),int(rnd*256))
_Limit 200
Next i
_delay 2
loop until _keydown(27)


Sub polyFT (cx As Long, cy As Long, rad As Long, sides As Integer, rang As Long, klr As _Unsigned Long, lineyes As _Unsigned Long)
    'draw an equilateral polygon using filled triangle for each segment
    'centered at cx,cy to radius rad of sides # of face rotated to angle rang scaled to ww and vv of color klr and lineyes if there is an outline, a value 0 would create no outline
    Dim px(sides)
    Dim py(sides)
    dim pang, ang, p
    pang = 360 / sides
    ang = 0
    For p = 1 To sides
        px(p) = cx + (rad * Cos(0.01745329 * (ang + rang)))
        py(p) = cy + (rad * Sin(0.01745329 * (ang + rang)))
        ang = ang + pang
    Next p
    For p = 1 To sides - 1
    line (cx,cy)-(px(p),py(p)),klr
    ftri cx,cy,px(p),py(p),px(p+1),py(p+1),klr
    Next p
    line (cx,cy)-(px(sides),py(sides)),klr
    ftri cx,cy,px(sides),py(sides),px(1),py(1),klr
  if lineyes>0 then
  for p =1 to sides-1
    Line (px(p), py(p))-(px(p + 1), py(p + 1)), lineyes
  next
  Line (px(sides), py(sides))-(px(1), py(1)), lineyes
  end if
End Sub

'Andy Amaya's triangle fill modified for QB64, use if color already set
Sub ftri (xx1, yy1, xx2, yy2, xx3, yy3, c As _Unsigned Long)
    Dim x1 As Single, y1 As Single, x2 As Single, y2 As Single, x3 As Single, y3 As Single
    Dim slope1 As Single, slope2 As Single, length As Single, x As Single, lastx%, y As Single
    Dim slope3 As Single
    'make copies before swapping
    x1 = xx1: y1 = yy1: x2 = xx2: y2 = yy2: x3 = xx3: y3 = yy3

    'triangle coordinates must be ordered: where x1 < x2 < x3
    If x2 < x1 Then Swap x1, x2: Swap y1, y2
    If x3 < x1 Then Swap x1, x3: Swap y1, y3
    If x3 < x2 Then Swap x2, x3: Swap y2, y3
    If x1 <> x3 Then slope1 = (y3 - y1) / (x3 - x1)

    'draw the first half of the triangle
    length = x2 - x1
    If length <> 0 Then
        slope2 = (y2 - y1) / length
        For x = 0 To length
            Line (Int(x + x1), Int(x * slope1 + y1))-(Int(x + x1), Int(x * slope2 + y1)), c
            lastx% = Int(x + x1)
        Next
    End If

    'draw the second half of the triangle
    y = length * slope1 + y1: length = x3 - x2
    If length <> 0 Then
        slope3 = (y3 - y2) / length
        For x = 0 To length
            If Int(x + x2) <> lastx% Then
                Line (Int(x + x2), Int(x * slope1 + y))-(Int(x + x2), Int(x * slope3 + y2)), c
            End If
        Next
    End If
End Sub



RE: polyFT in QBJS - James D Jarvis - 09-09-2023

neat. my older QB64 version uses _maptriangle .  I haven't seen ftri yet.


RE: polyFT in QBJS - grymmjack - 09-10-2023

(09-09-2023, 11:16 PM)James D Jarvis Wrote: polyFT almost working in QBJS.  I just haven't figured out how to make it look good with thicker lines using the 2d graphic library functions yet but it is working with lines of one pixel thick.

Code: (Select All)
'polyFT in QBjs
'v 0.5
'haven't quite figure out how to make the this look good with thicker lines but it works fine at one pixel wide
Import G2D From "lib/graphics/2d.bas"

Randomize Timer
For i = 0 to 100
polyFT Rnd * 640, Rnd * 400,rnd*30+30,int(3+rnd*12),int(rnd*360),_rgb32(int(rnd*256),int(rnd*256),int(rnd*256)),_rgb32(int(rnd*256),int(rnd*256),int(rnd*256))
_Limit 200
Next i


Sub polyFT (cx As Long, cy As Long, rad As Long, sides As Integer, rang As Long, klr As _Unsigned Long, lineyes As _Unsigned Long)
    'draw an equilateral polygon using filled triangle for each segment
    'centered at cx,cy to radius rad of sides # of face rotated to angle rang scaled to ww and vv of color klr and lineyes if there is an outline, a value 0 would create no outline
    Dim px(sides)
    Dim py(sides)
    pang = 360 / sides
    ang = 0
    For p = 1 To sides
        px(p) = cx + (rad * Cos(0.01745329 * (ang + rang)))
        py(p) = cy + (rad * Sin(0.01745329 * (ang + rang)))
        ang = ang + pang
    Next p
    For p = 1 To sides - 1
    line (cx,cy)-(px(p),py(p)),klr
    G2D.FillTriangle cx,cy,px(p),py(p),px(p+1),py(p+1),klr
    Next p
    line (cx,cy)-(px(sides),py(sides)),klr
    G2D.FillTriangle cx,cy,px(sides),py(sides),px(1),py(1),klr
  if lineyes>0 then
  for p =1 to sides-1
    Line (px(p), py(p))-(px(p + 1), py(p + 1)), lineyes
  next
  Line (px(sides), py(sides))-(px(1), py(1)), lineyes
  end if
End Sub

Gotta use qb tag for pasting literal code not qbjs for qbjs share. For qbjs use share link from qbjs.org and then qbjs bbcode.
Like @bplus did.


Looks cool so far!


RE: polyFT in QBJS - bplus - 09-10-2023

(09-09-2023, 11:51 PM)James D Jarvis Wrote: neat. my older QB64 version uses _maptriangle .  I haven't seen ftri yet.

Yes I have fTri (Fill Triangle) in my drawing tools
https://qb64phoenix.com/forum/showthread.php?tid=272&pid=1204#pid1204 (as filltri because several versions of fill triangle in there.)

I have been using it for filling triangles for the pumpkin in my Fall Banner code. I also showed it to Charlie for his polygon fill because he doesn't have _MapTriangle either. Very handy for Basics without _MapTriangle.

@grymmjack this is 2nd instance where a link to my mini-mod board thread takes me to bottom of page of that thread and inside forum editor when I am logged in. But works correctly when I am logged out.


RE: polyFT in QBJS - dbox - 09-10-2023

James, I thought your original version looked great that used G2D.FillTriange:



What was the issue?  If you want to adjust the line width you can use the G2D.LineWidth method.  I added it to your program, you can uncomment it to play with different line widths.


RE: polyFT in QBJS - James D Jarvis - 09-10-2023

(09-10-2023, 04:21 AM)dbox Wrote: James, I thought your original version looked great that used G2D.FillTriange:



What was the issue?  If you want to adjust the line width you can use the G2D.LineWidth method.  I added it to your program, you can uncomment it to play with different line widths.

Thanks.  

If you crank up the line width to just say 4 or 5 pixels you'll notice a gap in the line segments on the outline with the routine I used. When I did this in QB64 I used a custom fat-line routine that used filled circles to draw lines and as such there was always overlap at endpoints due to how the line segments were drawn.  There may be fancier ways to draw those line with the graphics library in QBJS that I am just unaware of at this point. Is there a continuous line plot I just didn't notice yet, how's the end-cap work on line segments?


RE: polyFT in QBJS - bplus - 09-10-2023

Oh Fill triangle was available in QBJS G2D. library, missed it.

Here is link to QBJS keywords to bookmark:
https://github.com/boxgaming/qbjs/wiki/Supported-Keywords#qbjs-standard-modules


RE: polyFT in QBJS - dbox - 09-10-2023

Ah, there are a couple of additional line cap styles you can use with the G2D.LineCap method.  Here it is with the round cap style:




RE: polyFT in QBJS - grymmjack - 09-10-2023

(09-10-2023, 03:11 AM)bplus Wrote:
(09-09-2023, 11:51 PM)James D Jarvis Wrote: neat. my older QB64 version uses _maptriangle .  I haven't seen ftri yet.

Yes I have fTri (Fill Triangle) in my drawing tools
https://qb64phoenix.com/forum/showthread.php?tid=272&pid=1204#pid1204 (as filltri because several versions of fill triangle in there.)

I have been using it for filling triangles for the pumpkin in my Fall Banner code. I also showed it to Charlie for his polygon fill because he doesn't have _MapTriangle either. Very handy for Basics without _MapTriangle.

@grymmjack this is 2nd instance where a link to my mini-mod board thread takes me to bottom of page of that thread and inside forum editor when I am logged in. But works correctly when I am logged out.

Turn off the editor at the bottom. It’ll stop. I posted the solution in another thread. Press the little switch on the bottom of the editor. It will hide it. Then you won’t autofocus to it. This only seems to happen when the default editor is source mode.