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

4 views (last 30 days)

Show older comments

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.

##### 0 Comments

### Accepted Answer

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
on 22 Sep 2021

### More Answers (2)

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

Image Analyst
on 22 Sep 2021

Not sure what's in your mat files. It's not what you showed in your original post

s1 = load('left_seg1.mat')

s2 = load('left_seg2.mat')

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;

Matt J
on 22 Sep 2021

##### 12 Comments

Image Analyst
on 30 Sep 2021

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!