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 › Problem making a for loop of triangles
Page Index Toggle Pages: 1
Problem making a for loop of triangles (Read 667 times)
Problem making a for loop of triangles
Nov 24th, 2009, 4:57pm
 
Hi, I'm very new to Processing, have only been using it for two weeks. I'm still having a bit of trouble with using loops in generating 2D line images.

I have some code I'm writing right now and I can't work out why the triangles won't loop the way I'd like them too.

Is anyone able to help by telling me how I can create a window of triangles from one triangle's parameters?

The triangle's parameters before I try and generalise them into a for loop are triangle(0,25,50,0,50,25); and I just want the next triangle to start right from the edges of the proceeding triangle.

Any help would be greatly appreciated thanks.


void setup()
{
 size(500,500);
 background(0);
 stroke(250);
 strokeWeight(0.5);
 smooth();
 
}

int space=50;

void tri()
{
 background(0);
 noFill();
 strokeWeight(1);
 
 for (int x = 0; x < 10; x = x+1)
 {
   for (int y = 0; y < 10; y = y+1)
   {
     triangle(0,3*y*(space/2),x*space,0,x*space,3*y*(space/2));
   }
 }
}

void circle()
{
 {
 translate(space/2,space/2);
 
 noFill();
 stroke(255);
 strokeWeight(0.5);
 
 for(int x = 0; x < 10; x = x+1)
 {
   for(int y = 0; y < 10; y = y+1)
   {
     ellipse(x*space,y*space,space,space);
   }
 }
 }
}

void draw()
{
 
 background(0);
 
 tri();
 circle();
 
}


Re: Problem making a for loop of triangles
Reply #1 - Nov 24th, 2009, 5:43pm
 
if you closely look at your triangle you can tell that it can not work.
there are 2 variables that are always 0 for example...

this is one way to do it :
for (int x = 0; x < 10; x = x+1)
{
  for (int y = 0; y <= 10; y = y+1)
  {
     triangle(space*x,space*y,space*x+(space/2),y*space-space,space*x+space,space*y);

  }
}

another maybe easier one would be to simply write
for (int x = 0; x < 10; x = x+1)
{
  for (int y = 0; y <= 10; y = y+1)
  {
     pushMatrix();
     translate(x*space,y*space);
     triangle(0,space,space/2,0,space,space);
     popMatrix();
  }
}
Re: Problem making a for loop of triangles
Reply #2 - Nov 24th, 2009, 5:44pm
 
oh and instead of x = x+1 you can just write x++

and there is a command called ellipseMode(CENTER);
that centers the ellipse so you dont have to do that translation.
Re: Problem making a for loop of triangles
Reply #3 - Nov 24th, 2009, 7:49pm
 
oh right that's fantastic, yeah and centering the ellipse like that works well. thanks so much for your help- that's really kind!

Have a good day!
Page Index Toggle Pages: 1