title: distance from given point to given ellipse
body: <p>I have an ellipse, defined by Center Point, radiusX and radiusY, and I have a Point. I want to find the point on the ellipse that is closest to the given point. In the illustration below, that would be S1.  </p>
  
  <p><img src="https://i.imgur.com/pEb1jBs.png" alt="graph1"></p>
  
  <p>Now I already have code, but there is a logical error somewhere in it, and I seem to be unable to find it. I broke the problem down to the following code example:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;opencv2/core/core.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;math.h&gt;
  
  using namespace std;
  
  void dostuff();
  
  int main()
  {
      dostuff();
      return 0;
  }
  
  typedef std::vector&lt;cv::Point&gt; vectorOfCvPoints;
  
  void dostuff()
  {
  
      const double ellipseCenterX = 250;
      const double ellipseCenterY = 250;
      const double ellipseRadiusX = 150;
      const double ellipseRadiusY = 100;
  
      vectorOfCvPoints datapoints;
  
      for (int i = 0; i &lt; 360; i+=5)
      {
          double angle = i / 180.0 * CV_PI;
          double x = ellipseRadiusX * cos(angle);
          double y = ellipseRadiusY * sin(angle);
          x *= 1.4;
          y *= 1.4;
          x += ellipseCenterX;
          y += ellipseCenterY;
          datapoints.push_back(cv::Point(x,y));
      }
  
      cv::Mat drawing = cv::Mat::zeros( 500, 500, CV_8UC1 );
  
      for (int i = 0; i &lt; datapoints.size(); i++)
      {
          const cv::Point &amp; curPoint = datapoints[i];
          const double curPointX = curPoint.x;
          const double curPointY = curPoint.y * -1; //transform from image coordinates to geometric coordinates
  
          double angleToEllipseCenter = atan2(curPointY - ellipseCenterY * -1, curPointX - ellipseCenterX); //ellipseCenterY * -1 for transformation to geometric coords (from image coords)
  
          double nearestEllipseX = ellipseCenterX + ellipseRadiusX * cos(angleToEllipseCenter);
          double nearestEllipseY = ellipseCenterY * -1 + ellipseRadiusY * sin(angleToEllipseCenter); //ellipseCenterY * -1 for transformation to geometric coords (from image coords)
  
  
          cv::Point center(ellipseCenterX, ellipseCenterY);
          cv::Size axes(ellipseRadiusX, ellipseRadiusY);
          cv::ellipse(drawing, center, axes, 0, 0, 360, cv::Scalar(255));
          cv::line(drawing, curPoint, cv::Point(nearestEllipseX,nearestEllipseY*-1), cv::Scalar(180));
  
      }
      cv::namedWindow( "ellipse", CV_WINDOW_AUTOSIZE );
      cv::imshow( "ellipse", drawing );
      cv::waitKey(0);
  }
  </code></pre>
  
  <p>It produces the following image:</p>
  
  <p><img src="https://i.imgur.com/blF3fYZ.png" alt="snapshot1"></p>
  
  <p>You can see that it actually finds "near" points on the ellipse, but it are not the "nearest" points. What I intentionally want is this: (excuse my poor drawing)  </p>
  
  <p><img src="https://i.imgur.com/MOm0QVg.png" alt="snapshot2"></p>
  
  <p>would you extent the lines in the last image, they would cross the center of the ellipse, but this is not the case for the lines in the previous image.<br>
  I hope you get the picture. Can anyone tell me what I am doing wrong?</p>