# Best way to fit a near-regular shape with perfect regular shape?

I have an image of a curve that roughly consist of 4 straight line segments + 4 ellipse arcs (in theory it is a set of points with equal distance to a square, then compressed to a 2:1 aspect ratio).

But it is really a "rough" approximation, as you can see all kinds of alias when zoomed in:

I wonder if there is a way to perfectly fit it with regular shaped curves? Do I have to draw each segment with a Bezier curve manually? What if there are 40 segments?

I have tried the "trace bitmap" feature in Inkscape following this post: How to smoothen (curve fitting) this image text in Gimp? And I have tried all the variants including centerline tracing. The results are not ideal though:

• Not sure I understand why you are doing this, but you can draw an ellipse in Inkscape, convert to a path, select all the nodes, and add nodes using the Insert New Nodes button, to get as many segments as you want. see example Jan 26 at 21:46
• @BillyKerr I'm doing this for a game mod. The original image is generated from the game itself and I don't have control over that. My intention is simply to make the curve accurately representing 4 line segments + 4 ellipse arcs. Thanks for the suggestion! I will try later. Jan 26 at 22:18
• In Gimp that would be a rounded-corners square, scaled down along one dimension. Jan 31 at 22:37
• @xenoid That's an easy way indeed! Thanks Feb 2 at 19:38

Not actually a generally valid answer for arbitarily complex curves, but a method to make a good vector replica of your current curve

If it's the set of the points which are all as far away from a base square and then squeezed vertically to 50% you can find the base square and make a new offset path.

At first reverse the squeezing by scaling it vertically to 200%:

The cyan lines are drawn with the pen between the opposite straight line sections. Looks, like the black curve is a rounded square which is rotated 45 degrees from the default position in Inkscape. The green shape in the next image is a separately drawn 45 degrees rotated square and that's your base square:

We can test it by removing its fill color, making a duplicate and inserting transformation effect Path > Dynamic Offset:

The dynamic offset handle is dragged outwards with the node tool for best fit. As you see it's not 100% perfect because the cyan lines are drawn inaccurately and the original already was somehow rough because it was your tracing attempt, I guess.

But If you can make better base square you will get better rounded square, too. Freeze the offset effect by applying Path > Object to path. Then you can make with the node tool easily some minor edits for perfect fit. In the next image I tried to make it fit better to your black curve, but you should use the original bitmap image:

The cyan lines are deleted. The node tool was used to move the nodes a little and to push the green curve towards the black. Squeeze the result vertically to 50% and that's your vector replica:

• Oh wow! Thank you! This is such a well written answer. Yes I finally found the mathematical description of what I look for: parallel curve / offset curve. And Inkscape's dynamic offset seems to be exactly this. I think the only inaccurate part here is you have to manually draw the center square? I have got a satisfying result yesterday by drawing an ellipse and then split path at the 4 arc midpoints, and adding line segments, then dragging the Bezier control point. I will try your approach next time :) Jan 28 at 1:33
• @YurenHuang I see the weakest point was guessing separately 8 times where's the joint of the arc and the straight part. I tried to fix it partially by drawing somehow average square instead of using the crossings. A math aware programmer would write some code which finds the best fitting elliptical and line sections by taking into the account how your curve is constructed and what dependencies it causes between the parts. Jan 28 at 10:43