In: Advanced Math
MATLAB please make it simple to understand
P4.2.5 Write a script that draws an equilateral triangle that is partitioned into four smaller equilateral
triangles by connecting the midpoints of its sides. The four little triangles should be colored
differently.
MATLAB CODE :
%Let's make an equilateral triangle of side 2 with vertices as
A(0,0),B(0,2) and C(sqrt(3),1)
V = [0 0; 2 0 ;1 sqrt(3)]; %defining vertices
plot([V(1,1),V(2,1)], [V(1,2),V(2,2)], 'Color', 'k');
hold on
plot([V(2,1),V(3,1)], [V(2,2),V(3,2)], 'Color', 'k');
hold on
plot([V(3,1),V(1,1)], [V(3,2),V(1,2)], 'Color', 'k');
% We'll join the midpoints of each line, coordinates of midpoint of
each line
v = [ 1 0 ; 0.5 sqrt(3)/2; 1.5 sqrt(3)/2]; %[1 0 ;% 1*cos 60 1* sin
60; 2 - 1*cos 60 1*sin60]
plot([v(1,1),v(2,1)], [v(1,2),v(2,2)], 'Color', 'k');
hold on
plot([v(2,1),v(3,1)], [v(2,2),v(3,2)], 'Color', 'k');
hold on
plot([v(3,1),v(1,1)], [v(3,2),v(1,2)], 'Color', 'k');
% Using vertices of all 4 triangles, we can fill the triangle with
patch command;
% 1st triangle
x1 = [ 0 0.5 1];
y1 = [ 0 sqrt(3)/2 0];
patch(x1,y1,'g')
%2nd triangle
x2 = [ 0.5 1 1.5];
y2 = [ sqrt(3)/2 sqrt(3) sqrt(3)/2];
patch(x2,y2,'b')
%3rd triangle
x3 = [ 1.5 2 1];
y3 = [ sqrt(3)/2 0 0];
patch(x3,y3,'r')
% 4th triangle
x4 = [ 1 0.5 1.5];
y4 = [ 0 sqrt(3)/2 sqrt(3)/2];
patch(x4,y4,'y')
Result: