title: Getting point on line segment that is closest to another point
body: <p>I want to find a point on segment line AB, that is closest to another point P.</p>
  
  <p>My idea was:</p>
  
  <ol>
  <li>Get <code>a1</code> and <code>b1</code> from line formula <code>y1 = a1x + b1</code>, using A and B points coordinates.</li>
  <li>Get normal line formula, from <code>a1</code> and P coordinates, <code>y2 = a2x + b2</code>.</li>
  <li>Get intersection point x coord, by equating <code>y1</code> and <code>y2</code> and next using one of above formulas, to get y.</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/1dZLy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1dZLy.png" alt="enter image description here"></a></p>
  
  <p>My code:</p>
  
  <pre><code>#include &lt;SFML\Graphics.hpp&gt;
  #include &lt;iostream&gt;
  
  sf::Vector2f getClosestPointOnLine(sf::Vector2f A, sf::Vector2f B, sf::Vector2f P)
  {
      //convert to line formula
      float a = (B.y - A.y)/(B.x - A.x);
      float b = -a * A.x + A.y;
  
      //get normal line formula
      float a2 = -a / 2;
      float b2 = -a2 * P.x + P.y;
  
      //get x
      float a3 = a - a2;
      float b3 = b2 - b;
  
      float x = b3 / a3;
  
      //get y
      float y = a * x + b;
  
      return { x, y };
  }
  
  int main()
  {
      sf::RenderWindow gameWindow(sf::VideoMode(800, 600), "App");
  
      sf::View view(sf::FloatRect(0, 0, 800, 600));
      gameWindow.setView(view);
  
      gameWindow.setFramerateLimit(60);
  
      sf::VertexArray plane(sf::LinesStrip, 2);
  
      plane[0] = { { view.getSize().x * 0.5f, view.getSize().y * 0.8f } };
      plane[1] = { { view.getSize().x * 0.8f, view.getSize().y * 0.6f } };
  
      sf::CircleShape ball(10);
  
      ball.setOrigin(10, 10);
      ball.setPosition({view.getSize().x * 0.7f, view.getSize().y * 0.4f});
  
      while (gameWindow.isOpen())
      {
          sf::Event e;
          while (gameWindow.pollEvent(e))
          {
              if (e.type == sf::Event::Closed)
              {
                  gameWindow.close();
              }
          }
  
          //draw
          gameWindow.clear(sf::Color{30, 30, 30});
  
          ball.setPosition((sf::Vector2f)sf::Mouse::getPosition(gameWindow));
  
          sf::Vector2f closest = getClosestPointOnLine(plane[0].position, plane[1].position, ball.getPosition());
  
          sf::CircleShape cs(5);
          cs.setOrigin(5, 5 );
          cs.setPosition(closest);
  
          gameWindow.draw(cs);
          gameWindow.draw(plane);
          gameWindow.draw(ball);
          gameWindow.display();
      }
  }
  </code></pre>
  
  <p>Result:
  <a href="https://i.stack.imgur.com/bj4j2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bj4j2.png" alt="enter image description here"></a></p>
  
  <p>As you can see function <code>getClosestPointOnLine</code> returns me wrong intersection point.
  What is wrong with my function?</p>
  
  <p>------------------EDIT: 
  As n.m. mentioned, <code>-a / 2</code> is not formula for normal line slope, I was wrong with this formula, the right is: <code>-1 / a</code>.</p>