We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › 2d grid with vertex
Page Index Toggle Pages: 1
2d grid with vertex (Read 494 times)
2d grid with vertex
Jan 28th, 2010, 10:20am
 
Hi. I would like to draw a 2-dimensional grid with the vertex function.
I need to change the position of some of the intersection points later on, so I store their position in an array.
Unfortunately, the result looks nothing like a grid.. What do I have to change?

Quote:
int xPos[] = new int[200];
int yPos[] = new int[200];
int aPos = 0;

void setup()
{
    size(150, 150);

    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < 9; j++)
        {
            addPoint(i, j);
            addPoint(i+1, j);
            addPoint(i, j+1);
            addPoint(i+1, j+1);
        }
    }
}

void draw()
{
    background(80);
    translate(30, 30);

    beginShape();

    for(int i = 0; i < aPos + 1; i++)
    {       
        vertex(xPos[i] * 10, yPos[i] * 10);      
    }

    endShape();
}

void addPoint(int x, int y)
{
    boolean addValue = true;

    for(int i = 0; i < xPos.length; i++)
    {
        if(
        xPos[i] == x &&
            yPos[i] == y
            )
        {
            addValue = false;
        }
    }

    if(addValue)
    {
        aPos++;     

        xPos[aPos] = x;
        yPos[aPos] = y;
    }
}
Re: 2d grid with vertex
Reply #1 - Jan 28th, 2010, 12:11pm
 
Well, a first step would be to use beginShape(LINES); instead.
Not enough, but better...

To be honest, I don't understand your algorithm...
Re: 2d grid with vertex
Reply #2 - Jan 28th, 2010, 12:12pm
 
you're drawing all your points as the same Shape (between beginShape and endShape). so it's joining them ALL together, hence the zigzag lines. it's also filling the resultant shape, hence the white.

try beginShape(LINES) which will join pairs of points (i doubt this is exactly what you want but it'll be closer. you probably want to draw them in groups using two nested for loops rather than 1 big one)

also put noFill(); in setup() to turn off the white.

(too long, too slow... does he never sleep?)
Page Index Toggle Pages: 1