How to get a reflection vector?

$$r = d - 2 (d \cdot n) n$$

where $d \cdot n$ is the dot product, and $n$ must be normalized.


Let $\hat{n} = {n \over \|n\|}$. Then $\hat{n}$ is the vector of magnitude one in the same direction as $n$. The projection of $d$ in the $n$ direction is given by $\mathrm{proj}_{n}d = (d \cdot \hat{n})\hat{n}$, and the projection of $d$ in the orthogonal direction is therefore given by $d - (d \cdot \hat{n})\hat{n}$. Thus we have $$d = (d \cdot \hat{n})\hat{n} + [d - (d \cdot \hat{n})\hat{n}]$$ Note that $r$ has $-1$ times the projection onto $n$ that $d$ has onto $n$, while the orthogonal projection of $r$ onto $n$ is equal to the orthogonal projection of $d$ onto $n$, therefore $$r = -(d \cdot \hat{n})\hat{n} + [d - (d \cdot \hat{n})\hat{n}]$$ Alternatively you may look at it as that $-r$ has the same projection onto $n$ that $d$ has onto $n$, with its orthogonal projection given by $-1$ times that of $d$. $$-r = (d \cdot \hat{n})\hat{n} - [d - (d \cdot \hat{n})\hat{n}]$$ The later equation is exactly $$r = -(d \cdot \hat{n})\hat{n} + [d - (d \cdot \hat{n})\hat{n}]$$

Hence one can get $r$ from $d$ via $$r = d - 2(d \cdot \hat{n})\hat{n}$$ Stated in terms of $n$ itself, this becomes $$r = d - {2 d \cdot n\over \|n\|^2}n$$