Drawing a square with a circle using a function.
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
How would I write a function with the requirements down below:
The function takes at least one input argument: the size of the square-shaped image in number of pixels. The default color for the square is black, and the default color for the circular disk is white.
If a second input argument is provided, it should be the color of the circular disk. If a third input argument is provided, it will be the color of the square.
I need some overall help on where to get started. I'm not sure how I should add in the white circle, as well as how to change the colors.
I have tried using a function(can no longer remember) that would allow me to insert a circle, but I do not have the proper toolbox for it. I have created an all black matrix of however many pixels are entered by the user. Adding the circle is where I am currently stuck. Any suggestions would be helpful.
This is what I have created so far:
function week7hw3(pixels,varargin)
mat=zeros(pixels,pixels);
n=nargin;
if n==1
image(mat)
elseif n==2
end
end
Any help is appreciated!
3 Kommentare
Walter Roberson
am 21 Jul. 2019
Your mat should be uint8.
In the case of no extra arguments, you display mat, but mat is all zeros.
In the case of one extra argument, you activate a colormap according to the input, and then display mat, but mat is all zeros.
In the case of two extra arguments, you activate a colormap according to the input, and then display mat, but mat is all zeros and you ignore the first extra argument.
"If a second input argument is provided, it should be the color of the circular disk."
Okay, but in what form is it being passed in? Is it being passed in as the name of a colormap such as 'prism' ? Is it being passed in as the short name of a color, such as 'r' ? Is it being passed in as the name of one of the built-in colors, such as 'red' ? Is it being passed in as the name of a general color such as 'seafoam green' ? Is it being passed in as an rgb vector?
Antworten (1)
Jyothis Gireesh
am 2 Aug. 2019
I am assuming that the background and foreground colors are given in the RGB format and the order of function arguments are as (Square size, color of circle, color of square).
To determine the circular region, it may be helpful to use the Euclidean distance between the indexes of the center pixel and other pixels and compare it with half the length of the side of the square.
The circle’s color can be modified by initializing a 3D matrix with the R, G and B components in each channel. Multiply the image with the mask to determine the region of interest.
A similar method can be used to create the background region also. Combine these to get the final image.
This code may help you.
function y = argtest(varargin)
x = varargin;
[X, Y] = meshgrid(linspace(-1, 1, x{1}));
R = hypot(X, Y);
y = 255*(R<1);
if nargin>1
fore_ground = x{2};
matrix_fore = cat(3,fore_ground(1)*ones(x{1}),fore_ground(2)*ones(x{1}),fore_ground(3)*ones(x{1}));
matrix_fore = matrix_fore.*repmat((R<1),[1 1 3]);
y = matrix_fore;
if nargin == 3
back_ground = x{3};
matrix_back = cat(3,back_ground(1)*ones(x{1}),back_ground(2)*ones(x{1}),back_ground(3)*ones(x{1}));
matrix_back = matrix_back.*repmat((R>=1),[1,1,3]);
y = matrix_fore + matrix_back;
end
end
end
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!