How to generate random points and place it within a rectangular/ square boundary?

2 views (last 30 days)
I have generated 'n' random points( Say n= 50) and created line segments. So I want to place these linesegments within a rectangular boundary and eliminate those which lie outside the domain and keep those which are partially inside the domain.
Could someone help me how to do the boundary conditions?

Accepted Answer

Joe Vinciguerra
Joe Vinciguerra on 3 Jul 2019
Check out the intersect function.
Here is one approach:
% generate a rectangular boundry
rectX = [0.1 0.1 0.9 0.9];
rectY = [0.1 0.5 0.5 0.1];
rect = polyshape(rectX,rectY);
% initialize a figure for display purposes
figure(1);clf
titles = [...
"All Lines";...
"Partially Within";...
"Exclusively Within"];
for i = 1:3
subplot(1,3,i)
hold on
title(titles(i))
plot(rect)
xlim([0 1])
ylim([0 1])
end
% define the number of line segments to generate
nLines = 25;
% initialize a structure to store the lines (if you need the data later)
lines = struct();
% generate each line, calculate it's intersection conditions, and plot
for i=1:nLines
% generate points for a random line segment
lines(i).points = rand(2);
% section the line into parts at intersection with the rectangle
[in, out] = intersect(rect, lines(i).points);
% plot all the lines that were generated
subplot(1,3,1)
line(lines(i).points(:,1),lines(i).points(:,2))
% plot lines that are partially within the boundary
if isempty(in)==0
subplot(1,3,2)
line(lines(i).points(:,1),lines(i).points(:,2))
end
% plot lines that are exclusively within the boundary
if isempty(out)==1
subplot(1,3,3)
line(lines(i).points(:,1),lines(i).points(:,2))
end
end
Here is the output:
RandLines.jpg
  2 Comments
Stephen23
Stephen23 on 6 Jul 2019
Mounisha Ganesan's "Answer" moved here:
Hi, Thank you for your instant help. I am trying to implement your code with my program.
I would need help regarding using periodic boundary conditions. Say I have random linesegments partially outside the boundary, so I need to bring the endpoints outside the boundary, at the same distance, to be on the inside of the boundary . Now How do I take those points and place it inside of the boundary.
I have an image attached here to show I want that linesegment to be. If you could help me with that,it would be nice
Joe Vinciguerra
Joe Vinciguerra on 8 Jul 2019
"I would need help regarding using periodic boundary conditions."
I'm not familiar with PBC's, so someone who is may be able to provide a more robust and elegant solution. I suggest you create a new question.
Conceptually, you could consider it as a rotation of point2 180° around point1, OR a vector move of point2 in the direction of the line by twice the magnitiude of the line, OR a mirror of point2 across the normal to the linesegment at point1.
However, while I don't know exactly what you are trying to do, I can imagine many cases that don't fit your example and would need additional consideration or explaination. If these are lines generated from random endpoints then there are many cases where the result of this additional transform will be a similar condition you started with: one point being outside the boundary. When you post your new question consider how you want that handled or provide additional informaiton, and provide a link back to this question.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!