How to create a circle within a matrix

27 views (last 30 days)
Need to figure out how to create a circle of a specificed size and position, within a premade matrix of zero's. Currently I have the code for the Matrix set and I am able to control the boundaries of the maxtrix. but I would like to be able to place a circle matrix of desired radius and location within the maxtrix of zero's Ive specified
cx1 = .01; %x position of circle
cy1 = .05; %y position of circle
cr1 = .02; %radius of circle
th = 0:pi/100:2*pi;
xunit = cr1 * cos(th) + cx1
yunit = cr1 * sin(th) + cy1
plot (xunit,yunit)
viscircles([centerX, centerY], radius);
x=-.10:.02:.10;
[X,Y]=meshgrid(x); % xy space
v1=zeros(size(X)); % previous v
v1(xunit,yunit)=10 % top boundary CONTROLS COLOR BAR
v1(end,:)=10 % bottom boundary
v1(:,1)=10 % left boundary
v1(:,end)=10 % right boundary

Accepted Answer

Walter Roberson
Walter Roberson on 7 Dec 2019
Edited: Walter Roberson on 7 Dec 2019
x=-.10:.002:.10; %note change from 0.02 to 0.002
[X,Y]=meshgrid(x); % xy space
v1=zeros(size(X)); % previous v
xidx = interp1(x, 1:length(x), xunit, 'nearest');
yidx = interp1(x, 1:length(x), yunit, 'nearest');
idx = sub2ind(size(X), xidx, yidx);
v1(idx) = 10;
v1(end,:)=10; % bottom boundary
v1(:,1)=10; % left boundary
v1(:,end)=10; % right boundary
You were painting a circle with radius 0.02 into a location with pixels spaced 0.02 apart. The result was essentially unrecognizable as a circle. You can switch the resolution back in the definition of x if you want but you will not see anything useful.
By the way, if you have the Computer Vision toolbox, use insertShape instead of all of this.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!