MATLAB Answers

Issue while fitting semi-circle on a set of points

27 views (last 30 days)
sparsh garg
sparsh garg on 22 Sep 2021
Commented: sparsh garg on 30 Sep 2021 at 14:28
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.

Accepted Answer

Matt J
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]
radius: 4.3551
>> plot(fobj)
  2 Comments
Image Analyst
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.

Sign in to comment.

More Answers (2)

Image Analyst
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 Comments
sparsh garg
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.

Sign in to comment.


Matt J
Matt J on 22 Sep 2021
  12 Comments
sparsh garg
sparsh garg on 30 Sep 2021 at 14:28
ah thanks ,is it okay if I share some more examples to get your second opinion.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by