Array 2D

array-2d.pde
/**
 * Array 2D.
 *
 * Demonstrates the syntax for creating a two-dimensional (2D) array.
 * Values in a 2D array are accessed through two index values.
 * 2D arrays are useful for storing images. In this example, each dot
 * is colored in relation to its distance from the center of the image.
 */

float* distances;
float maxDistance;
int spacer;

void setup() {
    size(640, 360);
    maxDistance = dist(width/2, height/2, width, height);
    distances = new float[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            float distance = dist(width/2, height/2, x, y);
            distances[x + y * width] = distance / maxDistance * 255;
        }
    }
    spacer = 10;
    strokeWeight(6);
    noLoop();
}

void draw() {
    background(0);
    for (int y = 0; y < height; y += spacer) {
        for (int x = 0; x < width; x += spacer) {
            stroke(distances[x + y * width]);
            point(x + spacer/2, y + spacer/2);
        }
    }
}