Snapping a Locator to an Interpolation curve

*To*: mathgroup at smc.vnet.net*Subject*: [mg111861] Snapping a Locator to an Interpolation curve*From*: Fred Klingener <gigabitbucket at BrockEng.com>*Date*: Sun, 15 Aug 2010 07:40:18 -0400 (EDT)

I'm working to snap a Locator to a curve specified as an InterpolatingFunction of a form that returns a 2D point for a single real parameter. The approach I've taken snaps the Locator value to the point on the curve closest to the Locator position the user sets on the screen. The problems I'm having are so basic that they can be illustrated without involving the Locator machinery and on a simple case that could be worked simpler ways, but here goes: I construct an InterpolatingFunction of a circle with radius 1.0, centered on the origin. The parameter is the polar angle. Clear[t,x,distance, distance2] t=Interpolation[Table[{x,{Cos[x],Sin[x]}},{x,0, 2. Pi,Pi/12.}]] InterpolatingFunction[{{0.,6.28319}},<>] It seems to be an ordinary function that I can understand. Plot shows the x and y values: Plot[t[x],{x,0,2. Pi}] and ParametricPlot shows the curve: ParametricPlot[t[x],{x, 0, 2. Pi}] Say the user has set the Locator point to {0.5, 0.5}, and I want to snap the value to the closest point on the interpolated line. The distance from the point to the line as a function of x is distance[x_]:=Norm[t[x]-{0.5,0.5}] It seems to be a fairly normal easy to understand function: distance[Pi/4] 0.292893 and it has a pretty straightforward plot with a simple minimum around x = Pi/4: Plot[distance[x],{x,0,2 Pi}] It seems that something this simple would have a minimum that would be easy to find, but FindMinimum[distance[x],{x,0.7}] returns unevaluated with an inscrutable message: "During evaluation of In[21]:= FindMinimum::nrnum: The function value {0.374438,0.203872} is not a real number at {x} = {0.7}. >>" What "function"? distance[] returns a perfectly respectable real value at x = 0.7 distance[0.7] 0.301469 Does it have a problem with t[]? t[0.7] {0.764767,0.644159} Maybe, but its return is unrelated to the point that the message writer is worried about. For a time I worried about some inscrutable interaction with FindMinimum's attributes. Maybe, but Plot has the same attributes, and that seems to be able to handle it in a comprehensible way. Do I have some fundamental misunderstanding of the process? Very possibly, but the success of this next form leaves me without a clue of the direction to go to get smarter: distance2[x_]:=Norm[{Cos[x],Sin[x]}-{0.5,0.5}] FindMinimum[distance2[x],{x,0.7}] {0.292893,{x->0.785398}} So, what's up? TIA, Fred Klingener