Random points between lines

6 views (last 30 days)
utsav kakkad
utsav kakkad on 3 Mar 2019
Commented: utsav kakkad on 12 Mar 2019
Can anyone tell me how can I go about plotting random points between two straight lines?

Accepted Answer

Image Analyst
Image Analyst on 3 Mar 2019
Try this, adapting as needed for your particular equations of lines:
% Create x axis:
x = linspace(-10, 10, 1000);
% Create line 1
m1 = 0.13;
b1 = 0.5;
y1 = m1 .* x + b1;
plot(x, y1, 'b-', 'LineWIdth', 2);
grid on;
% Create line 2
m2 = 0.5;
b2 = 1;
y2 = m2 .* x + b2;
hold on;
plot(x, y2, 'b-', 'LineWIdth', 2);
grid on;
% Get random points along x axis.
numRandomPoints = 100;
randomXValues = min(x) + (max(x)-min(x)) * rand(1, numRandomPoints);
% Get y1 and y2 at those x values
y1r = interp1(x, y1, randomXValues);
y2r = interp1(x, y2, randomXValues);
% Construct random y values for each random x value.
upperValues = max([y1r; y2r], [], 1); % Upper boundary in y direction.
lowerValues = min([y1r; y2r], [], 1); % Lower boundary in y direction.
yr = lowerValues + (upperValues - lowerValues) .* rand(1, numRandomPoints);
% All done! Now just simply plot the random points.
plot(randomXValues, yr, 'r.', 'MarkerSize', 12);
xlabel('x', 'FontSize', 15);
ylabel('y', 'FontSize', 15);
0000 Screenshot.png

More Answers (5)

utsav kakkad
utsav kakkad on 4 Mar 2019
Hey,
Your plot is great & kinda helps me.
I need a setup like this:
topology.JPG
How to go about it?
  1 Comment
Image Analyst
Image Analyst on 4 Mar 2019
Set b = 0, then have the slopes m be arctand(0), arctand(15), arctand(30), arctan(45), arctan(60), arctand(75), and then you'll have to handle the 90 degree situation specially. Not hard - you're a smart engineer so I'm sure you can handle it.

Sign in to comment.


utsav kakkad
utsav kakkad on 7 Mar 2019
so the intercept in the slope intercept equation is what you are refering to right?
  3 Comments
utsav kakkad
utsav kakkad on 7 Mar 2019
Edited: Image Analyst on 9 Mar 2019
that is what I was asking
%create the x axis
x=linspace(0,40,1000);
%creating line 1
m1=tand(0);
y1=m1*x;
plot(x,y1);
grid on;
%slope of the lines will be
%arctand(0),arctand(15),arctand(30),arctand(45),arctand(60),arctand(75)
%line 2
m2=tand(15);
y2=m2*x;
plot(x,y2);
grid on;
%line 3
m3=tand(30);
y3=m3*x;
plot(x,y3);
grid on;
%line 4
m4=tand(45);
y4=m4*x;
plot(x,y4);
grid on;
%line 5
m5=tand(60);
y5=m5*x;
plot(x,y5);
%line 6
m6=tand(75);
y6=m6*x;
plot(x,y6);
%generate random X axis values
numRand=25;
randXvals=min(x)+(max(x)-min(x))*rand(1,numRand);
%getting y vals at these x values
y1r=interp1(x,y1,randXvals);
y2r=interp1(x,y2,randXvals);
y3r=interp1(x,y3,randXvals);
y4r=interp1(x,y4,randXvals);
y5r=interp1(x,y5,randXvals);
y6r=interp1(x,y6,randXvals);
%constructing random y values for x values
uv1=max([y1r; y2r],[],1);%upper boundary for the first division in y axis
lv1=min([y1r; y2r],[],1);%lower boundary for the first division in y axis
%second division
uv2=max([y2r; y3r],[],1);%upper boundary in y axis
lv2=min([y2r; y3r],[],1);%lower boundary in y axis
%third division
uv3=max([y3r; y4r], [],1);%upper bound in y axis
lv3=min([y3r; y4r], [],1);%lower bound in y axis
%4th div
uv4=max([y4r; y5r], [],1);%upper bound in y axis
lv4=min([y4r; y5r], [],1);%lower bound in y axis
%constructing random y values
yr1=lv1+(uv1-lv1).*rand(1,numRand);
yr2=lv2+(uv2-lv2).*rand(1,numRand);
yr3=lv3+(uv3-lv3).*rand(1,numRand);
yr4=lv4+(uv4-lv4).*rand(1,numRand);
%plotting the WSN topology thus created
plot(numRand,yr1,'r.');
plot(numRand,yr2,'g.');
plot(numRand,yr3,'c.');
plot(numRand,yr4,'m.');
what is the error here ? can anyone of you please point it out ?
btw I haven't used .* in the equations that should be y=m.*x as matlab was giving the error message "Dot indexing is not supported for variables of this type."
Image Analyst
Image Analyst on 9 Mar 2019
When I run your code I don't get an error - I get this plot:
0000 Screenshot.png

