sheng/shape and text

From CDS 130

Jump to: navigation, search


  1. Objective
  2. Motivation
  3. Pre-questions
  4. Slides
    1. Using text in Matlab
    2. Using fill to draw polygons
    3. Using plot in matlab
    4. example.m

1. Objective

  • To introduce basic Matlab commands for drawing text and polygons.
  • To introduce ways to combine text, polygons with filled colors to generate figures.

2. Motivation

  • Computing is often done with generating figures (pictures and plots) for scientific reports.
  • Computing is often done on images with interesting features.

3. Pre-questions

  • How to display text on images?
  • How to generate different colors?
  • How to draw polygons and circles?
  • How to make simple x-y plots?
  • How to print/save images?

4. Slides

4.1. Using text in Matlab

Syntax: text(X,Y,'string') 

adds the text in the quotes to location (X,Y) on the current axes, where (X,Y) is in units from the current plot. If X and Y are vectors, TEXT writes the text at all locations given. If 'string' is an array the same number of rows as the length of X and Y, TEXT marks each point with the corresponding row of the 'string' array.

text (0.5, 0.5, 'my text')
text (0.5, 0.5, 'my text', 'color', 'r')
text (0.5, 0.5, 'my text', 'color', [1,0,0], 'FontSize', 16)

4.2. Using fill to draw polygons

fill (x, y, 'g')
fill (x, y, 'r', 'EdgeColor', [1, 1, 1])

example: using fill to draw a square

 clear all; clc; 
 x = [0 1 1 0]; 
 y = [0 0 1 1]; 
 fill (x, y, 'g');

 %% remove axes
 axis off

 %% make the current axes region square 
 axis square 

 %% retain the current plot while adding new plots
 hold on

 x = x * 0.5
 y = y * 0.5
 fill (x, y, 'r')

 %% using print to generate a figure. The file name must be a string with single quotation marks.
 print -dpng 'square.png' 

example: using fill to draw a hexagon

%% linspace(a, b, n): generates a row vector y of n points linearly spaced between a and b
%% (The first point starts from a, and the last point ends at b)
theta = linspace(0, 2*pi, 7)
r = 2.0; 
x = r * sin(theta); 
y = r * cos(theta); 
fill (x, y, 'k', 'EdgeColor', 'y'); 
%% set the limits of the x-axis and y-axis of the current plot axis([xmin xmax ymin ymax])
axis([-4 4 -4 4])
axis square
axis off
print -dpng 'hexagon.png'

4.3. Using plot in matlab

plot(x, y, 'k')
plot(x, y, '-rg')

plots vector Y versus vector X. If X or Y is a matrix, hen the vector is plotted versus the rows or columns of the matrix, whichever line up. If X is a scalar and Y is a vector, disconnected line objects are created and plotted as discrete points vertically at X.

Be familiar with axis and title

axis off 
axis square

4.4. example.m

% Script Eg4_2
% Displays interpolants of the colors cyan and magenta

% Prepare the figure window...
close all
axis equal off
hold on

% Color initializations...
cyan    = [0 1 1];  % rgb of the "bottom" color
magenta = [1 0 1];  % rgb of the "top" color
n = 10;             % the number of "in between" colors is n-1
x = [0 3 3 0];      % locates the x-values in the tiles
y = [0 0 1 1];      % locates the y-values in the tiles

% Add colored tiles to the figure window...
for j=0:n
    % Display the jth tile and its rgb value...
    f = j/n;
    v = (1-f)*cyan + f*magenta;
    text(3.5,j+.5,fprintf('[ %4.2f , %4.2f , %4.2f ]',v(1),v(2),v(3)))
hold off

  • Two examples:

x = linspace(0, 2*pi, 100);
y = sin(x); 
plot(x, y, 'r*') 
axis ([0, 2*pi, -1.5, 1.5])
axis square
xlabel ('xlabel')
ylabel ('sin(x)')
text(4.0, 1.0, 'sin(x)', 'FontSize', 20, 'Color', 'y')
hold on
theta = pi/2; 
y = sin(x + theta)
plot (x, y, 'g^')
print -dpng mypic.png
clear all; clc; 
x = [0, 1, 1, 0]; 
y = [0, 0, 1, 1]; 
fill (x, y, 'r')
axis off
axis equal
hold on
fill (x+1, y, 'g')
fill (x, y+1, 'g')
fill (x+1, y+1, 'r')
theta = linspace(0, 2*pi, 7) 
x = cos(theta)
y = sin(theta) 
fill (x +1 , y +1, [0.8, 0.6, 0.5])
fill (x*0.75 +1 , y*0.75 +1, 'w')
Personal tools