![C++ Game Development By Example](https://wfqqreader-1252317822.image.myqcloud.com/cover/625/36698625/b_36698625.jpg)
Vector magnitude
The magnitude of the vector is equal to the length of the vector itself,. But how do we calculate it mathematically? .
The magnitude of a vector is given by the Pythagorean theorem, which specifies that in a right triangle , the square of length of a diagonal is equal to the sum of the squares of the adjacent sides. So when we look at the right triangle as follows, c2 = x2 + y2.
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/33d29f10-660e-4b50-951a-d0dabbcc34d9.png?sign=1739591221-CnR4xEG7jqOMGcFHKxC0igny5ZDGdGTT-0-0de7ee196af758048af111f7a821546e)
This can be extended to three dimensions with c2 = x2 + y2 + z2.
Magnitudes of vectors are indicated by double vertical bars, so the magnitude of a
vector is denoted by
. The magnitude is always greater than or equal to zero.
So, if vector A = (X, Y, Z) then the magnitude is given by the following equation:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/2d887c18-924e-41c4-9f54-ec8091ec16cd.png?sign=1739591221-Uw8n1YQVr4ZiYvEkiX3OTzF0FM0MMdg3-0-34feb3b238fae6e53b167961bd6f0e2f)
If = (3, -5, 7), Then:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/f07ae15b-1c8c-4586-8c39-5f54ca27a01c.png?sign=1739591221-PbTy6bjzYREIwYPjh62PvQDGAB43u84u-0-c750d860baa8a06ba0427aeee2d325d7)
= 9.110
The vector is therefore 9.11 units long