From Wikipedia: RANSAC is an abbreviation for "RANdom SAmple Consensus". It is an iterative method to estimate parameters of a mathematical model from a set of observed data which contains outliers.

### Example:

In the case of a 2D line fitting, Ransac is very simple:

bestModel bestScore = Infinity bestInliers = [] for a sufficient number of iterations :-) currentModel = estimate a line from 2 points randomly selected in dataset currentScore = 0 currentInliers = [] for all points in the dataset p = current point in the dataset currentError = distance(currentModel, p) if (currentError < threshold) currentScore += currentError currentInliers.push(p) else currentScore += threshold if (currentScore < bestScore) bestScore = currentScore bestModel = currentModel bestInliers = currentInliers return bestModel, bestInliers, bestScore

Given a set of 2d points you can use Ransac to estimate a fitting line. But the resulting line should only be estimated using inliers and not be contaminated by the outliers. Example of ransac iteration: bad model estimated | final ransac iteration if everything went well :-)

### Live demo:

Source code: ransac.js (core Ransac + RobustLineFitting) available under MIT license.