# Best fit 2D points to closed curve

In the pic below I want to keep the extruded object in the same position and I want to automatically align / best fit the Blue points with the edges and at the same time take the red points with it as shown in the second pic. The Blue points may be slightly larger or smaller than the design item but I would like to best fit between the four edges.

I can find info on fitting lines to points and regressions etc but nothing about best fitting points to closed curves, if there is a function you know of that may allow me to do this then please tell me as I am stuck at present.

Hi @RogerD,

iâ€™ll asume that the curve is planar and in the same plane of all points. If you get points from the discontinuities of the curve and fit a line trough these points to get Line A, you could then fit another line through the blue points to get Line B. You then probably need to check line directions and flip one of them before you could orient (2pt) the geometries from line to line.

_
c.

Hi Clement,

Both plane and points are planar, so far I set the centre of the points over the area centroid of the surface and rotate the points checking the closest point distances until the deviation is minimal but this is not the best fit which is what I am after. Likewise if I orientate on 2 pts then again it isnâ€™t the best fit, close but not close enough.

Just thought someone might know of a Numpy or Scipy function that would suit the job.

Hi @RogerD,

if the overal shape of the blue points matches the curve roughly, why not extract the discontinuities from the curve and fit a circle to them? Then fit a circle to the blue points. Orient center to center and then rotate until you get the lowest deviation (distances per angle, measured between curve and blue points).

_
c.

just a starting point:

and just a guess: but i would start with boundingbox - center-point - alignment, and start above algorithm with two goes, or even four - start with 0, 90, 180,270 rotationâ€¦ the algorithm might stuck to some local best fit - without rotating 180 degreeâ€¦

tell us how you finally solved your problemâ€¦ sound s like a nice challenge.

You might want to edit the @, i was confused for a second here

Roger! over.

1 Like

Hi Clement,

I think the @ went in automatically, thought it was supposed to but will look out for it in the future, thanks for the heads up.

Roger

Maybe one of the evolutionary solvers in grasshopper might help.

Thanks Ncik I will take a good look at this approach.