We are about to switch to a new forum software. Until then we have removed the registration on this forum.

- All Categories 25.7K
- Announcements & Guidelines 13
- Common Questions 30
- Using Processing 22.1K
- Programming Questions 12.2K
- Questions about Code 6.4K
- How To... 4.2K
- Hello Processing 72
- GLSL / Shaders 292
- Library Questions 4K
- Hardware, Integration & Other Languages 2.7K
- Kinect 668
- Arduino 1K
- Raspberry PI 188
- Questions about Modes 2K
- Android Mode 1.3K
- JavaScript Mode 413
- Python Mode 205
- Questions about Tools 100
- Espanol 5
- Developing Processing 548
- Create & Announce Libraries 211
- Create & Announce Modes 19
- Create & Announce Tools 29
- Summer of Code 2018 93
- Rails Girls Summer of Code 2017 3
- Summer of Code 2017 49
- Summer of Code 2016 4
- Summer of Code 2015 40
- Summer of Code 2014 22
- p5.js 1.6K
- p5.js Programming Questions 947
- p5.js Library Questions 315
- p5.js Development Questions 31
- General 1.4K
- Events & Opportunities 289
- General Discussion 365

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);
}
}
```

Tagged:

## 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.

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

Take notice that

setMag()method is exactlynormalize()+mult(): ;;)https://Processing.org/reference/PVector_setMag_.html