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 › For loops examples. Post your ones for videotuto !
Page Index Toggle Pages: 1
For loops examples. Post your ones for videotuto ! (Read 1304 times)
For loops examples. Post your ones for videotuto !
Aug 30th, 2009, 7:16am
 
Hi , i am learning processing , and on the learning way i want to share what i learn with others as i dont see many videotutorials on processing as there should be.


So i would like if you could write some very basic for loops, i want to get as many examples as posible they should be something like :
Code:

size(500,500);
smooth();
background(255);




for(int i=0;i<width;i+=10)
{
 ellipse(width-i,i,10,10);
}


as you can see , newbies like me can understand this pretty good.

so the examples should be very basic, just one for loop by example i think it would be good so we can focus on the for loop but if you have a good example with 3 for loops go ahead.

PLEASE make a small explanation of the example,what i doing what where. thanx !

Then i will try to explain them in a video tutorial, i will try to do this with  variables, if else statements  and so on.

Please give it a try !

thanx !
Re: For loops examples. Post your ones for videotuto !
Reply #1 - Aug 30th, 2009, 8:28am
 
what about ellipses or spheres in 2 and 3 dimensions?

void setup(){
 size(500,500);
 background(0);

 translate(125,125);
 for(int i=0;i<25;i++){
   for(int j=0;j<25;j++){
    fill(i*10,255,j*10);  
     ellipse(i*10,j*10,10,10);
   }
 }
}
Re: For loops examples. Post your ones for videotuto !
Reply #2 - Aug 30th, 2009, 8:34am
 
maybe you can come up with something better looking, but like this :


float xmag, ymag = 0;
float newXmag, newYmag = 0;

void setup()
{
 size(640, 360, P3D);
 noStroke();
 colorMode(HSB,5);
}

void draw() {  
 background(2);

 pushMatrix();
 translate(width/2, height/2, -30);

 newXmag = mouseX/float(width) * TWO_PI;
 newYmag = mouseY/float(height) * TWO_PI;

 float diff = xmag-newXmag;
 if (abs(diff) >  0.01) {
   xmag -= diff/4.0;
 }

 diff = ymag-newYmag;
 if (abs(diff) >  0.01) {
   ymag -= diff/4.0;
 }

 rotateX(-ymag);
 rotateY(-xmag);
 scale(3);

 for(int i=0;i<5;i++){
   for(int j=0;j<5;j++){
     for(int k=0;k<5;k++){
       fill(i,j,k);  
       pushMatrix();
       translate(i*10,j*10,k*10);
       sphere(2);
       popMatrix();
     }
   }
 }

 popMatrix();
}



Re: For loops examples. Post your ones for videotuto !
Reply #3 - Aug 30th, 2009, 4:08pm
 
Cedric wrote on Aug 30th, 2009, 8:34am:
maybe you can come up with something better looking, but like this :


float xmag, ymag = 0;
float newXmag, newYmag = 0;

void setup()
{
 size(640, 360, P3D);
 noStroke();
 colorMode(HSB,5);
}

void draw() {  
 background(2);

 pushMatrix();
 translate(width/2, height/2, -30);

 newXmag = mouseX/float(width) * TWO_PI;
 newYmag = mouseY/float(height) * TWO_PI;

 float diff = xmag-newXmag;
 if (abs(diff) >  0.01) {
   xmag -= diff/4.0;
 }

 diff = ymag-newYmag;
 if (abs(diff) >  0.01) {
   ymag -= diff/4.0;
 }

 rotateX(-ymag);
 rotateY(-xmag);
 scale(3);

 for(int i=0;i<5;i++){
   for(int j=0;j<5;j++){
     for(int k=0;k<5;k++){
       fill(i,j,k);  
       pushMatrix();
       translate(i*10,j*10,k*10);
       sphere(2);
       popMatrix();
     }
   }
 }

 popMatrix();
}





that looks cool , but is that simple thing supposed to go so slow im on a new mac book pro. and it is pretty slow , is this norma
Re: For loops examples. Post your ones for videotuto !
Reply #4 - Aug 30th, 2009, 4:22pm
 
you are right, we should add a  sphereDetail(10);
run faster now ?
Re: For loops examples. Post your ones for videotuto !
Reply #5 - Aug 31st, 2009, 7:54am
 
great keep them coming and by the way yhe ones that have allready posted ab example could you explain them a little bit

thanx!Cedric wrote on Aug 30th, 2009, 4:22pm:
you are right, we should add a  sphereDetail(10);
run faster now

Re: For loops examples. Post your ones for videotuto !
Reply #6 - Aug 31st, 2009, 8:22am
 
You might recognize this from the reference pages.
http://processing.org/reference/set_.html

Using 2D coordinates for direct access to pixels.
Code:
size(256,256);

loadPixels(); //make sure pixels[] is available

//This:
///*
//1D array access
for (int idx=0; idx<pixels.length; idx++) {
 pixels[idx] = idx | 0xff000000; //mask with 100% alpha to see the colors
}
//*/

//Is the same as:
/*
//From 2D coordinates to 1D array
for (int x=0; x<width; x++) {
 for (int y=0; y<height; y++) {
   
   int idx = y * width + x; //calculate the offset
   pixels[idx] = idx | 0xff000000;
   
 }
}
//*/

updatePixels();
Re: For loops examples. Post your ones for videotuto !
Reply #7 - Sep 3rd, 2009, 4:30am
 
Allright , guy thanx ! if someone could post  just a couple more , but even simpler , would be great , then i have to undestand them and try to explain them in a videotut.

thanx !!
Re: For loops examples. Post your ones for videotuto !
Reply #8 - Sep 3rd, 2009, 7:57am
 
I suggest adding some spaces, it helps in reading code.
Also using some constants helps in understanding relationship of various uses:
Code:
size(500, 500); 
smooth();
background(255);
fill(200, 0, 0);
int EL_SIZE = 20;

for (int i = 0; i < width; i += EL_SIZE)
{
ellipse(width - i, i, EL_SIZE, EL_SIZE);
}

You can also make variations on some parameters, like fill color:
Code:
size(500, 500); 
smooth();
background(255);
int EL_SIZE = 20;
color START_COLOR = color(0); // Black
color END_COLOR = color(255, 0, 0); // Red

for (int i = 0; i < width; i += EL_SIZE)
{
fill(lerpColor(START_COLOR, END_COLOR, (float) i / width));
ellipse(width - i, i, EL_SIZE, EL_SIZE);
}

or ellipse size:
Code:
fill(END_COLOR);

for (int i = 0; i < width; i += EL_SIZE)
{
float elSize = EL_SIZE * 1.414 * i / width;
ellipse(width - i, i, elSize, elSize);
}

or on each parameter:
Code:
for (int i = 0; i < width; i += EL_SIZE)
{
float elSize1 = EL_SIZE * 3.0 * i / width;
float elSize2 = EL_SIZE * (1.00 - 2.0 * i / width);
ellipse(width - i, i, elSize1, elSize2);
}

It shows it is interesting to play with variations on code.
Page Index Toggle Pages: 1