views:

474

answers:

2

I have a file a.txt which is like:

0 0 0 3 4 3
0 0 3 0 3 4
0 1 0 4 4 4
0 1 3 1 3 5
0 2 0 5 4 5
0 3 0 0 4 0

These are vertices of triangles [x1 y1 x2 y2 x3 y3] that I need to plot on a 6x6 grid. I need to see these triangles on a single graph.

How can this be done in MATLAB?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ thanks a lot everyone!

finally what worked:

a = dlmread('a.txt');

clf
xlim([0 6])
ylim([0 6])
for i = 1:size(a,1)

   line(a(i,[1:2:5,1]), a(i,[2:2:6,2]), 'color',rand(1,3))
   pause;

end
grid on;
+6  A: 
a = dlmread('a.txt')
clf

for i = 1:size(a,1)
    line(a(i,[1:2:5,1]), a(i,[2:2:6,2]), 'color',rand(1,3))
end

Notice that I am repeating the vertice to complete the triangle and the I am using a random color each time through the loop.

Because the format is easy, I can use DLMREAD with defaults.

MatlabDoug
thanks @MatlabDoug, how can I make it pause for a keypress after adding a triangle to the image?
Lazer
You can use pause.
Jacob
add **pause** command after line()
Amro
+2  A: 

You can use the PATCH function to accomplish this, although many of the triangles you have specified lay on top of one another:

a = [0 0 0 3 4 3; ...  % A variable "a" containing the data from the file
     0 0 3 0 3 4; ...
     0 1 0 4 4 4; ...
     0 1 3 1 3 5; ...
     0 2 0 5 4 5; ...
     0 3 0 0 4 0];
x = a(:,[1 3 5])';  % Get the x coordinates, one set per column
y = a(:,[2 4 6])';  % Get the y coordinates, one set per column
patch(x,y,'r');     % Use patch to plot one triangle per column, colored red
gnovice
most of the triangles I have are overlapping, so patch() will not be that useful, unless it can display the triangles one by one, say after pausing for a keypress?
Lazer
Perhaps adding the third dimension might help:patch(x,y, repmat(1:size(a,1),3,1), 'r'); view(3)
Amro