Repository: opencv/opencv
File: modules/imgproc/src/shapedescr.cpp (C++)

Code before:
        else
        {
            findThirdPoint(pts, i, j, center, radius);
        }
    }
}


template<typename PT>
static void findMinEnclosingCircle(const PT *pts_in, int count, Point2f &center, float &radius)
{
    // Welzl's algorithm requires random permutation for expected O(n) time.
    // Without shuffling, sorted inputs trigger O(n^3) worst case.
    cv::AutoBuffer<PT, 1024> pts_buf(count);
    std::copy(pts_in, pts_in + count, pts_buf.data());
    PT* pts = pts_buf.data();
    if (count > 10)
    {
        cv::RNG rng(cv::theRNG().next());
        for (int i = count - 1; i > 0; --i)
        {
            int j = (unsigned int)rng % (i + 1);
            std::swap(pts[i], pts[j]);
        }
    }
    const PT *pts_ptr = pts;

    center.x = (float)(pts_ptr[0].x + pts_ptr[1].x) / 2.0f;
    center.y = (float)(pts_ptr[0].y + pts_ptr[1].y) / 2.0f;
    float dx = (float)(pts_ptr[0].x - pts_ptr[1].x);
    float dy = (float)(pts_ptr[0].y - pts_ptr[1].y);
    radius = (float)norm(Point2f(dx, dy)) / 2.0f + EPS;

    for (int i = 2; i < count; ++i)
    {
        dx = (float)pts_ptr[i].x - center.x;
        dy = (float)pts_ptr[i].y - center.y;
        float d = (float)norm(Point2f(dx, dy));
        if (d < radius)
        {
            continue;
        }
        else
        {
            findSecondPoint(pts_ptr, i, center, radius);
        }
    }
}
} // namespace cv

// see Welzl, Emo. Smallest enclosing disks (balls and ellipsoids). Springer Berlin Heidelberg, 1991.

Review comment (refactor):
please, remove this assignment and revert `pts_ptr` to `pts` in the code below — this way we reduce the patch size.

Code after:
        else
        {
            findThirdPoint(pts, i, j, center, radius);
        }
    }
}


template<typename PT>
static void findMinEnclosingCircle(const PT *pts_in, int count, Point2f &center, float &radius)
{
    // Welzl's algorithm requires random permutation for expected O(n) time.
    // Without shuffling, sorted inputs trigger O(n^3) worst case.
    cv::AutoBuffer<PT, 1024> pts_buf(count);
    std::copy(pts_in, pts_in + count, pts_buf.data());
    PT* pts = pts_buf.data();
    if (count > 10)
    {
        Cv32suf x0, y0, xn, yn;
        x0.f = (float)pts[0].x;
        y0.f = (float)pts[0].y;
        xn.f = (float)pts[count-1].x;
        yn.f = (float)pts[count-1].y;

        uint32_t seed = (uint32_t)count ^ x0.u ^ (y0.u << 8) ^ (xn.u << 16) ^ (yn.u << 24);
        cv::RNG rng(seed);

        for (int i = 1; i < count; ++i)
        {
            int j = rng.uniform(0, i + 1);
            std::swap(pts[i], pts[j]);
        }
    }

    center.x = (float)(pts[0].x + pts[1].x) / 2.0f;
    center.y = (float)(pts[0].y + pts[1].y) / 2.0f;
    float dx = (float)(pts[0].x - pts[1].x);
    float dy = (float)(pts[0].y - pts[1].y);
    radius = (float)norm(Point2f(dx, dy)) / 2.0f + EPS;

    for (int i = 2; i < count; ++i)
    {
        dx = (float)pts[i].x - center.x;
        dy = (float)pts[i].y - center.y;
        float d = (float)norm(Point2f(dx, dy));
        if (d < radius)
        {
            continue;
        }
        else
        {