Effacer les filtres
Effacer les filtres

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

15 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

Matt J
Matt J le 11 Fév 2022
Modifié(e) : Matt J le 11 Fév 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 commentaires
Walter Roberson
Walter Roberson le 11 Fév 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.
Matt J
Matt J le 11 Fév 2022
No, I kept the face ordering specified in the OP.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by