Subclass with dynamic method

37 次查看(过去 30 天)
Hi Everyone,
I'm trying to implement an OOP code with a superclass and subclass. In order to simplify my question, I created a simple case where the superclass is the class of polygons and the subclass is the class of rectangles (I used here the name quadrangular in order to avoid problems with matlab built-in function). I attach a possible simple implementation of these classes.
I have a method in the polyhedron class that is complex to evaluate, in the case attached, let's say it is the point-in-polygon test. Of course, this check is trivial for a rectangle, so I overload the method when the object belong to this subclass.
% create rectangle
p = quadrangular([0 0; 1 0; 1 1; 0 1]);
% random points
Q = 2*rand(100,2);
% check in rectangle
in = inside(p,Q);
% plot
figure, hold on
plot(p)
plot(Q(in,1),Q(in,2),'b*');
plot(Q(~in,1),Q(~in,2),'ro');
So far so good...
Now I would like to create another method in the superclass to join two objects. Suppose I can do all the operations, and create a new polygon that is the boolean union of two inputs, considering only the boundary nodes, etc. In the general case, the point-in-polygon test should work (I know this function is for closed convex polygons, but this is only an oversimplified version of this problem).
Question: In case of the union of two rectangles, it would be smart to exploit the knowledge of the genesis of the polygon, and check if a point is inside the new polygon with a function that checks if the point is inside the first rectangle OR inside the second, instead of relying on the general purpose point-in-polygon test. Hower I don't know how to implement such function. I tried to create a new property that is a handle to an anonymous function, but this approach seems not to work. Any suggestion on how to implement a feature like this?
Note: I oversimplified the problem to have a case that is easily understandable and that produces a simple code (attached). For this reason, I'm not interested in fast point-in-polygon test. My question is syntax related.
Thanky you in advance for any help or suggestion

采纳的回答

埃博拉酱
埃博拉酱 2024-11-2,14:28
编辑:埃博拉酱 2024-11-2,14:45
classdef Combination<polygon
properties(SetAccess=immutable)
PolygonA
PolygonB
AIsQuadrangular
BIsQuadrangular
end
methods(Access=protected)
function obj=Combination(PolygonA,PolygonB)
[Vertices,Edges]=YourMethodToCalculateVerticesAndEdges(PolygonA,PolygonB);
obj@polygon(Vertices,Edges);
obj.PolygonA=PolygonA;
obj.PolygonB=PolygonB;
obj.AIsQuadrangular=obj.PolygonA.shape=="quadrangular";
obj.BIsQuadrangular=obj.PolygonB.shape=="quadrangular";
end
end
methods
function in = inside(p,Q)
if p.AIsQuadrangular
in=p.PolygonA.inside(Q);
if p.BIsQuadrangular
in(~in)=p.PolygonB.inside(Q(~in,:));
else
in(~in)=inside@polygon(Q(~in,:));
end
elseif p.BIsQuadrangular
in=p.PolygonB.inside(Q);
in(~in)=inside@polygon(Q(~in,:));
else
in=inside@polygon(Q);
end
end
end
end
  3 个评论
Fabio Freschi
Fabio Freschi 2024-11-2,16:24
I worked on the proposed solution and with few modifications I got what I wanted
classdef polyadd < polygon
properties(SetAccess=immutable)
PolygonA
PolygonB
end
methods(Access=protected)
function obj = polyadd(PolygonA,PolygonB)
[Vertices,Edges] = MyMethodToCalculateVerticesAndEdges(PolygonA,PolygonB);
obj@polygon(Vertices,Edges);
obj.PolygonA=PolygonA;
obj.PolygonB=PolygonB;
obj.shape = 'polyadd';
end
end
methods
function in = inside(p,Q)
% the choice is automatic here
in = inSolid(p.p1,QLoc) | inSolid(p.p2,QLoc);
end
end
end
and in the methods of class polygon I added
% addition function
function p = plus(p1,p2)
p = polyadd(p1,p2);
end
Still testing, but it seems I can also nest different objects
Thank you again 埃博拉酱 (unfortunately I can't spell your name!)
埃博拉酱
埃博拉酱 2024-11-3,4:08
If you don't want to extend the polygon class, you can only add new properties to the polygon and save the original graphics before the combination in the polygon.
The combination of graphs is an operation in which information is lost. If you want to take advantage of the original graphics, you'll have to save the information in some form anyway. Whether it's a polygon property or a new subclass, it's the same purpose. But in terms of OOP design philosophy, I think it's more elegant to extend to a new class than to add properties (that might not be used in cases other than combination) to polygon.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by