title: How to fit a circle to a set of points with a constrained radius?
body: <p>I have a set of points that represent a small arc of a circle. </p>
  
  <p>The current code fits a circle to these points using linear least-squares:</p>
  
  <pre><code>void fit_circle(const std::vector&lt;cv::Point2d&gt; &amp;pnts,cv::Point2d &amp;centre, double &amp;radius)
  {
      int cols = 3;
      cv::Mat X( static_cast&lt;int&gt;(pnts.size()), cols, CV_64F );
      cv::Mat Y( static_cast&lt;int&gt;(pnts.size()), 1, CV_64F );
      cv::Mat C;
  
      if (int(pnts.size()) &gt;= 3 )
      {
          for (size_t i = 0; i &lt; pnts.size(); i++)
          {
              X.at&lt;double&gt;(static_cast&lt;int&gt;(i),0) = 2 * pnts[i].x;
              X.at&lt;double&gt;(static_cast&lt;int&gt;(i),1) = 2 * pnts[i].y;
              X.at&lt;double&gt;(static_cast&lt;int&gt;(i),2) = -1.0;
              Y.at&lt;double&gt;(static_cast&lt;int&gt;(i),0) = (pnts[i].x * pnts[i].x + pnts[i].y * pnts[i].y);
          }
          cv::solve(X,Y,C,cv::DECOMP_SVD);
      }
      std::vector&lt;double&gt; coefs;
      C.col(0).copyTo(coefs);
      centre.x = coefs[0];
      centre.y = coefs[1];
      radius = sqrt ( coefs[0] * coefs[0] + coefs[1] * coefs[1] - coefs[2] );
  }
  </code></pre>
  
  <p>However, <strong>I have a constraint that the radius must be within a certain range</strong>,<br>
  i.e. <code>minRadius &lt;= radius &lt;= maxRadius</code>. </p>
  
  <p>So the best fitting circle with a radius within that range should be selected, instead of the overall best fit.</p>
  
  <p><strong>How can I modify the code to add this constraint?</strong></p>