Node Garden XST Javascript

This is my version of Node Garden in XST Lib in JavaScript.

winScreen(600, 600, false);
setTitle('Node Garden');
setFps(30);

var maxDist = 100;
var nodes = [];
var i = 0;
var h = getScreenWidth();
var w = getScreenHeight();

for(i = 0; i < maxDist+50; i++) {
    nodes.push({
        x: rnd(h),
        y: rnd(w),
        vx: rnd(2)-1,
        vy: rnd(2)-1,
        color: getRandomColor()
    });
}

function main() {
    clear();
    for(var i = 0; i < maxDist+50; i++) {         var node = nodes[i];         node.x += node.vx;         node.y += node.vy;         if (node.x > w) {
            node.x = 0;
        }
        else if (node.x < 0) {             node.x = w;         }         if (node.y > h) {
            node.y = 0;
        }
        else if (node.y < 0) {
            node.y = w;
        }
        fillCircle(node.x, node.y, 2,node.color);
    }

    for (var i = 0; i < nodes.length - 1; i++) {
        var nodeA = nodes[i];
        for(var j = i + 1; j < nodes.length; j++) {
            var nodeB = nodes[j];
            var dist = distance(nodeB.x, nodeB.y, nodeA.x, nodeA.y);
            if (dist < maxDist) {
                line(nodeA.x, nodeA.y, nodeB.x, nodeB.y, (1-dist/maxDist), nodeA.color);
            }
        }
    }
}

See it here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s