![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1463057/image.png)
Searching best way to do trajectory planning 2D on a GRID
4 ビュー (過去 30 日間)
古いコメントを表示
Hello everybody,
I'm searching the best way to do this job on Matlab :
I have a grid like in the picture with some points , that can be various for each case, and i want to draw some lines that go accross this point specifying the start point and the end point.
The rules are that the line must have the same length and that one line must not overlap the others most important no points without passage of the line.
The line can turn only in the two columns of both side of the grid
The lines can be one or more like a maximum of 4.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1404674/image.png)
( in the picture a wrong example because the line doesn t touch all the point
I have some skill in Matlab but I don't know the better and more efficiently way to do this .
Thanks In advance
0 件のコメント
回答 (1 件)
Moksh
2023 年 8 月 23 日
Hi Davide,
As per my understanding you are asking for a grid design with the required functionality where you can implement the above stated rules and plot lines more efficiently.
You can use the following MATLAB code for designing a grid and call the ‘plotLine’ function to plot a line between the 2 specified grid cells. The function takes an argument of 2 grid cell numbers and the color for the line. The code below calculates the midpoint coordinates of the 2 cells and plots a line segment between them in the specified colour. Assume the numbering from bottom left cell to the top right cell in an increasing fashion. Code along with a sample run is attatched below, and the cell numbers are also scribled for reference.
% Define grid size
n = 4;
m = 4;
figure;
% Plotting the specified sized grid on figure
for ix = 1:n
for iy = 1:m
r(ix,iy) = rectangle('Position', [ix,iy,1,1]);
end
end
% Your Logic to begin from start point till end point goes here
plotLine(1, 2, m, "red");
plotLine(2, 6, m, "red");
plotLine(6, 3, m, "red");
% Function to get the middle coordinates of the given rectangle number
function [x, y] = getCoordinates(rectangleNumber, m)
x = mod(rectangleNumber - 1, m) + 1.5;
y = floor((rectangleNumber - 1) / m) + 1.5;
end
% Function to plot the line between Rectangle number p1 and p2. You can also specify the color you want to give to that line
function plotLine(p1, p2, m, clr)
hold on;
[x1, y1] = getCoordinates(p1, m);
[x2, y2] = getCoordinates(p2, m);
A = [x1 x2];
B = [y1 y2];
plot(A,B,"-o","Color", clr,"LineWidth",3);
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1463057/image.png)
You can restrict the ‘plotLine’ function further by adding your rules using conditional statements and the ‘pdist2’ function in MATLAB to compute the Eucledian distance between 2 coordinates.
Documentation for the 'plot' and 'pdist2' function
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!