Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Another small filled circe sub (not as fast as fcirc)
#84
Nice collection of routines there.  I didn't know there were so many of them.   I've given up trying to be fastest, and will just enjoy the FC party now. Tongue

Hmm, but I do a see a couple ways to make the gradient part of mine go faster, make the sub require separate r,g,b,a parameters instead of clr~&, so _red32, _Blue32, _Green32 won't have to be called to break it up.  Also, I could remove some division out of the For i = -x To x NEXT loop, and make variables for them at the top. The (red / 2), (blu /2), (grn /2) ones.

Added a twist on the ballfield thing.  Spins balls around also now, follows mouse as the center point.

- Dav

Code: (Select All)

'=============
'BALLFIELD.BAS v2.0
'=============
'Shows balls spinning towards you.
'Move mouse to move swirling ball tunnel.
'Dav, SEP/2024

'Uses updated version of FC SUB discussed here:
'https://qb64phoenix.com/forum/showthread.php?tid=2989


Randomize Timer

Screen _NewImage(1000, 700, 32)

balls = 600
Dim ballx(balls), bally(balls), ballz(balls), ballSize(balls)
Dim ballc~&(balls), sort(balls) 'array for bubble sort
Dim balla(balls) 'ball angle for spin

' Init balls
For i = 0 To balls - 1
    ballx(i) = Rnd * _Width 'x pos
    bally(i) = Rnd * _Height 'y pos
    ballz(i) = Rnd * 100 + 25 'z depth
    ballc~&(i) = _RGBA(55 + (Rnd * 200), 55 + (Rnd * 200), 55 + (Rnd * 200), 255)
    balla(i) = Rnd * 360
Next

Do
    Cls

    While _MouseInput: Wend
    mx = _MouseX: my = _MouseY

    For i = 0 To balls - 1

        balla(i) = balla(i) + 1 * (1 / ballz(i))

        'move faster towards front based on z
        ballz(i) = ballz(i) - (.75 * (_Width - ballz(i)) / _Width)
        'if done, reset ball data
        If ballz(i) < 1 Then
            ballz(i) = Rnd * 100 + 25
            ballx(i) = Rnd * _Width
            bally(i) = Rnd * _Height
            balla(i) = Rnd * 360 ' Reset balla
        End If
        'get size from depth (decreases with depth)
        ballSize(i) = (4 / ballz(i)) * 100
        'calc spin effect
        ballx(i) = mx + ((ballSize(i) / 2) * 25) * Cos(balla(i))
        bally(i) = my + ((ballSize(i) / 2) * 25) * Sin(balla(i))
        'drift off from center (veer away)
        veerx = (ballx(i) - mx) * (ballSize(i) / _Width)
        veery = (bally(i) - my) * (ballSize(i) / _Height)
        ballx(i) = ballx(i) + veerx
        bally(i) = bally(i) + veery
        sort(i) = i 'store this ball into array for sorting
    Next

    'bubble sort based on size (smallest first)
    'so small ones are drawn first, big ones last
    For i = 0 To balls - 2
        For j = 0 To balls - 2 - i
            If ballSize(sort(j)) > ballSize(sort(j + 1)) Then
                Swap sort(j), sort(j + 1)
            End If
        Next
    Next

    'draw balls in the sort() array order
    For i = 0 To balls - 1
        'only call a draw if the ball is in screen bounds
        If ballx(sort(i)) > 0 And ballx(sort(i)) < _Width Then
            If bally(sort(i)) > 0 And bally(sort(i)) < _Height Then
                fc ballx(sort(i)), bally(sort(i)), ballSize(sort(i)), ballc~&(sort(i)), 1
            End If
        End If
    Next

    _Limit 30
    _Display
Loop Until InKey$ <> ""


Sub fc (cx, cy, radius, clr~&, grad)
    If grad = 1 Then
        red = _Red32(clr~&)
        grn = _Green32(clr~&)
        blu = _Blue32(clr~&)
        alpha = _Alpha32(clr~&)
    End If
    r2 = radius * radius
    For y = -radius To radius
        x = Sqr(r2 - y * y)
        'if doing gradient
        If grad = 1 Then
            For i = -x To x
                dis = Sqr(i * i + y * y) / radius
                red2 = red * (1 - dis) + (red / 2) * dis
                grn2 = grn * (1 - dis) + (grn / 2) * dis
                blu2 = blu * (1 - dis) + (blu / 2) * dis
                clr2~& = _RGBA(red2, grn2, blu2, alpha)
                Line (cx + i, cy + y)-(cx + i, cy + y), clr2~&, BF
            Next
        Else
            Line (cx - x, cy + y)-(cx + x, cy + y), clr~&, BF
        End If
    Next
End Sub

Find my programs here in Dav's QB64 Corner
Reply


Messages In This Thread
RE: Another small filled circe sub (not as fast as fcirc) - by Dav - 09-02-2024, 08:02 PM



Users browsing this thread: 85 Guest(s)