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.
Page Index Toggle Pages: 1
A Grid Function (Read 2484 times)
A Grid Function
Nov 23rd, 2007, 4:24am
 
When developing Processing sketches, I often use graph paper to develop ideas --- I find that it's also useful to overlay a grid on a on-going processing sketch -- as a feature request, could this be added to the Processing Core? Also, perhaps an enhancement would be for the IDE to toggle the grid on and off.

here is the code I'm currently using:

void grid(int x, int y, int w, int h, int n, color c) {
 stroke(c);
 for (int ix=x; ix <= x+w; ix += n) {
   line(ix, y, ix, y+h);
 }

 for (int iy=y; iy <= y+h; iy += n) {
   line(x, iy, x+w, iy);
 }
}

void grid(int w, int h, int n) {
 grid(0,0,w,h,n,color(126,50));
}
Re: A Grid Function
Reply #1 - Nov 23rd, 2007, 5:18pm
 
please don't double post.
Re: A Grid Function
Reply #2 - Mar 10th, 2008, 2:19am
 
Good Idea!
I expanded your method to have more options:
Code:
void setup() {
size(400,400);
background(153);
noLoop();
}//setup()

void draw() {
gridWH(100,100,20);
gridWH(4,4,130,130,20);
gridMNC(8,8,4,4,20,color(230,60,100,80));
gridMNC(110,110,5,9,20,color(100,220,70,180));
}//draw()

//========================================================
// grid of given width/height

void gridWHC(int x0, int y0, int w, int h, int cellw, color c) {
stroke(c);
for (int iy=y0; iy<=y0+h; iy+=cellw) line(x0, iy, x0+w, iy);
for (int ix=x0; ix<=x0+w; ix+=cellw) line(ix, y0, ix, y0+h);
}//gridWHC()

void gridWHC(int w, int h, int cellw, color c) {
gridWHC(0,0,w,h,cellw,c);
}//gridWHC()

void gridWHC(int x0, int y0, int w, int h, color c) {
gridWHC(x0,y0,w,h,10,c);
}//gridWHC()

void gridWHC(int w, int h, color c) {
gridWHC(0,0,w,h,10,c);
}//gridWHC()

void gridWH(int x0, int y0, int w, int h, int cellw) {
gridWHC(x0,y0,w,h,cellw,color(20,100,100,80));
}//gridWH()

void gridWH(int w, int h, int cellw) {
gridWHC(0,0,w,h,cellw,color(20,100,100,80));
}//gridWH()

void gridWH(int x0, int y0, int w, int h) {
gridWHC(x0,y0,w,h,10,color(20,100,100,80));
}//gridWH()

void gridWH(int w, int h) {
gridWHC(0,0,w,h,10,color(20,100,100,80));
}//gridWH()

//========================================================
// grid of given #row/#column

void gridMNC(int x0, int y0, int mrow, int ncol, int cellw, color c) {
stroke(c);
int x1=x0+ncol*cellw;
int y1=y0+mrow*cellw;
for (int i=0, iy=y0; i<=mrow; i++, iy+=cellw) line(x0, iy, x1, iy);
for (int i=0, ix=x0; i<=ncol; i++, ix+=cellw) line(ix, y0, ix, y1);
}//gridMNC()

void gridMNC(int mrow, int ncol, int cellw, color c) {
gridMNC(0,0,mrow,ncol,cellw,c);
}//gridMNC()

void gridMNC(int x0, int y0, int mrow, int ncol, color c) {
gridMNC(x0,y0,mrow,ncol,10,c);
}//gridMNC()

void gridMNC(int mrow, int ncol, color c) {
gridMNC(0,0,mrow,ncol,10,c);
}//gridMNC()

void gridMN(int x0, int y0, int mrow, int ncol, int cellw) {
gridMNC(x0,y0,mrow,ncol,cellw,color(20,100,100,80));
}//gridMN()

void gridMN(int mrow, int ncol, int cellw) {
gridMNC(0,0,mrow,ncol,cellw,color(20,100,100,80));
}//gridMN()

void gridMN(int x0, int y0, int mrow, int ncol) {
gridMNC(x0,y0,mrow,ncol,10,color(20,100,100,80));
}//gridMN()

void gridMN(int mrow, int ncol) {
gridMNC(0,0,mrow,ncol,10,color(20,100,100,80));
}//gridMN()
Page Index Toggle Pages: 1