nature of code exercise 1.4

edited June 2016 in How To...

I was following this book example and they gave this little exercise and I cant figure out what I’m supposed to do and the book never gives the solution.

how do I calculate the limit of a vector? here is the exercise.

void limit(float max) {
  if (_______ > _______) {
    _________();
    ____(max);
  }
}

i am trying to accomplish something like this

velocity.add(acceleration);
velocity.limit(topspeed);

to get some background here is the link to said exercise. http://natureofcode.com/book/chapter-1-vectors/

ANSWER EDIT:

void limitt(float max){
    if(max > 10){
      velocity.normalize();
      velocity.mult(max);
    }
  }

Answers

  • It asks you to write your own limit function for learning purposes, as this method is already included in PVector class. Look, you need to check if magnitude is greater then max and if so, it should be reset back to max, so it would never be higher than this value.

  • i imagine he wants normalise and multiple functions in the gaps - the first will restrict the vector to length 1, the second will multiple it to the required max length.

  • Answer ✓

    I got it, just thinking to complicated. I edit the post with my answer. Thanks for your replies.

  • Take notice that setMag() method is exactly normalize() + mult(): ;;)
    https://Processing.org/reference/PVector_setMag_.html

Sign In or Register to comment.