0

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;
user1118321
  • 25,567
  • 4
  • 55
  • 86
Lazer
  • 90,700
  • 113
  • 281
  • 364
  • i am still searching. cannot find any triangle function in matlab... also one problem is that the graph gets automatically truncated near the highest values, how to get it fixed at 6x6? – Lazer Oct 01 '09 at 20:35
  • 1
    The auto setting of limits really deserves its own question. It is best to keep these questions focused. (using XLIM and YLIM will help though!) – MatlabDoug Oct 01 '09 at 20:40

2 Answers2

6
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
  • 5,704
  • 1
  • 24
  • 36
  • thanks @MatlabDoug, how can I make it pause for a keypress after adding a triangle to the image? – Lazer Oct 01 '09 at 20:41
2

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
  • 125,304
  • 15
  • 256
  • 359
  • 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 Oct 01 '09 at 20:48
  • Perhaps adding the third dimension might help: patch(x,y, repmat(1:size(a,1),3,1), 'r'); view(3) – Amro Oct 01 '09 at 21:06