DIvide an area into sectors

10 visualizzazioni (ultimi 30 giorni)
Elysi Cochin
Elysi Cochin il 20 Apr 2022
Commentato: Elysi Cochin il 21 Apr 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 Commenti
Matt J
Matt J il 20 Apr 2022
Modificato: Matt J il 20 Apr 2022
In what form would the output be given? A label map of the sectors?
Elysi Cochin
Elysi Cochin il 20 Apr 2022
Sir I edited the question, I added few more details

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 20 Apr 2022
Modificato: Image Analyst il 21 Apr 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]);

Più risposte (1)

Matt J
Matt J il 20 Apr 2022
Modificato: Matt J il 20 Apr 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 Commenti
Elysi Cochin
Elysi Cochin il 20 Apr 2022
@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 il 20 Apr 2022
@Elysi Cochin It is definitely possible, but you are far from a Matlab novice. You should be able to do it.

Accedi per commentare.

Categorie

Scopri di più su Scripts in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by