Nature of Code exercise 6.6 help

edited July 2014 in How To...

This solution looks pretty simple but I am failing to implement a correct answer.

Nature of Code exercise 6.6 http://natureofcode.com/book/chapter-6-autonomous-agents/

image alt text

https://github.com/shiffman/The-Nature-of-Code-Examples/blob/master/chp6_agents/NOC_6_04_Flowfield/FlowField.pde (line 32)

The top left of the grid is -1,-1 and the bottom is 1,1 for PVector rotation.

PVector v = new PVector(____________,____________);
v.______________();
field[i][j] = v;

Thanks

Tagged:

Answers

  • edited July 2014 Answer ✓

    I am working towards the answer, so choose how much you want to know... :)]

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    The unique aspect for each position is an i and a j where the range is between 0 and cols or rows respectively.

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    You want to create a direction vector. So you probably want to remap those i and j values to the range of (-1, 1) for outward vectors or (1, -1) for inward vectors.

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    You can do this easily with Processing's convenience method map().

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    Once you have done this, you'll notice that the outer values are closer to 1 and the inner values (around the center) are closer to 0. So to have equal magnitude for all vectors you can normalize() them. That way all vectors are of magnitude 1.

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    According to this reasoning the solution to the excercise should be:

    PVector v = new PVector(map(i, 0, cols, 1, -1), map(j, 0, rows, 1, -1));
    v.normalize();
    
  • Thanks for the step by step breakdown before the final answer!

Sign In or Register to comment.