calculate distance and angles between points in a grid

조회 수: 13 (최근 30일)
ELISABETTA BILLOTTA
ELISABETTA BILLOTTA 2021년 9월 3일
편집: darova 2021년 9월 8일
I have created a grid of this type where for each point I know the coordinates and each point of the grid is one degree both in latitude and in longitude. I therefore have to calculate the distance in km and the angle in degrees from the blue point (x = 14.75: y = 40.5) to all the other orange points.
I created a function to calculate the distances and angles between two points, but where I always have to enter the coordinate values and I would like to avoid this step. I wonder, is it possible to calculate the distances and angles for all these points without manually entering the coordinate values?
  댓글 수: 3
Drishan Poovaya
Drishan Poovaya 2021년 9월 6일
It would be helpful if you could share your function which you have created, as well as data of the orange grid you have created. It would then be possible to vectorize or maybe loop the function to calculate all the distances and angles
ELISABETTA BILLOTTA
ELISABETTA BILLOTTA 2021년 9월 6일
편집: darova 2021년 9월 8일
this is the code:
A=load('plot_mappe_jacopo.txt'); %plottare i confini dei continenti!!!
plot(A(:,1),A(:,2));
hold on
clear;
cx = 14.75; % coordinata x punto centrale, x-coordinates = 50 (example)
cy = 40.5; % coordinata y punto centrale, y-coordinates = 25 (example)
plot(cx,cy,'bo'); % Your center point
hold on
longrd=0:1:30; %dimensione griglia
latgrd=25:1:55;
ic=0; %contatore
for i=1:length(latgrd) %matrice che contiene tutti i valori
for j=1:length(latgrd)
ic=ic+1;
lonlatgrd(ic,1)=longrd(i);
lonlatgrd(ic,2)=latgrd(j);
end
end
%figure()
plot(lonlatgrd(:,1),lonlatgrd(:,2),'.');

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

채택된 답변

Drishan Poovaya
Drishan Poovaya 2021년 9월 7일
Based on the inromation you have provided, the below code should calculate the distance and angle for all 961 coordinates from cx and cy. I have made some small changes to your original code as well, it should run faster now that I have eliminated the for loop
cx = 14.75; % coordinata x punto centrale, x-coordinates = 50 (example)
cy = 40.5; % coordinata y punto centrale, y-coordinates = 25 (example)
plot(cx,cy,'bo'); % Your center point
hold on
longrd=0:1:30; %dimensione griglia
latgrd=25:1:55;
[X ,Y] = ndgrid(longrd,latgrd);
X = reshape(X.',1,[]);
Y = reshape(Y.',1,[]);
lonlatgrd(:,1) = X';
lonlatgrd(:,2) = Y'
%figure()
plot(lonlatgrd(:,1),lonlatgrd(:,2),'.');
%sqrt((x-x1)^2 + (y-y1)^2)
D1 = X - cx;
D2 = Y - cy;
D1 = D1.*D1;
D2 = D2.*D2;
distance = sqrt(D1+D2);
%angle = tan(y-y1/x-x1);
D1 = X - cx;
D2 = Y - cy;
angle = tan(D2./D1);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Geographic Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by