Feng Zhang
2003-Feb-25 18:49 UTC
[R] How to calculate the closest distance from a point to a curve?
Hely, R-list Now I have non-parametric curve function, that is, I only use N 2-Dimensional data points to represent this curve, without explicit function formulation. And given a new measurement (x1,x)', how can I calculate the shortese Euclidean distance from this new data point to the above curve? Thanks a lot. Fred
Jose Lozano (MEGA)
2003-Feb-25 20:10 UTC
[R] How to calculate the closest distance from a point to a curve?
>Hely, R-list > >Now I have non-parametric curve function, that is, >I only use N 2-Dimensional data points to represent >this curve, without explicit function formulation. >(x,y) gives the points that define the curve (I've generated a circle centered at 0) (a,b) is the point (I've set it to (2,2)) x<-seq(from=-1,to=+1,length=1000) y<-sqrt(1-x^2) x<-c(x,x) y<-c(y,-y) n<-length(x) a<-2 b<-2 The code to find the closest point to (a,b) is: minimo<-min((1:n)[sqrt((a-x)^2+(b-y)^2)==min(sqrt((a-x)^2+(b-y)^2))]) cat(x[minimo],y[minimo],"\n") 0.7077077 0.7065053 Regards Jose Lozano