94 views (last 30 days)

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!!

Payas Bahade
on 27 May 2020

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.

Hope this helps!

Payas Bahade
on 31 May 2020

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])

% saving the figure

output=gcf;

savefig(output,'Triangle.fig');

Ron
on 1 Sep 2020

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.

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/532688-draw-filled-triangle-and-possibly-change-aspect-ratio#comment_862043

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/532688-draw-filled-triangle-and-possibly-change-aspect-ratio#comment_862043

Sign in to comment.