Question About correcting my code Pulses

Hi all,
I am a beginner in processing. could you please help me to correct the code.
the question is:
write the reverse motion of the previous example (using Processing
array function is not allowed)

And the below code is the baseline, but I need to reverse it without using Array.

int[]	arrY;	
void	setup(){	
		size(600,	200);	
		smooth();	
		arrY	=	new	int[width];	
}	
void	draw(){	
		background(204);	
		for(int	i	=	arrY.length-1;	i	>	0;	i--){				//	shifting	
				arrY[i]	=	arrY[i-1];	
		}	
		arrY[0]	=	constrain(mouseY,	0,	height-1);		//	new	value	
		for(int	i	=	1;	i	<	arrY.length;	i++){						//	display	
				line(i,	arrY[i],	i-1,	arrY[i-1]);	
		}	
}

My code is here:

int right; 
int i;
void  setup() { 
  background(204); 
  i=width-1;
  size(600, 200);  
  smooth();  
  right  =  constrain(mouseY, 0, height-1);
}  
void  draw() {  

    

  int left  =  constrain(mouseY, 0, height-1); 

  line(i, right, i-1, left);
  right=left;
  i--;
}

But there is a problem.I cannot save the previous frame just like the above code. Not it is reverse but not bring the previous frame like a shifting in Array. Do anyone know with what commend I could correct my code to reach the exact reverse of the above code without using the array.
Thank you

1 Like

Hi gisue,

Can you please format your code?

To do that, click on the pen icon in the bottom right corner of your previous post to edit it. Then select your code and hit the following icon </> in the textbox toolbar.

Don’t forget to hit ctrl+t in the processing IDE to properly indent every thing.

If you’re not allowed to use an array, how are you going to remember what the previous values were?

Could you use an image? … How about a lot of variables?

Thank you for your answer.I change the format.

I ask my professor. he told me we can use array command(just like the baseline he gave). But not array Function in processing.
Now, could you please tell me ,how can I edit my code to get reverse result with array?

There are a couple of approaches that would work.

The first, and by far the easiest, it to translate the origin of your coordinate system to the top right corner (instead of the top left corner), an then change the scaling of the coordinate system so that the positive X direction goes in the opposite direction, that is, scale it by -1.

The other approach is to loop over your values the same as the example code, but draw your lines not at their x positions, but at their position taken away from the sketch width. This is the math version of the first approach.

Or maybe you could take the time to understand the example code. What is the array? What do its values represent? What are the loops doing? What is shifted? Is a value lost? How is the lost value replaced? Once you understand how it works, you will easily be able to make it go the other way. You will change the loop that does the shifting. The value lost will be in a different place. Does the drawing have to change?

The first approach adds only two lines of code.
The second approach changes only one line of code.
The third approach only changes three lines of code.

So, pick an approach and try it yourself.

3 Likes