How to write a Matlab script that is able to output images of triangles based on the size input? The following shows the half of a triangle but i can't find a way to make an isolated triangle. (The size you can put at range 3 - 8 )

조회 수: 3 (최근 30일)
x=input('Size: ') X=triu(ones(x),1) imshow(X)
  댓글 수: 2
Kenneth Phang
Kenneth Phang 2016년 10월 5일
Is there anyway for me to make a matrix to this?
1 1 1 1 1 1 1 0 1 1 1 1 1 1 1
1 1 1 1 1 1 0 0 0 1 1 1 1 1 1
1 1 1 1 1 0 0 0 0 0 1 1 1 1 1
1 1 1 1 0 0 0 0 0 0 0 1 1 1 1
1 1 1 0 0 0 0 0 0 0 0 0 1 1 1
1 1 0 0 0 0 0 0 0 0 0 0 0 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Matthew Eicholtz
Matthew Eicholtz 2016년 10월 5일
See the second option in my answer below (insertShape). More specifically for the matrix above, use:
x = ones(8,15);
x = insertShape(x,'filledpolygon',[1 8 8 1 15 8],'Color','black','Opacity',1);
x = (x(:,:,1)==1);

댓글을 달려면 로그인하십시오.

채택된 답변

Matthew Eicholtz
Matthew Eicholtz 2016년 10월 5일
편집: Matthew Eicholtz 2016년 10월 5일
What types of triangles do you need? And what will they be used for?
If you need very basic triangles, you could use the triangular marker types for scatter plots:
[y,x] = meshgrid(1:4);
z = 250:200:1000; %size of markers
figure;
axis([0 5 0 5]);
hold on;
scatter(x(1,:),y(1,:),z,'r^','FaceColor','r');
scatter(x(2,:),y(2,:),z,'gv','FaceColor','g');
scatter(x(3,:),y(3,:),z,'b<','FaceColor','b');
scatter(x(4,:),y(4,:),z,'m>','FaceColor','m');
hold off;
Or, if you need to create triangles with more variation in size, shape, and vertex position, try using insertShape:
figure;
I = zeros(100,'uint8');
J = insertShape(I,'filledpolygon',randi([10 90],1,6),'Color','white','Opacity',1);
imshow(J);
Note that this option actually embeds the triangle in an image. If you want to have a triangle "object", try using patch:
figure;
x = randi([0 10],1,3); %random x-coordinates
y = randi([0 10],1,3); %random y-coordinates
patch(x,y,'green');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by