Subclass with dynamic method
3 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
埃博拉酱
on 2 Nov 2024
Edited: 埃博拉酱
on 2 Nov 2024
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 Comments
埃博拉酱
on 3 Nov 2024
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.
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!