Matlab search if a graphic object exists in the figure at the given coordinates
I need to find a way to check if the patch object I created (like a rectangle) exists at the specific XY coordinates that I specify. I am using the following code as an example:
a = figure b = axes('Parent',a,'Xlim',[0 100],'Ylim',[0 100]) x = [0 10 10 0]; y = [0 0 10 10]; patch(x,y,'red')
Now I would like to know if there is an object in the picture at the point with coordinates x = 6 and y = 3. Is there a way to check this?
source to share
Use findall () and inpolygon function.
hPatches = findall(b, 'type', 'patch');
tgtX = 5; tgtY = 7;
inside = zeros (1, numel(hPatches));
for patchCtr = 1:numel(hPatches)
vert = get (hPatches(patchCtr), 'Vertices');
inside(patchCtr) = inpolygon (tgtX, tgtY, vert(:,1), vert(:,2));
end
source to share
I don't know if you are familiar with tools, but mpt-toolbox might come in handy here (from my Uni).
Once you have it set, you can define the rectangle as a polyhedron and just check if the point is inside the rectangle. An example of your code above:
Vertices = [0,0;10,0;10,10;0,10];
Rectangle = Polyhedron(Vertices);
TestPoint = [6;3];
Within = Rectangle.contains(Testpoint);
If Within
is a boolean variable (1 if point is inside Rectangle
, 0 otherwise)
EDIT
Of course the toolbar also works for intersections between your original polygon Rectangle
and says another polygon Intersect
.
source to share
You can use findobj to find objects of interest, in this case, patch objects and access the "XData" property, and then check if it falls within a range. You can do the same with the YData property.
Here's an example:
clc clear close all a=figure; b=axes('Parent',a,'Xlim',[0 30],'Ylim',[0 30]); x1 = [0 10 10 0]; y1 = [0 0 10 10]; x2 = [15 25 25 15]; y2= [10 10 20 20]; patch(x1,y1,'red') patch(x2,y2,'blue') hPatches = findall(a,'Type','patch') %// find patch objects InfoPatches = get(hPatches); %// Get info about the objects. Check for the XData property. XDataArray = zeros(4,numel(InfoPatches)); for k = 1:numel(InfoPatches) XDataArray(:,k) = InfoPatches(k).XData; %// Access the XData property, or any you want. end XDataArray
Picture:
And the XDataArray looks like this:
XDataArray =
15 0
25 10
25 10
15 0
Now there will be a part where you check if the object is in a specific position, but which is pretty easy to implement. Hope it helps!
source to share