The code below is used to draw a simple octagon on the screen with the word stop in the middle.
My question is, can anyone explain what is happening in the top statement. (variable t). What does the (1:2:15)' do?
Also, for example, if I wanted to draw a circle on the screen, how would I modify this code to do so.
t = (1:2:15)'*pi/8; x = sin(t); y = cos(t); fill(x,y,'r') axis square off text(0,0,'STOP','Color',[0 0 0],'FontSize',80,'FontWeight','bold','HorizontalAlignment','center') title('Figure STOP')
1:2:15 creates a vector like this:
The ' puts the values in lines (rows) instead of columns.
first vale is 1, step is 2 and last value is 15
If you want to create the circle you can do this
and keep the rest of the code without any changes
x and y are being created via a "parameterization" through t. The variable t is running from 0 to 2pi (i.e. around the circumference of the circle), but it does so in eight discrete intervals. (That's why you get eight sides.)
If you breakpoint your code after the first line, you will see that t takes equally spaced values around the circle. (The "linspace" command would have been another way to do this.) The part "1:2:15" is the vector [1 3 5 7 9 11 13 15].
Play games and win prizes!Learn more