Improved my small Gradient Ball drawing SUB
#17
(07-12-2023, 12:09 PM)Dav Wrote: @bplus you inspired me.  Had to give a Shaded Voronoi a try too.  Studied a few sources online, ended up with this simple version.

- Dav

Code: (Select All)

SCREEN _NEWIMAGE(1000, 600, 32)

DIM SHARED Points: Points = 50
DIM SHARED PointX(Points), PointY(Points), PointR(Points), PointG(Points), PointB(Points)

RANDOMIZE TIMER
FOR p = 1 TO Points
    PointX(p) = RND * _WIDTH
    PointY(p) = RND * _HEIGHT
    PointR(p) = RND * 255
    PointG(p) = RND * 255
    PointB(p) = RND * 255
NEXT

FOR x = 0 TO _WIDTH
    FOR y = 0 TO _HEIGHT
        min = SQR((x - PointX(1)) ^ 2 + (y - PointY(1)) ^ 2)
        closest = 1
        FOR p = 2 TO Points
            dis = SQR((x - PointX(p)) ^ 2 + (y - PointY(p)) ^ 2)
            IF dis < min THEN
                min = dis: closest = p
            END IF
        NEXT
        PSET (x, y), _RGB(PointR(closest) - min, PointG(closest) - min, PointB(closest) - min)
    NEXT
NEXT

SLEEP

This code is freaking me out. I played around with optimizing it a bit.

Code: (Select All)
CONST POINTS = 50

TYPE TYPE_POINT
    x AS INTEGER
    y AS INTEGER
    r AS INTEGER
    g AS INTEGER
    b AS INTEGER
END TYPE

DIM p(POINTS) AS TYPE_POINT
DIM AS INTEGER p, x, y, closest
DIM AS SINGLE min, max, dis

RANDOMIZE TIMER
SCREEN _NEWIMAGE(1000, 600, 32)
max = _HYPOT(_WIDTH, _HEIGHT) ' the maximum distance possible with given screen size
p = 0
DO
    p = p + 1
    p(p).x = RND * _WIDTH
    p(p).y = RND * _HEIGHT
    p(p).r = RND * 255
    p(p).g = RND * 255
    p(p).b = RND * 255
LOOP UNTIL p = POINTS
x = -1
DO
    x = x + 1
    y = -1
    DO
        y = y + 1
        min = max '            reset to maximum possible distance
        p = 0
        DO
            p = p + 1
            dis = _HYPOT(x - p(p).x, y - p(p).y)
            IF dis < min THEN
                min = dis
                closest = p
            END IF
        LOOP UNTIL p = POINTS
        PSET (x, y), _RGB(p(closest).r - min, p(closest).g - min, p(closest).b - min)
    LOOP UNTIL y = _HEIGHT
LOOP UNTIL x = _WIDTH
SLEEP
Software and cathedrals are much the same — first we build them, then we pray.
QB64 Tutorial
Reply


Messages In This Thread
RE: Improved my small Gradient Ball drawing SUB - by TerryRitchie - 07-12-2023, 03:21 PM



Users browsing this thread: 6 Guest(s)