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 & HelpPrograms › how to incrementally add to my y vars... (newb)
Page Index Toggle Pages: 1
how to incrementally add to my y vars...? (newb) (Read 608 times)
how to incrementally add to my y vars...? (newb)
Sep 28th, 2005, 10:18am
 
I'm just beginning processing, not to mention programming, and I'm trying to draw a grid. I've found equations for the first row and column and the diagonal, but I don't know a how to do a real grid using simple equations. So I figured I'd have it draw a new row each frame for the first several, but I can't get the T var section to do anything, let alone make it draw the subsequent ranks.

int x1; int x2; int x3; int x4;
int y1; int y2; int y3; int y4;

int a; int b; int c; int d; int e; int f; int g; int h;

int T;

void setup()
{
 size(210, 210);
 background(102);
 framerate (5);
}

void draw()
{
 T = frameCount;

 {
   for (T = 2; T <= 7; T++)
  y1 += 30;
  y2 += 30;
  y3 += 30;
  y3 += 30;
 
 }
       
for (int i = 1; i <= 6; i++)
{
     
 x1 = 0; y1 = 0;
 x2 = 30; y2 = 0;
 x3 = 30; y3 = 30;
 x4 = 0; y4 = 30;
 
 quad( x1, y1, x2, y2, x3, y3, x4, y4);
     
 a = (x1 + 30*i); b = (y1);
 c = (x2 + 30*i); d = (y2);
 e = (x3 + 30*i); f = (y3);
 g = (x4 + 30*i); h = (y4);
 
 quad( a, b, c, d, e, f, g, h);
 
}

}

any suggestions and/or pointers towards code for a grid would be greatly appeciated
Re: how to incrementally add to my y vars...? (new
Reply #1 - Sep 28th, 2005, 3:47pm
 
Code:

int x,y;
void setup(){
size(400,400);
}
void draw(){
background (250);
setStage();
x = (x+5)%405;
y = (y+5)%405;
fill(0);
rect(x,y,5,5);
}
void setStage(){
stroke(220);
//voila, ici un grid
for(int i = 0; i < 405; i += 5) {
line (i,0,i,400);
line (0,i,400,i);
}
stroke (180);
line (200,0,200,400);
line (0,200,400,200);
}

Wink

Look up arrays too. You can use a for loop to skip through them all instead of going x1=,x2=,x3=. Very helpful when it comes to drawing a lot to the screen.
Re: how to incrementally add to my y vars...? (new
Reply #2 - Sep 29th, 2005, 3:39am
 
thanks st33d, that's wonderfully simple. if I go the array route, will I eventually be able to have identifiable 'cells' with labels and states?
Re: how to incrementally add to my y vars...? (new
Reply #3 - Sep 29th, 2005, 10:31pm
 
For that you would want to use objects. You create objects by making a mould called a class and then punch objects out of that mould (like a cookie cutter).

It's easier at first to think of an object as a program within a program. So with an array of objects you could have hundreds of little programs running around.

As you get used to using objects you should start thinking of them the same way you use int, float and String. They all have methods and properties you can call upon. If you're not au fait with this then there's plenty of tutorials out there which will cover it and help you get your head round it.

http://www.processing.org/learning/examples/arrayobjects.html

And here's another example:
Code:

Draggable [] example;
void setup(){
size(400,400);
example = new Draggable[10];
for (int i = 0; i < example.length; i++){
example[i] = new Draggable (i*20,i*20,20,color(100,100,200),color(100,200,200),"rect");
}
}
void draw(){
background(144,130,140);
for (int i = 0; i < example.length; i++){
example[i].draw();
example[i].update();
}
}
class Draggable {
//properties
int x,y,z,size,c1,c2;
String mode;
boolean locked = false;
//setup - called a constructor
Draggable (int x, int y, int size, color c1, color c2, String mode) {
this.x = x;
this.y = y;
this.size = size;
this.c1 = c1;
this.c2 = c2;
this.mode = mode;
}
//methods
void draw(){
noStroke();
if (locked || over()){
fill(c1);
}
else{
fill(c2);
}
if (mode == "rect"){
rect(x,y,size,size);
}
if (mode == "ellipse"){
ellipse(x,y,size,size);
}
}
void update(){
if (over() && mousePressed){
locked = true;
}
if (locked){
x = mouseX;
y = mouseY;
}
if (!mousePressed){
locked = false;
}
}
boolean over(){
if (mouseX <= x+size && mouseX >= x && mouseY <= y+size && mouseY >= y){
return true;
}
else{
return false;
}
}
}
Page Index Toggle Pages: 1