Voronoi - Plotting Vertices

Hi all,

I've been experimenting the Voronoi Diagrams, and the Manhattan Distance function and ended up with some interesting results. My current (simple) code is below. I have an array of vectors, an array of colours. Each pixel on the screen is set to the colour at the index of the closest vector.

I'm now at the point where I would like to plot some geometry - I would like to use vertices to create the pattern from polygons, but I'm stuck, and at a loss as to how to achieve it. A lot of the reading on Voronoi is a on the heavy side, and what I'm after is really a simple explanation of the process.

Would anyone be kind enough to point me in roughly the right direction for this?

Thanks for your time,

Dave

frame

size(800, 800);
colorMode(HSB, 100);

int numSites = 20;
boolean manhattan = true;

PVector[] sites = new PVector[numSites];
color[] colors = new color[numSites];

for (int i = 0; i < numSites; i++) 
{
    colors[i] = color(random(100), 60, 100);
    sites[i] = new PVector(random(width), random(width));
}

for (int x = 0; x < width; x++) 
{
    for (int y = 0; y < height; y++) 
    {
        float minDistance = Float.MAX_VALUE;
        int closestIndex = 0;

        for (int i = 0; i < numSites; i++) 
        {
            float distance = manhattan ? abs(sites[i].x - x) + abs(sites[i].y - y) : dist(x, y, sites[i].x, sites[i].y);

            if (distance < minDistance) {
                closestIndex = i;
                minDistance = distance;
            }
        }
        set(x, y, colors[closestIndex]);
    }
}

for (int i = 0; i < numSites; i++) {
    ellipse(sites[i].x, sites[i].y, 5, 5);
}

Answers

Sign In or Register to comment.