Object Oriented Programming: How to create a child class that gives two parameters for working of parent class in Matlab?

6 views (last 30 days)
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?

Accepted Answer

Matt J
Matt J on 11 Feb 2022
Edited: Matt J on 11 Feb 2022
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 Comments
Walter Roberson
Walter Roberson on 11 Feb 2022
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!