Draw a Filled Circle in Matlab
Introduction to Matlab Plot Circle
MATLAB can exist used to perform operations involving geometric figures like circles, rectangles, squares etc. In this article, nosotros will focus on circles. Nosotros will learn how to create diverse types of circles in MATLAB. We tin can create solid or plane circles in MATLAB, which we will acquire as we go ahead in the article. Nosotros will besides acquire how to create a circumvolve using the rectangle function.
How to Create a circumvolve using Rectangle Role?
Let us offset larn syntax to draw a simple circle in MATLAB:
1. Let usa offset declare some points, here nosotros are taking 500 points. The below code will create these points.
- angles = linspace(0, 2*pi, 500);
2. Let us now declare the radius and centre of the circle. The eye will be defined by ten and y co-ordinates.
- radius = 20;
- CenterX = l;
- CenterY = 40;
3. Finally, we will plot our circumvolve.
- x = radius * cos(angles) + CenterX;
- y = radius * sin(angles) + CenterY;
iv. We volition likewise write some lawmaking for our output to look visually better. This is normal formatting and nosotros tin adjust it as per our requirement.
- plot(x, y, 'b-', 'LineWidth', 2);
- hold on;
- plot(CenterX, CenterY, 'k+', 'LineWidth', three, 'MarkerSize', 14);
- filigree on;
- axis equal;
- xlabel('X', 'FontSize', 14);
- ylabel('Y', 'FontSize', xiv);
five. This is how our input and output will look like in MATLAB console:
Code:
angles = linspace(0, ii*pi, 500);
radius = 20;
CenterX = 50;
CenterY = 40;
x = radius * cos(angles) + CenterX;
y = radius * sin(angles) + CenterY;
plot(x, y, 'b-', 'LineWidth', two);
hold on;
plot(CenterX, CenterY, 'yard+', 'LineWidth', three, 'MarkerSize', 14);
grid on;
centrality equal;
xlabel('X', 'FontSize', xiv);
ylabel('Y', 'FontSize', 14);
Output:
As we can see in the above output, the circumvolve is created with a radius 20 and center (50, 40) as defined past united states of america in the code.
How to Create a Solid 2D Circumvolve in MATLAB?
Next, permit us learn how to create a solid 2D circumvolve in MATLAB:
1. First, we volition be creating logical image of circle. For this, we will define eye, diameter and the image size. Permit us first create prototype.
- imageSizeOfX = 640;
- imageSizeOfY = 480;
- [colInImage rowsInImage] = meshgrid(1 : imageSizeOfX, ane : imageSizeOfY);
two. Next, we volition be creating the circle inside the prototype.
- centerOfX = 320;
- centerOfY = 240;
- radius = lxxx;
- Pixels = (rowsInImage – centerOfY).^two …
- + (colInImage – centerOfX).^ii <= radius.^two;
three. In the higher up line of lawmaking, Pixels is "logical" array and is 2D. Permit us at present display 'Pixels'.
- image(Pixels);
- colormap([0 0 0; one 1 1]);
- championship('Paradigm of circle');
4. This is how our input and output will await similar in MATLAB console:
Code:
imageSizeOfX = 640;
imageSizeOfY = 480;
[colInImage rowsInImage] = meshgrid(ane : imageSizeOfX, 1 : imageSizeOfY);
centerOfX = 320;
centerOfY = 240;
radius = 80;
Pixels = (rowsInImage - centerOfY).^2 ...
+ (colInImage - centerOfX).^2 <= radius.^2;
image(Pixels);
colormap([0 0 0; ane 1 1]);
title('Prototype of circle');
Output:
How to create a Circle in MATLAB Using Rectangle Function?
Let us now learn how to create a circle in MATLAB using rectangle role: Here is a unproblematic code to achieve this:
one. Like we discussed in above examples, we will declare the radius and heart co-ordinates of the required circumvolve.
- radius = 6;
- centerX = 30;
- centerY = 40;
- rectangle('Position',[centerX – radius, centerY – radius, radius*2, radius*ii],…
- 'Curvature',[ane,1],…
- 'FaceColor','b');
- axis square;
ii. Nosotros have passed 'FaceColor' equally "b" so our output circumvolve volition be of Blue colour.
Lawmaking:
radius = 6;
centerX = 30;
centerY = 40;
rectangle('Position',[centerX - radius, centerY - radius, radius*2, radius*two],...
'Curvature',[ane,1],...
'FaceColor','b');
axis square;
Output:
How nosotros tin can Create a Simple arc in MATLAB?
Finally, permit us discuss how we can create a simple arc in MATLAB. Every bit we know that arc is nothing but a small portion of the circle, code for creating an arc is as well very like to that of creating a circle.
1. Offset we define the parameters of required arc.
- xCenter = 1;
- yCenter = 1;
- radius = 4;
ii. Side by side, we define the angle theta as required.
- theta = linspace(20, 100, l);
- x = radius * cosd(theta) + xCenter;
- y = radius * sind(theta) + yCenter;
three. Finally, we plot our defined points.
- plot(10, y, 'b-', 'LineWidth', 2);
- axis equal;
- grid on;
Lawmaking:
xCenter = one;
yCenter = ane;
radius = 4;
theta = linspace(20, 100, fifty);
ten = radius * cosd(theta) + xCenter;
y = radius * sind(theta) + yCenter;
plot(x, y, 'b-', 'LineWidth', 2);
centrality equal;
grid on;
Output:
Conclusion
So, in this article, we learnt how to create circles in MATLAB. We can create both plane circles and solid circles in MATLAB. We as well learnt how we can leverage the Rectangle function to plot circles in MATLAB. We can likewise format our circle every bit per our requirement.
Recommended Articles
This is a guide to Matlab Plot Circumvolve. Hither we discuss an introduction, how to Create a circle using rectangle part, a Solid 2D Circle, a circle in MATLAB and Uncomplicated arc. You can as well get through our other related manufactures to acquire more –
- Pause in MATLAB
- Nested Loop in Matlab
- Matlab pcolor() | Examples
- Complete Guide to Optimset Matlab
- Plot Vector Matlab | Functions
- Matlab Figure | Examples
- xlabel Matlab | Examples
Source: https://www.educba.com/matlab-plot-circle/
Post a Comment for "Draw a Filled Circle in Matlab"