DIvide an area into sectors
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Elysi Cochin
le 20 Avr 2022
Commenté : Elysi Cochin
le 21 Avr 2022
I have a xy-coordinate say (350,339), I wanted to divide the area of size 640x640 centered at the xy-coordinate into 8 sectors (angle of each sector is 45 degree)

Sir the above figure, shows what i want. The blue circle in the center is the xy-coordinate say (350,339).
I also have a set of xy-coordinates attached in the mat-file xyc.mat
I need to find the xy-coordinate closest to the center xy-coordinate in each sector and its distance
2 commentaires
Réponse acceptée
Image Analyst
le 20 Avr 2022
Modifié(e) : Image Analyst
le 21 Avr 2022
I think this will do it. The closest points in each sector have a dark green line drawn to them indicating where they are.
% Demo to find the closest point to a specified point, in each 45 degree sector.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
s = load('xyc.mat')
xyc = s.xyc;
x = xyc(:, 1);
y = xyc(:, 2);
plot(x, y, 'c.', 'MarkerSize', 20);
grid on;
xlabel('x', 'FontSize',fontSize)
ylabel('y', 'FontSize',fontSize)
% Put a red crosshairs at point defined to be the center.
xc = 350;
yc = 339;
hold on;
xline(xc, 'Color', 'r', 'LineWidth', 2);
yline(yc, 'Color', 'r', 'LineWidth', 2);
plot(xc, yc, 'b.', 'MarkerSize', 30);
% Get distances from all points to (xc, yc)
allDistances = sqrt((x-xc).^2 + (y-yc).^2);
maxDistance = max(allDistances) % Use as a radius for the sector dividing lines.
% Get angles of all points from the center
angles = atan2d(y-yc, x-xc);
% Plot lines dividing the sectors
sectorAngles = 0 : 45 : 359;
for k = 1 : length(sectorAngles)
thisAngle = sectorAngles(k);
x1 = xc + maxDistance * cosd(thisAngle);
y1 = yc + maxDistance * sind(thisAngle);
x2 = xc + maxDistance * cosd(180 + thisAngle);
y2 = yc + maxDistance * sind(180 + thisAngle);
line([x1, x2], [y1, y2], 'Color', 'r');
end
% angles goes from -180 to +180. Make it goe from 0 to 360.
angles(angles < 0) = angles(angles < 0) + 360;
% Loop over every sector in steps of 45 degrees.
darkGreen = [0, 0.6, 0];
for k = 1 : 8
angle1 = (k-1) * 45;
angle2 = angle1 + 45;
% Get points in this angle range.
indexes = angles >= angle1 & angles < angle2;
distances = allDistances .* indexes;
% Find out which is closest
minDistance = min(distances(distances > 0));
indexOfClosest = find(distances == minDistance);
% Draw a green line between the closest one and the (xc, yc) point.
xp = x(indexOfClosest);
yp = y(indexOfClosest);
line([xc, xp], [yc, yp], 'Color', darkGreen, 'LineWidth', 2);
end
xlim([0, 650]);
ylim([0, 650]);

Plus de réponses (1)
Matt J
le 20 Avr 2022
Modifié(e) : Matt J
le 20 Avr 2022
load xyc
x0=350; y0=339;
[X,Y]=deal(xyc(:,1), xyc(:,2));
theta=180/pi*cart2pol(X-x0,Y-y0);
theta(theta<0)=360+theta(theta<0);
[distancesAngular,imin]=min( abs(theta-linspace(45/2,360-45/2,8)) );
x=X(imin);
y=Y(imin);
scatter(X,Y); hold on
scatter(x,y,80,'filled'); hold off
axis equal
xlabel x, ylabel y
legend('Given Data','Nearest Points')
2 commentaires
Matt J
le 20 Avr 2022
@Elysi Cochin It is definitely possible, but you are far from a Matlab novice. You should be able to do it.
Voir également
Catégories
En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!