Reflecting a Point About a Line

This is a pretty elementary operation in analytic geometry, but it’s come up in a few emails.  Although the solution is available online, programmers often have problems with equations expressed in vector notation.  That seems to be the common thread in the emails. I don’t have time for a comprehensive demo, so here is a quick Actionscript code snippet.

The problem at hand is how to reflect the point P2 about the line segment through points P0 and P1.  Let R be the reflection of P2 about the line segment.  I suspect the issue some people have problems with is the requirement to normalize the vector from P0 to P1.

If rx and ry are the x- and y-components of R, then

  var nx:Number = p1.x - p0.x;
  var ny:Number = p1.y - p0.y;
  var d:Number  = Math.sqrt(nx*nx + ny*ny);
  nx /= d;
  ny /= d;

  var px:Number = p2.x - p0.x;
  var py:Number = p2.y - p0.y;
  var w:Number  = nx*px + ny*py;
  var rx:Number = 2*p0.x - p2.x + 2*w*nx;
  var ry:Number = 2*p0.y - p2.y + 2*w*ny;

I’ll be using this method in an upcoming development, so we will have a chance to see it in action. In the mean time, I hope this helps the people who have been asking.


2 thoughts on “Reflecting a Point About a Line

Comments are closed.