필터 지우기
필터 지우기

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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Construct 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