Friday, February 27, 2015

Post #5. Some ascii art made in MATLAB

ASCII art is a text based artistic representation. It is possible to find incredible examples of ASCII art in the internet. There is lot of methods to create them. I developed an automated code to extract the spatial information of color from a picture file and insert it in a plot with some characters. Below, some examples of ASCII arts that I made in MATLAB codes. The basic idea of the ASCII art codes in MATLAB is to use a function named imread ( ). This function read .jpg, .bmp or .png files as matrix. After that,  I used a function named text( ) to plot the character using the color information from the graphical matrix. The code is too long, but below I wrote the fundamental steps:

I = imread(File); %This line read the picture as a matrix. File is the name of the picture address in your computer.

%The lines below shows how to plots the characters (in the example is the character 'x') using the colot information from the picture file. a and b variables are the units of spacing between the characters in the x and y axis, respectively.
[numrow,numcol] = size(I);
for i = 1:a:numrow
    for j = 1:b:numcol
            text(j,i,'x','Color',[I(i,j,1)/255 I(i,j,2)/255 I(i,j,3)/255],'FontName','Arial', 'FontSize',8);
   end
end





All the best,

Givago (givagosouza@gmail.com)

Saturday, January 17, 2015

Post #4. Drawing in MATLAB.

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