MATLAB Answers

How do I plot a circle with a given radius and center?

5,961 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2010 年 7 月 26 日
回答済み: Steven Lord 2020 年 12 月 25 日 5:29
I would like to plot a circle with a given radius and center.

採用された回答

MathWorks Support Team
MathWorks Support Team 2019 年 1 月 3 日
Here is a MATLAB function that plots a circle with radius 'r' and locates the center at the coordinates 'x' and 'y':
function h = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
An alternative method is to use the 'rectangle' function:
function h = circle2(x,y,r)
d = r*2;
px = x-r;
py = y-r;
h = rectangle('Position',[px py d d],'Curvature',[1,1]);
daspect([1,1,1])
If you are using version R2012a or later and have Image Processing Toolbox, then you can use the 'viscircles' function to draw circles:
viscircles(centers,radii)

  13 件のコメント

表示 10 件の古いコメント
Walter Roberson
Walter Roberson 2020 年 12 月 23 日 0:48
r * cos(th) is part of polar to cartesian coordinate conversion. For a given theta, for a circle of radius r centered on the origin, then the x coordinate is r multiplied by cos(theta) and the y coordinate is r multiplied by sin(theta). And then you add the actual center of the circle, x and y, to those coordinates.
A more common mathematical notation would be and for a circle of radius r centered on and
Tey Yi Hua
Tey Yi Hua 2020 年 12 月 25 日 2:05
Okayyyy thank you very much...this help a lot!!!
Walter Roberson
Walter Roberson 2020 年 12 月 25 日 4:47
r*cos(theta) is used because of the definition of cos. cos(theta) = y/r where r = sqrt(x^2 + y^2) so y = r*cos(theta)

サインインしてコメントする。

その他の回答 (5 件)

serwan Bamerni
serwan Bamerni 2016 年 2 月 17 日

  3 件のコメント

Walter Roberson
Walter Roberson 2016 年 10 月 17 日
This is part of the Image Processing Toolbox
ceethal piyus
ceethal piyus 2019 年 5 月 16 日
I've applied circular hough tranform for identifing the circlular objects in an image and i got the results . But when i used the code into an app designer, insted of ploting the circles into the image where the centers are marked its ploting the circle in a different figure. How can i get both circles and center points into the same axes ( named "app.segmented" )
code.JPG
error
code1.JPG
How to add the axes name to viscircles function in an app designer. I tired
viscircles(centres,radii,'color','b','parent','app.segemented'); which is giving the following error.
code2.JPG
Walter Roberson
Walter Roberson 2020 年 12 月 25 日 4:49
viscircles(app.segmented, centres, radii, 'color', 'b')

サインインしてコメントする。


Supoj Choachaicharoenkul
Supoj Choachaicharoenkul 2019 年 10 月 2 日
plot(x, y, 'bo', 'MarkerSize', 50);

  1 件のコメント

wagenaartje
wagenaartje 2020 年 12 月 7 日
This is the best solution by far if you want to highlight some part in the figure

サインインしてコメントする。


amine bouabid
amine bouabid 2018 年 7 月 23 日
編集済み: amine bouabid 2018 年 7 月 23 日
hello
you can plot a circle simply by writing :
syms x; syms y;
ezplot((x-xi).^2+(y-yi).^2-r.^2)
where xi and yi are the coordinates of the center and r is the radius

  0 件のコメント

サインインしてコメントする。


Devin Marcheselli
Devin Marcheselli 2020 年 1 月 17 日
how do i plot a circle using the equation: (x-h).^2+(y-k).^2 = r.^2

  3 件のコメント

