MATLAB Answers

A simple rout algorithm in finding the way from point (0,0) to a random point (x1,y1) in a block

1 ビュー (過去 30 日間)
Farnaz Baya
Farnaz Baya 2021 年 7 月 14 日
編集済み: Farnaz Baya 2021 年 7 月 14 日
In the first step, I have to make the below shape! However, the program must generate black dots and red dots randomly. The number of red dots is always 10 and their coordinates are random, and of course only the correct values ​​can be between zero and 10, as well as the black dot, which is always a random dot with the same characteristic of red dots. The blue dot is always in the zero and zero coordinates. The starting point or origin is the lower left corner marked in blue. The destination point is marked in black, which in this example is in coordinates 5 and 7. Suppose that we can only move 1 unit in each step and we can only go to the destination through the green dots. The red dots are actually obstacles in the path that we must somehow get around.
The program is supposed to offer two routes:
1 shortest between origin and destination! -
2 paths that required the least number of turns.
Assumption: Consider the length of horizontal, vertical and oblique steps the same, and for the shortest path only the number of step is important.
my code so far:, but I cannot go more. since I should do it without using for and while loops. the figures I need to produce are attached to this question.
any help is really appreciated. I need a hint to do the rest.
Sincerely, Niza
clc;clear;close all;
x = 0:10;
y = 0:10;
plot(xx, yy, 'bo', 'MarkerSize', 15,'markeredgecolor','b','markerfacecolor','g');
hold on;
plot(xx(1,1),yy(1,1),'o', 'MarkerSize', 15,'markeredgecolor','r','markerfacecolor','b')
annotation('textbox', [0.32, 0.06, 0.1, 0.1], 'String', "start point!")
plot(xxr, yyr, 'o', 'MarkerSize', 15,'markeredgecolor','b','markerfacecolor','r');
plot(xxb, yyb, 'o', 'MarkerSize', 15,'markeredgecolor','r','markerfacecolor','k');
grid on;
axis square;
hold all
xx(1,1)=NaN; yy(1,1)=NaN;
D = sqrt((xx .^ 2) + (yy .^ 2));
% Find the closest one from (0,0)
[minDistance, indexOfMin] = min(D);
closestX = xx(indexOfMin);
closestY = yy(indexOfMin);
% Mark it with red *
hold on; % Don't blow away existing points.
plot(closestX, closestY, 'r*', 'MarkerSize', 8, 'LineWidth', 2);
% Draw a line from the closest point to (0, 0)
line([0, closestX], [0, closestY], 'LineWidth', 2, 'Color', 'r');


David Hill
David Hill 2021 年 7 月 14 日
編集済み: David Hill 2021 年 7 月 14 日
I would use a graph.
M=zeros(100);%adjacency matrix start
r=randperm(100,11);%ten random nodes with last node the end point
e=r(11);%end node
for k=1:100%create adjacency matrix
if ismember(k,r)
if mod(k,10)==1
elseif mod(k,10)==0
d=graph(M);%create graph
s=shortestpath(d,10,e);%find shortest path from node 10 to node e
  4 件のコメント
Farnaz Baya
Farnaz Baya 2021 年 7 月 14 日
@John D'Errico no not even a graph :) yeah it is a little bit hard to deal with this way but the way that I should move between points is one step at a time and then reach the black point. graph is a good Idea but the code here which @David Hill suggested me to do doesnt help me in this regard since it is not just about the shortest way it is also about the way with the least detour.


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by