I am trying to draw a pyramid.
51 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Michael
am 7 Dez. 2013
Beantwortet: sixwwwwww
am 7 Dez. 2013
I'm not asking you to do my homework, but guide me through what I am doing wrong. I am trying to create a function that draws a pyramid by accepting 4 inputs. The origin, the length in the x direction, width in the y direction and height in the z direction. If you run my code, you will get a pyramid but with a little extra. You will see what I mean if you run it. I tried defining the x,y, and z arrays that define the 5 faces of the pyramid.
Here's my code:
function [] = pyramid(P,l,w,h) % PYRAMID will accept four inputs. P0 is an array of 3 scalar numbers for % the origin (x, y, and z). l is the length of the box in the x-direction, % w is the width of the box in the y-direction, and h is the height of the % box in the z-direction. The functin will draw a square pyramid. % Input: Four inputs, an array of the point of origin, a length, width, and % height. % Output: A pyramid drawn with a set transparency and different colors for % the faces % Proecessing: The first thing I will do is use the figure funciton in % order to prevent any previous figures from being overwritten.
figure x = [P(1),P(1)+l,P(1)+l,P(1)]; y = [P(2),P(2),P(2)-w,P(2)-w]; z = [P(3),P(3),P(3),P(3)]; face1 = [x;y;z]; surf(face1) fill3(x,y,z,'blue') hold on x2 = [P(1),P(1)+l,P(1) + l/2]; y2 = [P(2),P(2),P(2) - w/2]; z2 = [P(3),P(3),P(3)+h]; face2 = [x2;y2;z2]; surf(face2) fill3(x2,y2,z2,'green') x3 = [P(1)+l,P(1)+l,P(1) + l/2]; y3 = [P(2),P(2)-w,P(2)- w/2]; z3 = [P(3),P(3),P(3)+h]; face3 = [x3;y3;z3]; surf(face3) fill3(x3,y3,z3,'red') x4 = [P(1)+l,P(1),P(1)+ l/2]; y4 = [P(2)-w,P(2)-w,P(2)- w/2]; z4 = [P(3),P(3),P(3)+h]; face4 = [x4;y4;z4]; surf(face4) fill3(x4,y4,z4,'green') x5 = [P(1),P(1),P(1) + l/2]; y5 = [P(2),P(2)-w,P(2)- w/2]; z5 = [P(3),P(3),P(3)+h]; face5 = [x5;y5;z5]; surf(face5) fill3(x5,y5,z5,'red') alpha(0.3) hold off end
0 Kommentare
Akzeptierte Antwort
sixwwwwww
am 7 Dez. 2013
Dear Micheal, you don't need to use surf for this purpose just use fill. Below is your corrected code:
function [] = pyramid(P,l,w,h)
% PYRAMID will accept four inputs. P0 is an array of 3 scalar numbers for
% the origin (x, y, and z). l is the length of the box in the x-direction,
% w is the width of the box in the y-direction, and h is the height of the
% box in the z-direction. The functin will draw a square pyramid.
% Input: Four inputs, an array of the point of origin, a length, width, and
% height.
% Output: A pyramid drawn with a set transparency and different colors for
% the faces
% Proecessing: The first thing I will do is use the figure funciton in
% order to prevent any previous figures from being overwritten.
figure
x = [P(1),P(1)+l,P(1)+l,P(1)];
y = [P(2),P(2),P(2)-w,P(2)-w];
z = [P(3),P(3),P(3),P(3)];
fill3(x, y, z ,'blue'), hold on
x2 = [P(1),P(1)+l,P(1)+ l/2];
y2 = [P(2),P(2),P(2)-w/2];
z2 = [P(3),P(3),P(3)+h];
fill3(x2, y2, z2,'green'), hold on
x3 = [P(1)+l,P(1)+l,P(1) + l/2];
y3 = [P(2), P(2)-w,P(2)- w/2];
z3 = [P(3),P(3),P(3)+h];
fill3(x3, y3, z3,'red'), hold on
x4 = [P(1)+l,P(1),P(1)+ l/2];
y4 = [P(2)-w,P(2)-w,P(2)- w/2];
z4 = [P(3),P(3),P(3)+h];
fill3(x4,y4,z4,'green'), hold on
x5 = [P(1),P(1),P(1) + l/2];
y5 = [P(2),P(2)-w,P(2)- w/2];
z5 = [P(3),P(3),P(3)+h];
fill3(x5,y5,z5,'red')
Good luck!
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!