• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Reflect 2D vector from a line

## Recommended Posts

Having a vector a, and a line (or other vector), there is a direct method to reflect the vector a from the line axis ? I know that can make this by movin line to origin, rotating line to align it to an coord axis Y and reflecting by Y, and later do the steps backwards.

##### Share on other sites
rewrite your vector a as a sum of two vectors a = ap + ao.

ap is parallel to the 2nd vector b (line) : ap = x*b. You´ll most probably (=best) have to use the dot product a*b to find x.

ao is orthogonal to b. Best obtained by ao = a - ap.

For the reflected vector a'': a'' = ap - ao.
In words: The parallel part of a remains unchanged (that´s what´ll happen if you "reflect" your line on itself, no?) while the orthogonal part changes it´s direction.

Sadly, you didn´t explicitely say how you´d "reflect" ("to mirror" might fit in better) your vector on the Y-Axis but above is most probably what you meant.

btw.: If you want to "reflect" a vector on a line that doesn´t pass through the origin, then I don´t even know what you want to do (mirroring a point, perhaps?).

Vector2D Reflection(Vector2D vec, Vector2D lp1, Vector2D lp2){	Vector2D normal;	normal.x = lp2.x-lp1.x;	normal.y = lp2.y-lp1.y;	float temp = normal.x;	normal.x = -normal.y;	normal.y = temp;	normal.Normalize();	float dot2 = 2 * vec.Dot(normal);	vec.x-=dot2*normal.x;	vec.y-=dot2*normal.y;	return vec;}