GithubHelp home page GithubHelp logo

smallest-circle's Introduction

Overview

This project is an implementation of Smallest Enclosing Disc algorithm described in the book Computational Geometry (section 4.7). The following pictures show some test cases for 5 points, 500 points, and 5000 points.

Build

Cmake version 2.8.3.

mkdir build
cd build
cmake ..
make

Run

cd build
./SmallestCircle

You could also play with the range and number of test points. The following command tests 10,000 points within 100:

./SmallestCircle 100 10000

To visualize the test data,

cd src
python smallest_circle_visualization.py

Implementation

The algorithm takes a vector of points as input and return a circle. The point and circle class are defined as follows. All the member functions are ignored for illustration purpose.

struct Point {
  double x, y;
};

struct Circle {
  Point center;
  double radius = 0;
};

This structure is chosen so that the algorithm could do an efficient point-circle enclosure checking, i.e.:

struct Point {
  // Return the squared length of the vector
  const double LengthSquare() const { return Dot(*this); }
};

struct Circle {
 // Check if the circle encloses a point
  bool Encloses(const Point& p){
    return radius > 0 && center.DistanceSquare(p) <= radius * radius + Epsilon;
}
};

In the algorithm, we need to calculate a circle from two given points or three given points, which are handled differently. It's very clear that a minimal circle that encloses two points must have these two points on its diameter line. To calculate the minial circle from three points, the algorithm from Circumscribed circle is used. It's worth mention that these three points must not be colinear. In the algorithm, three colinear points will be first solved by constructing a circle from two points so this won't be a problem.

Testing

The code was tested against the following test cases:

  1. One point input, it returns a circle with the point as center and zero radius.

  2. Two points input, it returns the middle of 2 points as center and half of the length as radius

  3. Three colinear points, return a circle similar to two-points case that enclosed the third point

  4. More than three points. This test is done in the following way.

    1. Randomly generate three points and a minimal circle using brute force, this serves as ground truth
    2. Randomly generate points that are inside of the ground truth circle
    3. Compute the smallest circle using the alorithm and compare with the ground truth

smallest-circle's People

Contributors

malichao avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.