Sign in to comment.


utsav kakkad
utsav kakkad on 10 Mar 2019
Edited: utsav kakkad on 10 Mar 2019
Okay, now I am getting that as an output. However, how to make it distributed between the lines?
Now, I know that I can plot lines using the matlab line function.
However, I need to distribute the points between the lines which is not yet occuring. I need a topology like this for my wsn not what I am getting:-
  1 Comment
Image Analyst
Image Analyst on 10 Mar 2019
Not sure what form you're looking for but this just looks like uniformly distributed points with lines drawn through them every 15 degrees. So you can make data like this
x = rand(1, 58);
y = rand(1, 58);
Then you can make 7 lines knowing the slopes of the lines
angles = [0, 15, 30, 45, 60, 75, 90]
slopes = atand(angles);
What more do you want, if anything?

Sign in to comment.


utsav kakkad
utsav kakkad on 10 Mar 2019
Edited: utsav kakkad on 10 Mar 2019
I want to distribute points uniformly throughout this figure.
Between two lines there should be around 20 points.
Now, how do I plot points between two lines?

Image Analyst
Image Analyst on 10 Mar 2019
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
numPointsPerSector = 125;
numTrialPoints = numPointsPerSector * 6 * 10000; % Way more than enough.
% Define box
xMax = 10;
yMax = 10;
x = xMax * rand(1, numTrialPoints);
y = yMax * rand(1, numTrialPoints);
% Get angles for each point
angles = atan2d(y, x);
sectorAngleBoundaries = [0, 15, 30, 45, 60, 75, 90];
for k = 2 : length(sectorAngleBoundaries)
% Get acceptable indexes
mask = find(angles >= sectorAngleBoundaries(k-1) & angles <= sectorAngleBoundaries(k) & ...
x < xMax & y < yMax);
% Extract the prescribed number out of the masked array.
okIndexes = mask(1:numPointsPerSector);
% Store x and y into array.
thisX = x(okIndexes(1:numPointsPerSector));
thisY = y(okIndexes(1:numPointsPerSector));
plot(thisX, thisY, '.', 'MarkerSize', 13);
hold on;
% Draw lines
xLine1 = 2 * xMax * cosd(sectorAngleBoundaries(k-1));
xLine2 = 2 * xMax * cosd(sectorAngleBoundaries(k));
yLine1 = 2 * yMax * sind(sectorAngleBoundaries(k-1));
yLine2 = 2 * yMax * sind(sectorAngleBoundaries(k));
line([0, xLine1], [0, yLine1], 'Color', 'k');
line([0, xLine2], [0, yLine2], 'Color', 'k');
end
grid on;
xlim([0, xMax]);
ylim([0, yMax]);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
caption = sprintf('%d points per sector', numPointsPerSector);
title(caption, 'FontSize', fontSize);
axis square;
0000 Screenshot.png

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!