Monday, 29 April 2013

PASCAL CODE

https://www.khanacademy.org/cs/pascals-triangle/803149756




background(10, 177, 255);

// The modular base to use for calculating the color
var base = 4;

// The radius of each circle
var radius = 4;

// The number of rows in the triangle
var numRows = 60;

var dy = sqrt(3) * radius;
var rowAboveColors = [];

for (var row = 0; row < numRows; row += 1) {
    var currentRowColors = [];
    for (var col = 0; col <= row; col += 1) {
        var currentColor;

        if (col === 0 || col === row) {
            currentColor = 1;
        } else {
            var leftColor = rowAboveColors[col - 1];
            var rightColor = rowAboveColors[col];
            currentColor = (leftColor + rightColor) % base;
        }

        if (currentColor === 0) {
            fill(255, 0, 0);
        } else if (currentColor === 1) {
            fill(0, 0, 0);
        } else if (currentColor === 2) {
            fill(0, 255, 0);
        } else if (currentColor === 3) {
            fill(0, 115, 255);
        } else if (currentColor === 4) {
            fill(255, 132, 0);
        } else if (currentColor === 5) {
            fill(0, 247, 255);
        } else {
            fill(255, 255, 255);
        }

        ellipse(200 + radius * (2 * col - row), dy * (row + 1), radius * 2, radius * 2);
        currentRowColors[col] = currentColor;
    }

    rowAboveColors = currentRowColors;
}

No comments:

Post a Comment