필터 지우기
필터 지우기

DIvide an area into sectors

조회 수: 6 (최근 30일)
Elysi Cochin
Elysi Cochin 2022년 4월 20일
댓글: Elysi Cochin 2022년 4월 21일
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
Matt J
Matt J 2022년 4월 20일
편집: Matt J 2022년 4월 20일
In what form would the output be given? A label map of the sectors?
Elysi Cochin
Elysi Cochin 2022년 4월 20일
Sir I edited the question, I added few more details

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
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
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
Elysi Cochin
Elysi Cochin 2022년 4월 20일
@Matt J Sir is it possible to plot the center xy-coordinate also and the 8 sector lines as shown in the figure
Matt J
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 CenterFile Exchange에서 Scripts에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by