DIvide an area into sectors
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2022-4-20
编辑:Image Analyst
2022-4-21
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]);

更多回答(1 个)
Matt J
2022-4-20
编辑:Matt J
2022-4-20
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 个评论
Matt J
2022-4-20
@Elysi Cochin It is definitely possible, but you are far from a Matlab novice. You should be able to do it.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!