I would like to make a triangle like this one using these source codes that I made already:
function pyramid(a)
for b = 1:a
base = (sqrt(3)*b)/2;
height = 1 - (b * 3/2)
c = [-base base 0 -base];
d = [height height 1 height];
plot(c,d); hold on
end
this only plots the structure of the triangle
how do i make it plot like that? (with inner triangles)

 채택된 답변

Jan
Jan 2021년 4월 27일

0 개 추천

a = 5;
base = sqrt(3) / 2;
height = 1 - 3/2;
c = [-base, base, 0, -base];
d = [height, height, 1, height];
figure;
axes('NextPlot', 'add'); % As: hold on
for ia = 1:a
for ib = 1:ia
plot(c + (2 * ib - ia) * base, d + 3 * ia * height);
end
end

댓글 수: 4

Blake Steven Robers
Blake Steven Robers 2021년 4월 27일
I can't seem to squeeze the plot between y = 1 and y = 1 - height, how can i change it that way
Jan
Jan 2021년 4월 27일
I do not understand, what you want. What should be "squeezed"? You can scale the variables base and hight however you like.
Blake Steven Robers
Blake Steven Robers 2021년 4월 30일
The y-axis of the plot should be betwwen 1 and [ 1 - height ]. While its x axis is between [ - base ] and [ + base ]. So the triangle will be just be "squeezed" between those coordinates. Sorry for asking again.
Jan
Jan 2021년 5월 1일
There is no need to say "sorry", because questions about Matlab are the purpose of this forum. Let me mention, that you did not aks a question in your last comment.
According to your description you have to divide the positions by a sacalar factor and shift the y positions by a scalar constant. This is simple maths.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2021년 4월 27일

편집:

2021년 5월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by