Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Graphics 2
#1
This has nothing to do with my spinning circle graphics program, but its intresting nonetheless, imo. Not reccommended for slow computers...

to increase speed, or make it simpler, change the nodec constant lower.

Code:
declare function math.distance (x1 as double, y1 as double, x2 as double, y2 as double) as double
const nodec = 4 - 1

randomize timer

type nodetype

    x as double
    y as double

    r as double
    g as double
    b as double
    
end type
    
dim node(nodec) as nodetype

dim x as double
dim y as double
dim n as double
dim dist as double
dim dist2 as double
dim maxd as double
dim n1 as double
dim n2 as double
dim zz as double

screen 19, 32

for x = 0 to nodec
    
    node( x ).x = int( rnd * 800 )
    node( x ).y = int( rnd * 600 )
    
    node( x ).r = int(rnd * 16) + 240
    node( x ).g = int(rnd * 16) + 240
    node( x ).b = int(rnd * 96) + 128
    
    pset( node( x ).x, node( x ).y ), rgb( node( x ).r, node( x ).g, node( x ).b )
    
next x

print 800 * 600 * nodec / 4

for x = 0 to 799 step 4
    for y = 0 to 599 step 4
        dist = 10000
        for n = 0 to nodec
            zz = math.distance( x, y, node( n ).x, node( n ).y)
            if zz < dist then dist = zz
        next n
        if maxd < dist then maxd = dist
    next y
next x

for x = 0 to 799 step 1
    for y = 0 to 599 step 1
        
        dist = 10000
        dist2 = 10000
        
        for n = 0 to nodec
            
            zz = math.distance( x, y, node( n ).x, node( n ).y)
            if zz < dist then
                swap dist, dist2
                swap n1, n2
                dist = zz
                n1 = n
            elseif zz < dist2 and zz > dist then
                dist2 = zz
                n2 = n
            end if        
                  
        next n
        
        zz = ( dist + dist2 ) / 2 / maxd  
        
        red =   255 - ( node( n1 ).r / 2 + node( n2 ).r / 2  ) * zz - 64
        if red > 255 then red = 255
        if red < 0 then red = 0
        green = 255 - ( node( n1 ).g / 2 + node( n2 ).g / 2  ) * zz - 64
        if green > 255 then green = 255
        if green < 0 then green = 0
        blue =  255 - ( node( n1 ).b / 2 + node( n2 ).b / 2  ) * zz - 64
        if blue > 255 then blue = 255
        if blue < 0 then blue = 0
        
        pset( x, y ), rgb( red, green, blue )
        
    next y
next x

sleep
        
function math.distance (x1 as double, y1 as double, x2 as double, y2 as double) as double
    math.distance = sqr((x1-x2)^2 + (y1-y2)^2)
end function
[Image: freebasic.png]
Reply
#2
lol, another half an hour and I ended up with this.....Tongue

Code:
declare function math.distance (x1 as double, y1 as double, x2 as double, y2 as double) as double

const nodec = 32 - 1 'nodes
const xvar = 9 'max x movement per frame
const yvar = 7 'max y movement per frame
const maxframes = 23 'max frames, don't forget that 11 is 12, 1 is 2, etc...

randomize timer

type nodetype

    x as double
    y as double

    r as double
    g as double
    b as double
    
end type
    
dim node(nodec) as nodetype
dim scr(maxframes, 800*600+8) as integer

dim x as double
dim y as double
dim n as double
dim dist as double
dim dist2 as double
dim maxd as double
dim n1 as double
dim n2 as double
dim zz as double

screen 19, 32, 2

screenset 1, 1
print "Generating..."
screenset 0, 1

for x = 0 to nodec
    
    node( x ).x = int( rnd * 800 )
    node( x ).y = int( rnd * 600 )
    
    node( x ).r = int(rnd * 16) + 240
    node( x ).g = int(rnd * 16) + 240
    node( x ).b = int(rnd * 96) + 128
    
    pset( node( x ).x, node( x ).y ), rgb( node( x ).r, node( x ).g, node( x ).b )
    
next x

for asdf = 0 to maxframes
    
    cls
    
    dist = 10000
    dist2 = 10000
    maxd = 0
    
    for x = 0 to 799 step 4
        for y = 0 to 599 step 4
            dist = 10000
            for n = 0 to nodec
                zz = math.distance( x, y, node( n ).x, node( n ).y)
                if zz < dist then dist = zz
            next n
            if maxd < dist then maxd = dist
        next y
    next x
    
    for x = 0 to 799 step 1
        for y = 0 to 599 step 1
            
            dist = 10000
            dist2 = 10000
            
            for n = 0 to nodec
                
                zz = math.distance( x, y, node( n ).x, node( n ).y)
                if zz < dist then
                    swap dist, dist2
                    swap n1, n2
                    dist = zz
                    n1 = n
                elseif zz < dist2 and zz > dist then
                    dist2 = zz
                    n2 = n
                end if        
                      
            next n
            
            zz = ( dist + dist2 ) / 2 / maxd  
            
            red =   255 - ( node( n1 ).r / 2 + node( n2 ).r / 2  ) * zz - 64
            if red > 255 then red = 255
            if red < 0 then red = 0
            green = 255 - ( node( n1 ).g / 2 + node( n2 ).g / 2  ) * zz - 64
            if green > 255 then green = 255
            if green < 0 then green = 0
            blue =  255 - ( node( n1 ).b / 2 + node( n2 ).b / 2  ) * zz - 64
            if blue > 255 then blue = 255
            if blue < 0 then blue = 0
            
            pset( x, y ), rgb( red, green, blue )
            
        next y
    next x
    
    get ( 0, 0 )-(799, 599), scr( asdf, 0 )
    screenset 1, 1
    locate 2, 1 : print int( asdf / maxframes * 100 ); "% complete   "
    screenset 0, 1
    
    for x = 0 to nodec
    
        node( x ).x = node( x ).x - int( rnd * xvar ) + ( xvar - 1 ) / 2
        node( x ).y = node( x ).y - int( rnd * yvar )  + ( yvar - 1 ) / 2
    
    next x
    
next asdf

cls
screenset 1, 1
do
    
    for x = 0 to maxframes
    
        put( 0, 0 ), scr( x, 0 ), pset
        
        zz = timer + .1
        do: loop until timer > zz
        
    next x
    
    for x = maxframes to 1 step - 1
    
        put( 0, 0 ), scr( x, 0 ), pset
        
        zz = timer + .05
        do: loop until timer > zz
        
    next x

loop until multikey(1)

        
function math.distance (x1 as double, y1 as double, x2 as double, y2 as double) as double
    math.distance = sqr((x1-x2)^2 + (y1-y2)^2)
end function
[Image: freebasic.png]
Reply
#3
Very cool! 8)
But its not really simple graphics Tongue.
It's the difference between asking someone how much flour goes into pancakes, and handing them a sorry mix of oozing green goo and asking them to fix it." - Deleter

-Founder & President of the No More Religion Threads movement-
Reply
#4
true.....
[Image: freebasic.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)