You are on page 1of 7

To draw a 2D circle centered at the origin, you can use the cylinder function :

Radius = 2;
[x,y,z] = cylinder(Radius,200);
plot(x(1,:),y(1,:))
axis equal

1.5

0.5

-0.5

-1

-1.5

-2
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5

r = 1;
[theta,phi] = ndgrid(linspace(0,pi),linspace(0,2*pi));
X = r*cos(phi).*sin(theta);
Y = r*sin(phi).*sin(theta);
Z = r*cos(theta);
surf(X,Y,Z)
1

0.5

-0.5

-1
1
0.5 1
0 0.5
0
-0.5 -0.5
-1 -1

or if you just want circles, do this:


r = 1;
[theta,phi] = ndgrid(linspace(0,pi),linspace(0,2*pi));
X = r*cos(phi).*sin(theta);
Y = r*sin(phi).*sin(theta);
Z = r*cos(theta);
for k = 1:numel(theta)
plot3(X(k,:),Y(k,:),Z(k,:))
hold on
end
1

0.5

-0.5

-1
1
0.5 1
0 0.5
0
-0.5 -0.5
-1 -1

x = linspace(-sqrt(10),sqrt(10));
y1 = sqrt(10-x.^2);
y2 = -sqrt(10-x.^2);
plot(x,y1,x,y2)
axis equal
3

-1

-2

-3
-4 -3 -2 -1 0 1 2 3 4

% Create a logical image of a circle with specified


% diameter, center, and image size.
% First create the image.
imageSizeX = 640;
imageSizeY = 480;
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = 320;
centerY = 240;
radius = 100;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
image(circlePixels) ;
colormap([0 0 0; 1 1 1]);
title('Binary image of a circle');
Binary image of a circle

50

100

150

200

250

300

350

400

450

100 200 300 400 500 600

% Create a logical image volume of a sphere with specified


% diameter, center, and image size.
% First create the image.
imageSizeX = 100;
imageSizeY = 100;
imageSizeZ = 100;
[columnsInImage rowsInImage pagesInImage] = meshgrid(1:imageSizeX,
1:imageSizeY,1:imageSizeZ);

% Next create the sphere in the image.


centerX = 40;
centerY = 60;
centerZ = 50;
radius = 36;
sphereVoxels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 + (pagesInImage - centerZ).^2 <=
radius.^2;

% sphereVoxels is a 3D "logical" array.


% Now, display it using an isosurface and a patch
fv = isosurface(sphereVoxels,0);
patch(fv,'FaceColor',[0 0 .7],'EdgeColor',[0 0 1]);
view(45,45);
axis equal;
title('Binary volume of a sphere');
Binary volume of a sphere

80

60

40

20

20 80

40 60

60 40

% Create a random set of coordinates in a circle.


% First define parameters that define the number of points and the circle.
n = 5000;
R = 20;
x0 = 50; % Center of the circle in the x direction.
y0 = 90; % Center of the circle in the y direction.

% Now create the set of points.


% For a full circle, use 0 and 2*pi.
angle1 = 0;
angle2 = 2*pi;
% For a sector, use partial angles.
% angle1 = pi/4;
% angle2 = 3*pi/4;
t = (angle2 - angle1) * rand(n,1) + angle1;
r = R*sqrt(rand(n,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);
r = R*sqrt(rand(n,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);

% Now display our random set of points in a figure.


plot(x,y, '.', 'MarkerSize', 5)
axis square;
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
fontSize = 30;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Random Locations Within a Circle', 'FontSize', fontSize);
Random Locations Within a Circle
110

105

100

95

Y
90

85

80

75

70
30 35 40 45 50 55 60 65 70

You might also like