Problem with animation of graph.
in
Programming Questions
•
2 years ago
Hoi everybody!
i'm stuck with something simple...
my sketch should draw a graph, but cannot exceed a certain area. So when the max width is reached the graph should shift to the left.
- PVector[] my_array = new PVector[100];
- int val;
- int j;
- void setup() {
- frameRate(1);
- size(400,400);
- //initialize some values
- for (int i = 0; i < my_array.length; i++) {
- my_array[i] = new PVector();
- my_array[i].x = i;
- my_array[i].y = i*2;
- }
- // printArray(my_array);
- //
- // shiftAndAdd(my_array, 10);
- // printArray(my_array);
- //
- // shiftAndAdd(my_array, 999);
- // printArray(my_array);
- }
- void draw(){
- background(0);
- stroke(255);
- val = int(random (100,120));
- my_array = shiftAndAdd(my_array, val);
- for(int i = 0; i < my_array.length; i++){
- point(my_array[i].x,my_array[i].y);
- }
- }
- // shift all the values to the left, and set the new value at the end
- PVector[] shiftAndAdd(PVector a[], int val){
- int a_length = a.length;
- System.arraycopy(a, 1, a, 0, a_length-1);
- for(int i = 0; i < a.length; i++){
- a[i].x = i;
- }
- printArray(a);
- a[a_length-1].y = val;
- return a;
- }
- // function to print array values in a row
- void printArray(PVector a[]) {
- for (int i = 0; i < a.length; i++) {
- print("x: " + a[i].x + " y: " + a[i].y + ", ");
- }
- println("\n----");
- }
the random value val should be added to the array form which the line is drawn. for some reason this does not happen... The graph shifts, but without the las added item...
i have no idea why this is; please help!
thanks
1