circlefit(xp, yp)
Writes the RMS error of the distance of the original points to the circle directly onto the console.
optim
with this solution as starting point.The value to be minimized is the distance of the given points to the nearest point on the circle.
# set.seed(8421)
n <- 20
w <- 2*pi*runif(n)
xp <- cos(w) + 1 + 0.25 * (runif(n) - 0.5)
yp <- sin(w) + 1 + 0.25 * (runif(n) - 0.5)
rslt <- circlefit(xp, yp) #=> 0.9965782 1.0009066 1.0240452
x0 <- rslt[1]; y0 <- rslt[2]; r0 <- rslt[3]
plot(c(-0.2, 2.2), c(-0.2, 2.2), type="n", asp=1)
grid()
abline(h=0, col="gray"); abline(v=0, col="gray")
points(xp, yp, col="darkred")
w <- seq(0, 2*pi, len=100)
xx <- r0 * cos(w) + x0
yy <- r0 * sin(w) + y0
lines(xx, yy, col="blue")
Run the code above in your browser using DataLab