How to finds the intersection points between rectangle foot print and line segment?
2 views (last 30 days)
Show older comments
This is the Code.
N = 50; % Number of events
X = 50; Y = 50;
P1x = rand(N,1) * X;
P1y = rand(N,1) * Y ; % initial point
L = lognrnd(2,0.7,[N,1]); % Sample track length
Theta = mod(normrnd(90,15,[N,1]),360);
dx = L.*cos(deg2rad(Theta - 90));
dy = L.*sin(deg2rad(Theta - 270));
P2x = P1x + dx; P2y = P1y + dy; % Final point
plot([0 X X 0 0],[0 0 Y Y 0]); hold on
plot(P1x,P1y,'ro')
plot([P1x P2x]',[P1y P2y]','-')
xlabel('X [km]'); ylabel('Y [km]');
xlim([-X/4 1.25*X]); ylim([-Y/4 1.25*Y])
%% Draw rectangles
Vx = [P1x P2x]' ;
Vy = [P1y P2y]' ;
t = 1./2 ;
% Loop to get normals
for i = 1:size(Vx,2)
W=LineNormals2D([Vx(:,i) Vy(:,i)]') ;
C = [[Vx(:,i) Vy(:,i)]+t*W ;
[Vx(:,i) Vy(:,i)]-t*W] ;
idx = boundary(C(:,1),C(:,2)) ;
plot(C(idx,1),C(idx,2),'k')
end
% GEt the line
lx = [(X+Y)/2.25, (X+Y)/3, (X+Y)/3, (X+Y)/4] ;
ly = [0, (X+Y)/10, (X+Y)/2.5, (X+Y)/2];
plot(lx,ly,'b')
I need to make code in MATLAB for this. Many thanks !!
0 Comments
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!