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 › point = barrier for curve colour
Page Index Toggle Pages: 1
point = barrier for curve colour (Read 476 times)
point = barrier for curve colour
Sep 14th, 2006, 7:26pm
 
Hi,

i am trying to make a curve change its color. A point that moves from one end to the other, is to function as a "barrier": from start point of the curve to the moving point show color A, from the moving point to endpoint of the curve show color B.

1. So far, the point (shown by an ellipse) doesn´t move.
2. I don´t get that curve change its color at the moving point.

I´d appreciate any help.

Here´s the code:

position path[]=new position[9];

void setup()
{
 size(400,400);
 stroke (255);
 
 path=new position[9];
for(int i=0; i<path.length; i++)  
{
  path[0]= new position (100,100);
  path[1]= new position (100,100);
  path[2]= new position (170,200);
  //stroke(0,255,0);
  path[3]= new position (300,150);
  path[4]= new position (390,20);
  path[5]= new position (25,370);
  path[6]= new position (150,200);
  path[7]= new position (200,50);
  path[8]= new position (200,50);
   
   
 }
 }
 
 
 void draw()
 {
   background(0);
   
   beginShape(LINE_STRIP);
   for(int i=0;i<path.length;i++)
   {
//stroke(0,255,0);
curveVertex(path[0].x,path[0].y);
curveVertex(path[1].x,path[1].y);
curveVertex(path[2].x,path[2].y);
curveVertex(path[3].x,path[3].y);
//stroke(255,0,0);
curveVertex(path[4].x,path[4].y);
curveVertex(path[5].x,path[5].y);
//stroke(0,0,255);
curveVertex(path[6].x,path[6].y);
curveVertex(path[7].x,path[7].y);
curveVertex(path[8].x,path[8].y);
//stroke (255);
//ellipse(path[i].x,path[i].y,5,5);
break;
   }
   endShape();
   
   
   
   position cp=new position(path[1]);

 for (int i = 0; i< path.length; i++)
   {  
   float t = .01 ;
   cp=new position(curvePoint(path[i].x,path[i+1].x,path[i+2].x,path[i+3].x,t),curvePoint(path[i].y,path[i+1].y,path[i+2].y,path[i+3].y,t));
   
   break;
   }
   
   
   cp.x += cp.x;
   cp.y += cp.y;
   
   
   noFill();
   stroke(255,0,0);
   ellipse(cp.x,cp.y,10,10);
   stroke(255);
 
   }


class position        //position class that stores 2 values x and y
{
float x,y;
 position(){}
 
 position(float tx,float ty)
 {
   x=(tx);
   y=(ty);
 }
 
 position(position p)
 {
   if(p!=null)
   {
x=p.x;
y=p.y;
   }
 }
 }


Thanks in advance

brin
Re: point = barrier for curve colour
Reply #1 - Sep 15th, 2006, 1:23pm
 
I'm not sure if I understand exactly what you want to do, but if you have an array of points and you want to display an ellipse at the points one at a time, you do not need to use a "for" loop.  Instead, you can make use of the fact the draw() functions loop for you and implement a counter variable to keep track of an index in the array.

This is not complete, but the idea is something like:

Code:

int counter = 0;

void draw() {
counter = (counter + 1) % path.length;
position p = new position(path[counter]);
ellipse(p.x,p.y);
}


Just as sidenote, it's conventional to name your classes with a capital letter, i.e. class Position instead of class position.

good luck!
Dan
Re: point = barrier for curve colour
Reply #2 - Sep 15th, 2006, 4:31pm
 
thank you!

i worked it in like this:

int counter = 0;
Position path[]=new Position[9];


void setup()
{
 size(400,400);
 stroke (255);

 path=new Position[9];
 

 }
 
 
 void draw()
 {
       …


 Position cp = new Position(path[counter]);// ArrayIndexOutOfBoundsExeption

 counter = (counter + 1) % path.length;

   float t = .01 ;

   cp=new Position(curvePoint(path[counter].x,path[counter+1].x,path[counter+2].x,path[counter+3].x,t),curvePoint(path[counter].y,path[counter+1].y,path[counter+2].y,path[counter+3].y,t));
 

   }





However, the path definition in "setup()" causes a "ArrayIndexOutOfBoundsExeption" in the line where "cp" is defined. Is there anything missing?
Re: point = barrier for curve colour
Reply #3 - Sep 16th, 2006, 2:53pm
 
"path[counter+1].x" will give you an out of bounds error.   Say your array has 10 spaces (index values 0 through 9).  Counter goes from 0 to 9.  When counter equals nine, you are asking for the counter+1 element (i.e. 10) which doesn't exist!

If you need to ask for counter+1, then you should only allow counter to go up to 8 (i.e. path.length-2).

Code:

if (counter > path.length-2) {
counter = 0;
}


or

Code:

counter = (counter + 1) % (path.length-1);
Re: point = barrier for curve colour
Reply #4 - Sep 17th, 2006, 3:20pm
 
Hi,

after having simplified the whole code, i got it finally working! Thanks a lot!
Page Index Toggle Pages: 1