#circle #smallest #iterative #problem #points #computer-science #recursion

smallest-enclosing-circle

Iterative and recursive two-dimensional implementations of Welzl's algorithm for computing the smallest enclosing circle

2 unstable releases

0.2.0 Feb 25, 2024
0.1.0 Mar 25, 2023

#1473 in Algorithms

Download history 664/week @ 2024-07-23 1507/week @ 2024-07-30 1319/week @ 2024-08-06 586/week @ 2024-08-13 720/week @ 2024-08-20 879/week @ 2024-08-27 591/week @ 2024-09-03 1113/week @ 2024-09-10 998/week @ 2024-09-17 715/week @ 2024-09-24 680/week @ 2024-10-01 898/week @ 2024-10-08 940/week @ 2024-10-15 977/week @ 2024-10-22 729/week @ 2024-10-29 1241/week @ 2024-11-05

4,170 downloads per month

MIT license

30KB
595 lines

Smallest Enclosing Circle

Iterative and recursive two-dimensional implementations of Welzl's algorithm for computing the smallest enclosing circle.

The implementations in this crate solve the smallest enclosing circle problem, also known as smallest-circle problem, minimum covering circle problem, or bounding circle problem. Welzl's algorithm solves this problem in expected O(n) runtime. The original algorithm was formulated as recursive program, which leads to call stack overflow for larger problem sizes. Thus, the iterative implementation in this crate should be preferred. However, the recursive version is provided for demonstration purposes.

Please note that the expected runtime only holds for randomized inputs (i.e., you may want to shuffle your input stream in advance).

The implementation is based on the following work(s):

Welzl, E. (1991). Smallest enclosing disks (balls and ellipsoids). In New results and new trends in computer science (pp. 359-370). Springer, Berlin, Heidelberg.

Examples

use smallest_enclosing_circle::smallest_enclosing_circle;

// Input: Four corner points of square box of unit size
let points = Vec::from([[0., 0.], [1., 0.], [1., 1.], [0., 1.]]);
let circle = smallest_enclosing_circle(points.into_iter());
println!("Circle: {:?}", circle);
// Circle: Three([0.0, 1.0], [1.0, 1.0], [1.0, 0.0], false);
println!("Center: {:?}", circle.center());
// Center: Some([0.5, 0.5])
println!("Radius: {:?}", circle.radius());
// Radius: 0.7071067811865476

License: MIT

Dependencies

~1MB
~20K SLoC