Draw filled triangle and possibly change aspect ratio

조회 수: 64 (최근 30일)
Chen
Chen 2020년 5월 25일
댓글: Ron 2020년 9월 1일
How can I draw a filled equilateral triangle (actually white) with background black? I don't want to have any x and y axis on the drawing. I just need the graph.
And also how can I define an aspect ratio (hight:width) of the triangle and then get the different triangles of different aspect ratio from an equilateral triangle?
I think some logic for the length of pixels should work but I could not figure out.
Thanks in adavance!!
  댓글 수: 1
Rik
Rik 2020년 5월 25일
You can probably solve your question with the patch function. You only need to calculate the coordinates of the corners.

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

채택된 답변

Payas Bahade
Payas Bahade 2020년 5월 27일
Hi Chen,
As mentioned by Rik, you can use patch command to draw the triangles and set axes properties to get desired results. Below mentioned code will help you draw triangles with specified aspect ratio:
w=10; %width of triangle
ar=0.866; % Aspect ratio for equilateral triangle
h=ar*w;%height of triangle
x=[0 w w/2];%x coordinates of vertices
y=[0 0 h];%y coordinates of vertices
patch(x,y,'white') %plotting triangle in white color
set(gca,'Color','k','xticklabel',[],'yticklabel',[])%setting background black and removing labels
daspect([1 1 1]);%equal data unit length along x and y axis
xlim([-5 15])
ylim([-5 15])
You can tweak above code to get required geometry for the triangle.
Kindly refer this documentation link to know more details on using ‘patch’ command.
Hope this helps!
  댓글 수: 5
Payas Bahade
Payas Bahade 2020년 5월 31일
You can define the axes limits in terms of triangle's width and height to get require spacing around the triangle as follows:
% setting the axes limits
xlim([-w/2 1.5*w])
ylim([-w/2 h+w/2])
And to save the output figure, you can use 'savefig' command as follows:
% saving the figure
output=gcf;
savefig(output,'Triangle.fig');
Ron
Ron 2020년 9월 1일
Hi Payas,
Can you help me please? I used the code above to create one triangle. How can I create randomly distributed 500 triangles of size 3.4 x 3.4 x 3 cm? Thank you.

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

추가 답변 (1개)

Chen
Chen 2020년 6월 1일
편집: Chen 2020년 6월 1일
a solution by myself:
Use the logic of the pixels' positions:
function trianglePixels = ar_t_man(i)
a = zeros(150,150); %define graph size -- you can change
% will fill the triangle by value 1 (binary)
a(20,size(a,2)/2) = 1; %define position of the top point in the triangle
% -- you can change
b = 1:i*100; %how many columns in the triangle due to aspect ratio
for j = 1:size(b,2)
a(20+b(j),floor(size(a,2)/2-(50/size(b,2)*j)):floor(size(a,2)/2+(50/size(b,2)*j))) = 1; %the actual triangle
end
trianglePixels = a;
imshow(a, [])
end

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by