title: Reflection of a Point over a Line
body: <p>I have been looking at how to reflect a point in a line, and found <a href="https://stackoverflow.com/questions/3306838/algorithm-for-reflecting-a-point-across-a-line">this question</a> which seems to do the trick, giving this formula to calculate the reflected point:</p>
  
  <blockquote>
    <p>Given (x,y) and a line y = ax + c we want the point (x', y') reflected on the line.</p>
    
    <p>Set d:= (x + (y - c)*a)/(1 + a^2)</p>
    
    <p>Then x' = 2*d - x</p>
    
    <p>and y' = 2*d*a - y + 2c</p>
  </blockquote>
  
  <p>However there are two problems with this implementation for my needs:</p>
  
  <ol>
  <li>My line is not described in the form <code>y = ax + c</code> (so I'd have to translate it, which is easy to do, but it means the process is slower).</li>
  <li>What if <code>a</code> is infinity ie. a vertical line?</li>
  </ol>
  
  <p>Is there a simple way to calculate <code>(x', y')</code>, the reflection of point <code>(x, y)</code> in a line, where the line is described by the two points <code>(x1, y1)</code> and <code>(x2, y2)</code>?</p>
  
  <h3>Edit:</h3>
  
  <p>I've found a formula which does this, but it seems as though it does not work with lines that look like they have equation y = x.</p>
  
  <p>Here it is in actionscript:</p>
  
  <pre><code>public static function reflect(p:Point, l:Line):Point
      {
                      // (l.sx, l.sy) = start of line
                      // (l.ex, l.ey) = end of line
          var dx:Number = l.ex - l.sx;
          var dy:Number = l.ey - l.sy;
  
          if ((dx == 0) &amp;&amp; (dy == 0))
          {
              return new Point(2 * l.sx - p.x, 2 * l.sy - p.y);
          }
          else
          {
              var t:Number = ((p.x - l.sx) * dx + (p.y - l.sy) * dy) / (dx * dx + dy * dy);
              var x:Number = 2 * (l.sx + t * dx) - p.x;
              var y:Number = 2 * (l.sy + t * dy) - p.y;
              return new Point(x, y);
          }
      }
  </code></pre>
  
  <p>Does anyone have any idea where this formula goes wrong? I am still happy to take other solutions than the above formula - anything that'll work!</p>