A grid of normalised Vectors
in
Programming Questions
•
10 months ago
Hello,
I am trying to achieve a grid of normalized vectors.
Just for the first step I want a grid of normalised lines that could show the direction where the mouse is.
For example, the wind diagram is what I image as the result (where little arrows show directionality of the wind).
Just like in D.Shiffman's book cover: ---> D.S. BOOK COVER.
I tried a script written by Daniel Shiffman. He uses PVector in explaining vector logics in processing.
So what I tried was: an addition to the code using "for"loop to get a grid of points.
But when I tried to normalize a grid of lines , I did not get the result I wanted.
1st script is Daniel Shiffman script,
2nd script is written by me.
Why it does not work? And I really need Vector not coordinates, really....
Thank you in advance:)
I am trying to achieve a grid of normalized vectors.
Just for the first step I want a grid of normalised lines that could show the direction where the mouse is.
For example, the wind diagram is what I image as the result (where little arrows show directionality of the wind).
Just like in D.Shiffman's book cover: ---> D.S. BOOK COVER.
I tried a script written by Daniel Shiffman. He uses PVector in explaining vector logics in processing.
So what I tried was: an addition to the code using "for"loop to get a grid of points.
But when I tried to normalize a grid of lines , I did not get the result I wanted.
1st script is Daniel Shiffman script,
2nd script is written by me.
Why it does not work? And I really need Vector not coordinates, really....
Thank you in advance:)
- void setup() {
size(800,200);
smooth();
}
void draw() {
background(255);
// A vector that points to the mouse location
PVector mouse = new PVector(mouseX,mouseY);
// A vector that points to the center of the window
PVector center = new PVector(width/2,height/2);
// Subtract center from mouse which results in a vector that points from center to mouse
mouse.sub(center);
// Normalize the vector
mouse.normalize();
// Multiply its length by 50
mouse.mult(150);
translate(width/2,height/2);
// Draw the resulting vector
stroke(0);
strokeWeight(2);
line(0,0,mouse.x,mouse.y);
}
float[][] distances;
float maxDistance;
int spacer;
void setup() {
size(800,200);
smooth();
maxDistance = dist(width/2, height/2, width, height);
distances = new float[width][height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
float distance = dist(width/2, height/2, x, y);
distances[x][y] = distance/maxDistance * 255;
}
}
spacer = 100;
}
void draw() {
background(255);
stroke(0);
strokeWeight(2);
PVector mouse = new PVector(mouseX,mouseY);
PVector center = new PVector(width/2,height/2);
for (int y = 0; y < height; y += spacer) {
for (int x = 0; x < width; x += spacer) {
PVector startpoint = new PVector(x + spacer/2,y + spacer/2);
//here I am trying to normalize the vector
mouse.normalize();
mouse.mult(150);
line(startpoint.x,startpoint.y,mouse.x,mouse.y);
}
}
}
1