Indexing the intersection of polygons.
Mostra commenti meno recenti
I have the following code that creates two polygons and then intersection. I want to do indexing such that I have the index of intersection points in any of the input polygons.
%%
clc
clear all
close all
%%
xlimit = [3 13];
ylimit = [2 8];
xbox = xlimit([1 1 2 2 1]);
ybox = ylimit([1 2 2 1 1]);
distancesA = [0; cumsum(sqrt(diff(xbox').^2 + diff(ybox').^2))];
numPointsA = distancesA(end)*100*2;
equalSpacedDistancesA = linspace(0, distancesA(end), numPointsA);
xEqualSpacedA = interp1(distancesA, xbox, equalSpacedDistancesA);
yEqualSpacedA = interp1(distancesA, ybox, equalSpacedDistancesA);
hold on
plot(xEqualSpacedA, yEqualSpacedA, 'DisplayName','poly1','Marker','.')
x = [0 6 4 8 8 10 14 10 14 4 4 6 9 15];
y = [4 6 10 11 7 6 10 10 6 0 3 4 3 6];
distancesB = [0; cumsum(sqrt(diff(x').^2 + diff(y').^2))];
numPointsB = distancesB(end)*100*2;
equalSpacedDistancesB = linspace(0, distancesB(end), numPointsB);
xEqualSpacedB = interp1(distancesB, x, equalSpacedDistancesB);
yEqualSpacedB = interp1(distancesB, y, equalSpacedDistancesB);
hold on
plot(xEqualSpacedB, yEqualSpacedB, 'DisplayName','poly2','Marker','.')
%
[xi,yi] = polyxpoly(xEqualSpacedA,yEqualSpacedA,xEqualSpacedB,yEqualSpacedB);
scatter(xi,yi,'Marker','o')
Risposta accettata
Più risposte (1)
KALYAN ACHARJYA
il 25 Dic 2024
Modificato: KALYAN ACHARJYA
il 25 Dic 2024
Other way:
xlimit = [3 13]; ylimit = [2 8]; xbox = xlimit([1 1 2 2 1]); ybox = ylimit([1 2 2 1 1]);
distancesA = [0; cumsum(sqrt(diff(xbox').^2 + diff(ybox').^2))];
numPointsA = distancesA(end)*100*2;
equalSpacedDistancesA = linspace(0, distancesA(end), numPointsA);
xEqualSpacedA = interp1(distancesA, xbox, equalSpacedDistancesA);
yEqualSpacedA = interp1(distancesA, ybox, equalSpacedDistancesA);
plot(xEqualSpacedA, yEqualSpacedA, 'DisplayName','poly1','Marker','.')
hold on;
x = [0 6 4 8 8 10 14 10 14 4 4 6 9 15];
y = [4 6 10 11 7 6 10 10 6 0 3 4 3 6];
distancesB = [0; cumsum(sqrt(diff(x').^2 + diff(y').^2))];
numPointsB = distancesB(end)*100*2;
equalSpacedDistancesB = linspace(0, distancesB(end), numPointsB);
xEqualSpacedB = interp1(distancesB, x, equalSpacedDistancesB);
yEqualSpacedB = interp1(distancesB, y, equalSpacedDistancesB);
plot(xEqualSpacedB, yEqualSpacedB, 'DisplayName','poly2','Marker','.')
% [xi,yi] = polyxpoly(xEqualSpacedA,yEqualSpacedA,xEqualSpacedB,yEqualSpacedB); scatter(xi,yi,'Marker','o')
%%
% Find intersection points
[xi, yi] = polyxpoly(xEqualSpacedA, yEqualSpacedA, xEqualSpacedB, yEqualSpacedB);
distancesA = sqrt((xEqualSpacedA(:) - xi').^2 + (yEqualSpacedA(:) - yi').^2); % Ensure column vectors
[~, indicesA] = min(distancesA, [], 1); % Find closest points
distancesB = sqrt((xEqualSpacedB(:) - xi').^2 + (yEqualSpacedB(:) - yi').^2); % Ensure column vectors
[~, indicesB] = min(distancesB, [], 1); % Find closest points
% Make sure that the intersection points and their indices match in size
numIntersections = length(xi);
indicesA = indicesA(:);
indicesB = indicesB(:);
% This happen depending on data: If there are fewer indices than intersections, pad with NaN
if length(indicesA) < numIntersections
indicesA = [indicesA; NaN(numIntersections - length(indicesA), 1)];
end
if length(indicesB) < numIntersections
indicesB = [indicesB; NaN(numIntersections - length(indicesB), 1)];
end
% Display the results (If it Required, otherwise ignore it)
disp('Indices of intersection points:');
disp(table(xi, yi, indicesA, indicesB, 'VariableNames', {'X_Intersect', 'Y_Intersect', 'Index_A', 'Index_B'}));
% Show intersection points
scatter(xi, yi, 50,'MarkerEdgeColor',[0 .5 .5],'MarkerFaceColor',[0 .7 .7],'LineWidth',1.5);

Categorie
Scopri di più su Interpolation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
