Object Oriented Programming: How to create a child class that gives two parameters for working of parent class in Matlab?
14 次查看(过去 30 天)
显示 更早的评论
Parent class that created a 3D model
classdef shape
properties
name
color
opacity
vertices
faces
end
methods
function plot= shape(faces,vertices,color,opacity)
patch(faces,vertices,color,'FaceAlpha',opacity)
end
end
end
now,
next should be child class that gives two parameters 'faces' and 'vertices' to parent class
I tried this :-
classdef cube_shape < shape_1
properties
e
end
methods
function x= cube_shape(e)
x.e=e; % Edge length
v1=[0 0 0]; % 8 vertices of cube
v2=[e 0 0];
v3=[e e 0];
v4=[0 e 0];
v5=[0 0 e];
v6=[e 0 e];
v7=[e e e];
v8=[0 e e];
x.vertices =[v1;v2;v3;v4;v5;v6;v7;v8]; % combining 8 vertices together
x.faces=[1 2 3 4 1; % 6 faces of cube
5 6 7 8 5;
1 5 8 4 1;
1 5 6 2 1;
2 6 7 3 2;
3 7 8 4 3];
end
end
end
How do i use this x.vertices and x.faces in parent class?
0 个评论
采纳的回答
Matt J
2022-2-11
编辑:Matt J
2022-2-11
I had to make a number of changes to your code (see attached). However, once you've implemented the classes this way, you can do things like,
obj=cube_shape(1,'r',0.5);
obj.color='g';
5 个评论
Walter Roberson
2022-2-11
Did you happen to specifically ensure that the faces order was counter-clockwise so that the normals could be calculated properly for lighting purposes?
The code I posted in https://www.mathworks.com/matlabcentral/answers/892652-anybody-help-me-by-providing-matlab-code-for-the-three-dimensional-histograms#answer_863405 uses a different order for the vertices, so we are potentially producing the same outcome. When I created that code, I made sure that I used counter-clockwise consistently.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!