Reflection, within the context of this article, is flipping something's direction or velocity over a surface it has collided with. This kind of calculation has a number of uses, including simulating a particle bouncing off a wall, and bouncing a light ray off a mirror in ray tracing. Vectors are used to represent the incident direction, reflected direction, and normal of the surface.
Process
The idea is the incident vector can be projected onto the normal vector, giving the component along the normal direction. This component can then be reversed, scaled by 2, and added to the incident vector to find the reflected vector.
When the normal is a unit vector (has a length of 1), the calculation simplifies to:
set [scale factor v] to ((((incident.x) * (normal.x)) + ((incident.y) * (normal.y))) * (-2)) set [reflected.x v] to ((incident.x) + ((normal.x) * (scale factor))) set [reflected.y v] to ((incident.y) + ((normal.y) * (scale factor)))
Otherwise, a normal with any length can be compensated for by dividing by its length:
set [normal length v] to ([sqrt v] of (((normal.x) * (normal.x)) + ((normal.y) * (normal.y)))) set [scale factor v] to ((((incident.x) * ((normal.x) / (normal length))) + ((incident.y) * ((normal.y) / (normal length)))) * (-2)) set [reflected.x v] to ((incident.x) + (((normal.x) / (normal length)) * (scale factor))) set [reflected.y v] to ((incident.y) + (((normal.y) / (normal length)) * (scale factor)))