# Issue while fitting semi-circle on a set of points

4 views (last 30 days)
sparsh garg on 22 Sep 2021
Commented: sparsh garg on 30 Sep 2021
In this figure I would like to fit a semi-circle on the green curve
The ref code for plotting semi-circle is as follows
function Circ = semi_circle(xc,yc,R,flag)
theta=linspace(pi/2,-pi/2,100);
xcircle = R*cos(theta')+xc;
ycircle = R*sin(theta')+yc;
if flag
plot(xcircle,ycircle);
end
Circ=[xcircle,ycircle];
end
xc,yc and R are obtained as follows
First compute normals for left seg 1 using LineNormals2D,then project the points on left seg1 using the angles in LineNormals2D I will then look at where these projections intersect left_seg2 using InterX. The intersectin point will be the center and the distance between the center and the poin t on left seg 1 will be radius
The green points are enclosed left_seg1 and left_seg2.
Any help/suggestions will be appreciated.

Matt J on 22 Sep 2021
I recommend circularFit from
In addition to doing more careful data pre-normalization than the Bucher Izhak code routine, it contains methods for post-plotting the fit.
>> fobj=circularFit([left_seg,left_seg2])
fobj =
circularFit with properties:
center: [133.7487 51.8286]
>> plot(fobj)
sparsh garg on 22 Sep 2021
ok i will try out your method also,unfortunately i don't have to access to matlab on my personal system.I tried pratt circle fitting but the result wasn't correct because I want the circle to lie inside the boundary described by those points,as shown in the main figure.
Image Analyst on 22 Sep 2021
To get it inside the points, you have to identify the 3 points that are closest to the middle. So use the original code to find the center. Then get the distances of all points to that center, then sort, and take the 3 shortest distances and fit a cirvle through only those 3 points.

Image Analyst on 22 Sep 2021
I'd suggest you use the FAQ to fit a circle through the points you have circled:
function [xCenter, yCenter, radius, a] = circlefit(x, y)
% circlefit(): Fits a circle through a set of points in the x - y plane.
% USAGE :
% [xCenter, yCenter, radius, a] = circlefit(X, Y)
% The output is the center point (xCenter, yCenter) and the radius of the fitted circle.
% "a" is an optional output vector describing the coefficients in the circle's equation:
% x ^ 2 + y ^ 2 + a(1) * x + a(2) * y + a(3) = 0
% by Bucher Izhak 25 - Oct - 1991
numPoints = numel(x);
xx = x .* x;
yy = y .* y;
xy = x .* y;
A = [sum(x), sum(y), numPoints;
sum(xy), sum(yy), sum(y);
sum(xx), sum(xy), sum(x)];
B = [-sum(xx + yy) ;
-sum(xx .* y + yy .* y);
-sum(xx .* x + xy .* y)];
a = A \ B;
xCenter = -.5 * a(1);
yCenter = -.5 * a(2);
radius = sqrt((a(1) ^ 2 + a(2) ^ 2) / 4 - a(3));
##### 3 CommentsShow 1 older commentHide 1 older comment
Image Analyst on 22 Sep 2021
Not sure what's in your mat files. It's not what you showed in your original post
x1 = s1.left_seg(1, :)
y1 = s1.left_seg(2, :)
x2 = s2.left_seg2(1, :)
y2 = s2.left_seg2(2, :)
plot(x1, y1, 'b-', 'LineWidth', 2);
hold on;
plot(x2, y2, 'r-', 'LineWidth', 2);
grid on;
sparsh garg on 22 Sep 2021
oh right left seg1 and left seg 2 combine to make the green curve in the figure,I will upload the entire figure points later.

Matt J on 22 Sep 2021
Image Analyst on 30 Sep 2021
In your magnified example, the black line from the top/magenta point, through the center of the circle to the bottom of the circle IS "perpendicular to the tangent that passes through the bottom of the circle". By definition, any line from the center of a circle to the perimeter of the circle is necessarily perpendicular to a tangent of the circle at the point where the line crosses the perimeter. You are not showing the line tangent to the circle but if you did, you would see that it's perpendicular to the black line through the center.
sparsh garg on 30 Sep 2021
ah thanks ,is it okay if I share some more examples to get your second opinion.