Mark Rzewnicki
Mark Rzewnicki 2020 年 1 月 20 日
Hey there, I've been playing around with this as it relates to complex-valued functions.
It turns out that this is a very nontrivial question. There are several issues you run into along the way - you need to make some arbitrary choices about how to define your circle. I will share with you the way I found to be most natural.
Assuming y is the indpendent variable, expand the x term in the circle equation:
Bring these expanded term to the right-hand side of the equation:
Take the square root of both sides and bring k to the right-hand side:
This is the equation you can use to plot the circle. The code I came up with is a little messy (was intended for personal use in another context) but I will throw some comments in and hopefully you find it as interesting as I did.
***The big takeaway from all of this: We can deal with circles in rectangular coordinates, but it is much more natural to use polar coordinates and think of the circle as the locus of points with distance r from the center.
% Circle equation: (x-h)^2 + (y-k)^2 = r^2
% Center: (h,k) Radius: r
h = 1;
k = 1;
r = 1;
%% In x-coordinates, the circle "starts" at h-r & "ends" at h+r
%% x_res = resolution spacing between points
xmin = h - r;
xmax = h + r;
x_res = 1e-3;
X = xmin:x_res:xmax;
%% There are 2 y-coordinates on the circle for most x-coordinates.
%% We need to duplicate every x-coordinate so we can match each x with
%% its pair of y-values.
%% Method chosen: repeat the x-coordinates as the circle "wraps around"
%% e.g.: x = [0 0.1 0.2 ... end end ... 0.2 0.1 0]
N = length(X);
x = [X flip(X)];
%% ytemp1: vector of y-values as we sweep along the circle left-to-right
%% ytemp2: vector of y-values as we sweep along the circle right-to-left
%% Whether we take positive or negative values first is arbitrary
ytemp1 = zeros(1,N);
ytemp2 = zeros(1,N);
for i = 1:1:N
square = sqrt(r^2 - X(i)^2 + 2*X(i)*h - h^2);
ytemp1(i) = k - square;
ytemp2(N+1-i) = k + square;
end
y = [ytemp1 ytemp2];
%% plot the (x,y) points
%% axis scaling coefficient c: how far past the radius should the graph go?
c = 1.5;
figure(1)
plot(x,y)
axis([h-c*r h+c*r k-c*r k+c*r]);
Ashly Tom
Ashly Tom 2020 年 3 月 5 日
Thank you so much.
Can you please tell how to plot 2 circles in same figure, using your code ?
Mark Rzewnicki
Mark Rzewnicki 2020 年 3 月 17 日
Sadly I just saw this now, sorry.
The easiest way to do this would have been to write the original code twice (renaming the variables the second time) and plot both circles using a "hold on" statement.
This makes the code look brutally ugly - you really should vectorize things and define functions when scaling up code like this - but it will get the job done in a pinch. The result would look something like this (5-minute edit of my original code):
% Circle equation: (x-h)^2 + (y-k)^2 = r^2
% Center: (h,k) Radius: r
h = 1;
k = 1;
r = 1;
h1 = 2;
k1 = 2;
r1 = 2;
%% In x-coordinates, the circle "starts" at h-r & "ends" at h+r
%% x_res = resolution spacing between points
xmin = h - r;
xmax = h + r;
x_res = 1e-3;
X = xmin:x_res:xmax;
xmin1 = h1 - r1;
xmax1 = h1 + r1;
X1 = xmin1:x_res:xmax1;
%% There are 2 y-coordinates on the circle for most x-coordinates.
%% We need to duplicate every x-coordinate so we can match each x with
%% its pair of y-values.
%% Method chosen: repeat the x-coordinates as the circle "wraps around"
%% e.g.: x = [0 0.1 0.2 ... end end ... 0.2 0.1 0]
N = length(X);
x = [X flip(X)];
N1 = length(X1);
x1 = [X1 flip(X1)];
%% ytemp1: vector of y-values as we sweep along the circle left-to-right
%% ytemp2: vector of y-values as we sweep along the circle right-to-left
%% Whether we take positive or negative values first is arbitrary
ytemp1 = zeros(1,N);
ytemp2 = zeros(1,N);
ytemp11 = zeros(1,N1);
ytemp22 = zeros(1,N1);
for i = 1:1:N
square = sqrt(r^2 - X(i)^2 + 2*X(i)*h - h^2);
ytemp1(i) = k - square;
ytemp2(N+1-i) = k + square;
end
for i = 1:1:N1
square1 = sqrt(r1^2-X1(i)^2 + 2*X1(i)*h1 - h1^2);
ytemp11(i) = k1 - square1;
ytemp22(i) = k1 + square1;
end
y = [ytemp1 ytemp2];
y1 = [ytemp11 ytemp22];
%% plot the (x,y) points
figure(1)
plot(x,y)
hold on
plot(x1,y1)
axis([-5 5 -5 5]);

サインインしてコメントする。


Steven Lord
Steven Lord 2020 年 12 月 25 日 5:29
Another possibility is to approximate the circle using a polyshape with a large number of sides and plot that polyshape.
p = nsidedpoly(1000, 'Center', [2 3], 'Radius', 5);
plot(p, 'FaceColor', 'r')
axis equal

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by