Reputation: 33
I'm trying to randomly create 100 points from 0 - 180, then graph them. Then check the distance between them and if it falls under a certain threshold draw a line between them. I can get the points graphed, and even some lines. But it's not right, some points that are close enough aren't mapped, and some points that are further are mapped.
clc; clear all; format compact;
a = [2 3]
b = [16 50]
array = randi(180,100,1);
array2 = randi(180,100,1);
% x = array(:, 1);
% y = array(:,2);
plot(array,array2, '.')
line (a,b)
% radius is 18, which is given k * given d
for j = 1:100
for i = 1:100
d = sqrt((array(j)-array(i))^2+(array2(j)-array2(i))^2);
if d <= 18
point1 = [array(j), array2(j)];
point2 = [array(i), array2(i)];
line( point1, point2)
end
end
end
Sample output:
Upvotes: 0
Views: 36
Reputation: 60574
I see the issue now. line
doesn't draw a line from one point to the next, it draws a line through given x and y coordinates, much like plot
. Replace this bit:
point1 = [array(j), array2(j)];
point2 = [array(i), array2(i)];
line( point1, point2)
with this:
x = [array(i), array(j)];
y = [array2(i), array2(j)];
line(x, y);
or equivalently:
line(array([i,j]),array2([i,j]));
Unsolicited advice:
Use more sensical names than array
and array2
. For example x
and y
. This bug would have been easy to spot with better names!
Upvotes: 1