An interesting thing that MATLAB helped me was to observe the world as a matrix. Everything can be represented by a set of numbers. In the current posting, I want to show some codes to draw shapes. The more important point for most of the examples is to find coordinates of specific points that delimitate the shape you want to draw. The Figure 1 shows an example of a shape (triangle) with the three coordinates (in red) that can be used as reference to draw it.
Figure 1. Example of triangle drawn using MATLAB. The coordinates from three points were enough to be used to draw it.
We can read the code to draw a triangle as follows. The coordinates from x axis and y axis must be organized in the sequence of the drawing.
%CODE #1
a = 0;
b = 0.5;
c = 1;
x1 = [a b c a]; % x coordinates
y1 = [a c a a]; % y coordinates
plot(x1,y1,'-')
axis([-4 4 -4 4])
Now, a code to draw a square!
%CODE #2
a = 0;
b = 0.5;
x1 = [a a b b a]; % x coordinates
y1 = [a b b a a]; % y coordinates
plot(x1,y1,'-')
axis([-4 4 -4 4])
Now, a code to draw irregular shape!
%CODE #3
a = 1;
b = 2;
c = 4;
x1 = [a a b c b a];
y1 = [a b c b b a]
plot(x1,y1,'-')
axis([-4 4 -4 4])
Figure 2. The triangle, square and irregular shape drawn using the previous codes (Codes #1, #2, and #3, respectively).
An exception!! Circles!! Circle could be drawn after we find many, many, many coordinates, but it will spend lot of time of us!! It is easier such as follows.
%CODE #4
ang = 0:(2*pi/40):2*pi; %Sequence of 40 angles between 0 and 2 times pi
radius = 2; % Radius of the circle
x1 = cos(ang)*radius;
y1 = sin(ang)*radius;
plot(x1,y1,'-')
axis([-4 4 -4 4])
axis square
Figure 3. Circle drawn using the code #4.
Contact me to discuss about more codes. All the best.
Givago
givagosouza@gmail.com