Set x1,y1 values of a line to x2,y2 values of previously drawn line

edited April 2014 in How To...

Hello,

I am trying to draw an animated random zig zag made up of small lines. I want to set the x1,y1 values of each line to the x2,y2 values of the previously drawn line so that each line joins up. Is there a way to do this?

I've attached an image to help illustrate this. Screen Shot 2014-04-20 at 16.33.53

Tagged:

Answers

  • I think this will help you on the way:

    ArrayList<PVector> vecs = new ArrayList<PVector>();
    
    void setup() {
    }
    
    void draw() {
      background(255);
      PVector current, pre;
      if (vecs.size() >= 2) {
        for (int i = 1; i < vecs.size(); i++) {
          current = vecs.get(i);
          pre = vecs.get(i-1);
          line(pre.x, pre.y, current.x, current.y);
        }
      }
    }
    
    void mousePressed() {
      vecs.add(new PVector(mouseX, mouseY));
    }
    
  • Thanks, I am having trouble though as what I am trying to achieve is quite different to what your code produces although I am pretty sure I will need to use the PVector Array part but I can't work out how! The code I am using at the moment is below and I am trying to make it so that each line is a random zig zag rather than a straight line:

    int xPos = 0; 
    int yPos = 0;
    
    void setup () {
      size(600, 400);
      frameRate(160);
      background(255);
    }
    
    void draw () {
      stroke(0);
      line(xPos, yPos, xPos+10, yPos);
    
      if (xPos >= width) {
        xPos = 0;
        yPos = yPos+(int)random(10,50);
      } 
     else if (yPos >= height) {
        xPos = 0;
        yPos=0;
        //yPos = yPos+10;
        background(255);
      } 
      else {
        xPos++;
      }
    }
    
  • Here is one way you might do it, but the realistic solution needs an array or ArrayList as shown by @clankill3r

    int xPos = 0;
    int yPos = 0;
    
    void setup () {
      size(600, 400);
      frameRate(0.5);
    }
    
    void draw () {
      background(255);
      stroke(200);
      strokeWeight(2);
      line(0, height/2, width, height/2);
      stroke(0);
      strokeWeight(1);
      xPos = 0;
      yPos = height/2;
      while (xPos < width) {
        int xNextPos = xPos + round(random(10, 20));
        int yNextPos = yPos + round(random(-20, 20));
        yNextPos = constrain(yNextPos, 0, height/2);
        line(xPos, yPos, xNextPos, yNextPos);
        xPos = xNextPos;
        yPos = yNextPos;
      }
    }
    
  • I've never used an array list or PVector before so am i right in thinking that I am trying to essentially replace the void mousePressed part with something that puts the x2 and y2 values of a line after it is drawn into the array list and then sets these values as the x1 and y1 values of the next line drawn? (I'm new to this sorry!)

  • To draw a line you need four points. So to draw a multipart line you use the end points from the previous line as the starting point of the next line.

  • Thanks for all your help, but I think i'll have to learn more before I can work this one out!

Sign In or Register to comment.