1 view (last 30 days)

Show older comments

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;

[xx,yy]=meshgrid(x,y);

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!")

xxr=randperm(10,10);

yyr=randperm(10,10);

xxb=randperm(10,1);

yyb=randperm(10,1);

plot(xxr, yyr, 'o', 'MarkerSize', 15,'markeredgecolor','b','markerfacecolor','r');

plot(xxb, yyb, 'o', 'MarkerSize', 15,'markeredgecolor','r','markerfacecolor','k');

axis([-1,11,-1,11])

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
on 14 Jul 2021

Edited: David Hill
on 14 Jul 2021

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

r=r(1:10);

for k=1:100%create adjacency matrix

if ismember(k,r)

continue;

end

idx=[k-11,k-10,k-9,k-1,k+1,k+9,k+10,k+11];

idx=idx(idx>0&idx<101&~ismember(idx,r));

if mod(k,10)==1

idx=idx(mod(idx,10)~=0);

elseif mod(k,10)==0

idx=idx(mod(idx,10)~=1);

end

M(k,idx)=1;

end

d=graph(M);%create graph

s=shortestpath(d,10,e);%find shortest path from node 10 to node e

plot(d);

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

Start Hunting!