Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab question 1. Create the USA border and 100 cities inside the border includ

ID: 2083214 • Letter: M

Question

Matlab question

1. Create the USA border and 100 cities inside the border including a hub city

a) Write a function M-file cities.m that creates the location of cities inside a given border. It has three inputs, N , border and hub. N is a positive integer bigger than 0, that represents the desired number cities to be created. The input variable border is a matrix of dimension nx2. Every row of border represents the x and y coordinates of points located along the border of a polygon shape (with 3 points it is a triangular shape, with more it can be any irregular polygon shape in general). The input hub is a row vector (1x2) that gives the x and y coordinates of the hub city.

The location of the hub city is stored as the first row of the (N+1)x 2 output matrix P. Next randomly create N cities inside the border. Every city is represented by its x coordinate (a randomly generated real number in the range of x coordinates of border) and its y coordinate (another randomly generated real number in the range of y coordinates of border), such that it is located inside the polygon defined by border (hint: use built-in function inpolygon to check if a point is inside the polygon or not). The location of the i- th city is stored as the i-th row of the matrix, P.

Function cities.m:
Inputs: N, a positive integer bigger than 0,

border, annx2matrix hub, a 1x2 row vector

Output: P, an (N+1)x2 matrix, whose i-th row is (x,y coordinates) of the i-th city Example:

Input restrictions:
The function should halt and return a red error message for the following conditions. • The input N is not a positive integer bigger than 0
• The number of columns of input border is not 2
• The input hub is not a 1x2 row vector

Explanation / Answer

close all,

clear all,

clc,

N = 100; % No. of Cities

Border = [];% Border Points of size Nx2

hub = []; % Input Hub of 1x2 size

for r=1:N

Border(r,1)=0;

Border(r,2)=0;

end

ProjectPath = pwd;

ImagePath = strcat(ProjectPath,'.jpg');

figure, imshow(ImagePath);

h = impoly(gca, [20,50; 220,50;220,250;20,250]); %Draw Triangle

xv = uint16([20,220,220,20]);

yv = uint16([50,50,250,250]);

temp_X = 0;

temp_Y = 0;

r=1;

while(r < N),

temp_X = double(rand*1000); temp_X = uint16(temp_X);

temp_Y = double(rand*1000); temp_Y = uint16(temp_Y);

if(inpolygon(temp_X,temp_Y,xv,yv)==1),

inpolygon(temp_X,temp_Y,xv,yv);

rectangle('Position',[temp_X temp_Y 5 5]);

Border(r,1) = temp_X;

Border(r,2) = temp_Y;

r=r+1;

end

end

hub = [2,3];

P = cities(0,Border,hub)

function [P]=cities(N,Border,hub)

if(N <= 0),

disp('N is not an integer or less than zero');

else

P = [];

P(1,1) = hub(1,1);

P(1,2) = hub(1,2);

for i=1:N

P(i+1,1) = Border(i,1);

P(i+1,2) = Border(i,2);

end

end

end