![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206638/0000%20Screenshot.png)
Random points between lines
조회 수: 2 (최근 30일)
이전 댓글 표시
Can anyone tell me how can I go about plotting random points between two straight lines?
댓글 수: 0
채택된 답변
Image Analyst
2019년 3월 3일
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206638/0000%20Screenshot.png)
댓글 수: 0
추가 답변 (5개)
utsav kakkad
2019년 3월 4일
댓글 수: 1
Image Analyst
2019년 3월 4일
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.
utsav kakkad
2019년 3월 10일
편집: utsav kakkad
2019년 3월 10일
댓글 수: 1
Image Analyst
2019년 3월 10일
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?
Image Analyst
2019년 3월 10일
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207751/0000%20Screenshot.png)
Probably a better way is to adapt the code in the FAQ: https://matlab.fandom.com/wiki/FAQ#How_do_I_create_a_set_of_random_locations_within_a_circle.3F
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!