# How to draw a star in matlab

251 ビュー (過去 30 日間)
Christophre Dennen 2019 年 10 月 6 日

Essentially, I need to create a figure of the vietnamese flag, and have this so far:
clc
figure
axis([0 80 0 40]);
x1=[0 0 80 80];
y1=[0 40 40 0];
fill(x1,y1,'r')
hold on
x= [cos(pi/2),2/5*cos(pi/2+pi/5),cos(pi/2+2*pi/5),2/5*cos(pi/2+3*pi/5),cos(pi/2+4*pi/5),2/5*cos(3*pi/2),cos(pi/2+6pi/5),2/5*cos(pi/2+7*pi/5),cos(pi/2+8*pi/5),2/5*cos(pi/2+9*pi/5)];
y=[sin(pi/2),2/5*sin(pi/2+pi/5),sin(pi/2+2*pi/5),2/5*sin(pi/2+3*pi/5),sin(pi/2+4*pi/5),2/5*sin(3*pi/2),sin(pi/2+6pi/5),2/5*sin(pi/2+7*pi/5),sin(pi/2+8*pi/5),2/5*sin(pi/2+9*pi/5)];
plot(x,y)
fill(x,y, 'y')
Though I'm getting this error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
Sorry if my code is a bit rough, I'm still in my first few weeks of learning Matlab.
Thanks

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

### 回答 (3 件)

Adam Danz 2019 年 10 月 6 日

"I need to create a figure of the vietnamese flag"
I used this question as an excuse to play around with some of the image processing tools so I thought I'd share the idea of getting the star coordinates from the Vietnamese flag itself. I'd be happy to hear improvements to this method from anyone more familiar with these tools.
I downloaded the attached png image of the Vietnamese flag, read it into Matlab, computed the coordinates of the star edge, then scaled it to a normalized [-1,1] scale. You can use those coordinates to place the star anywhere on a new figure and the dimensions correspond to the actual flag.
% Read the image of the flag
% Show the flag (just to visually confirm a successful read)
imshow(I, cm)
You could actually stop here if you wanted.
Convert to binary (black & white)
% Convert image to binary
BW = im2bw(I,cm);
% Show the binary image (just to visually confirm a successful conversion)
imshow(BW)
Get the boundaries of the white object
% Get the boundaries of the white star
B = bwboundaries(BW);
B = B{1}; %only 1 white object image in this flag
Confirm that the bondary coordinates make sense
% confirm that the boundary coordinates make sense by plotting them
% on top of the imshow() image plotted above. Make axis ticks visible, too.
% Note the direction of the y axis!
hold on
plot(B(:,2),B(:,1),'r-','LineWidth',2)
axis on
Normalize the coordinates to [-1,1]
% Now you've got the coordinates of the star in pixel units.
% Let's convert them to normalized units ranging from [-1:1]
% based on the width of the image so you can scale the star any way you'd like.
Bnorm = ((B - min(B,[],1)) ./ (range(B(:,2))./2)) - 1;
% Exchange the x and y coordinates since bwboundaries() outputs [row,col] indices
Bnorm = fliplr(Bnorm);
% flip the y coordinates since imread uses reverse y axes
Bnorm(:,2) = -1 * Bnorm(:,2);
Plot the star on a new figure
% plot the star on a new axes, show center at (0,0)
figure()
plot(Bnorm(:,1), Bnorm(:,2), 'k-')
axis equal
axis tight
hold on
plot(0,0,'r+')
Change the center point and scale the size of the star
I'll skip the screen shots and let the curious viewers play around with this.
% To center the star on coordinate (2,3)
figure()
plot(Bnorm(:,1)+2, Bnorm(:,2)+3, 'k-')
% ^^ ^^
hold on
plot(2,3,'r+')
axis equal
axis tight
% To center the star on coordinates (2,3) and enlarge it from +/-1 to +/-5
plot(Bnorm(:,1)*5 +2, Bnorm(:,2)*5 +3, 'k-')
% ^^ ^^ ^^ ^^
hold on
plot(2,3,'r+')
axis equal
axis tight
Create a patch from the star coordinates and fill it will yellow. Plot it against a red axis.
Of course you can use an eyedropper color tool to get the real red and yellow colors from the flag image.
% To create a red background and fill the star with yellow
figure()
axes('color', 'r')
h = patch(Bnorm(:,1),Bnorm(:,2),[1 1 0],'EdgeColor', 'none');
axis equal
xlim([-3,3])
ylim([-2,2])

#### 0 件のコメント

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

the cyclist 2019 年 10 月 6 日

There are two places in your code where you wrote
6pi
6*pi
Assuming you are using the MATLAB editor, you should see that there were red lines on the far right, indicating errors in those lines. It will even show a little squiggle at the error. And if you hover over that, it tells you the error. See the documentation on Checking Code for Errors and Warnings for a much more complete description.

#### 2 件のコメント

Christophre Dennen 2019 年 10 月 6 日
Thank you! I'm not sure how I missed that so many times. Though, it still didn't print the star on the figure, so something else must still be wrong.
the cyclist 2019 年 10 月 6 日
I see a small star on the lower left. Maybe you need your eyes checked. ;-)

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

Adam Danz 2019 年 10 月 7 日

Here's an easy solution but the star size will not scale with axes size if the figure size is changed.
fh = figure();
rectangle('Position',[-3,-2,6,4],'EdgeColor','k','FaceColor','r')
hold on
plot(0,0,'yp','MarkerSize',100,'MarkerFaceColor', 'y')
axis equal

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

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by