フィルターのクリア

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

15 ビュー (過去 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?

採用された回答

Matt J
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
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.
Matt J
Matt J 2022 年 2 月 11 日
No, I kept the face ordering specified in the OP.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by