PVector and mouse

edited November 2017 in Questions about Code

hello, I am a beginner in coding with processing I'm following Shiffman channel on youtube, great guy!!

I got stuck with the following tutorial 1.4: Vector Math II - The Nature of Code

I wanted to expand the script but didn't succeed I can't normalize the length of the lines

here is the code below what I want to obtain and what I get so far any idea?

void setup() {
  size(800,600);
}

void draw(){
    background(0);

  for(int x = 0; x < width; x = x+20) {
    for(int y = 0; y < height; y = y+20) {
      stroke(255);
      PVector mouse = new PVector(mouseX,mouseY);
      PVector loc = new PVector(x,y);
      line(x,y-20,mouse.x,mouse.y);
      mouse.sub(loc);
      mouse.normalize();
      mouse.mult(10);
    }
  }
}

Screen Shot 2017-11-09 at 12.14.55

Screen Shot 2017-11-09 at 12.14.00

Tagged:

Answers

  • you normalise AFTER drawing the line.

  • Thank you Koogs I tried to put the line before as well, the result is still wrong.. any Idea?

  • Answer ✓

    Solved!!! small step but great satisfaction! thank you!

    void setup() {
      size(800,600); 
    }
    
    void draw(){
      background(0);
      for(int x = 20; x < width; x = x+20) {
        for(int y = 20; y < height; y = y+20) {
          stroke(255);
          PVector mouse = new PVector(mouseX,mouseY);
          PVector loc = new PVector(x,y);
          mouse.sub(loc);
          mouse.normalize();
          mouse.mult(10);
          line(x,y,x+mouse.x,y+mouse.y);
        }
      }
    }
    
Sign In or Register to comment.