MATLAB Answers

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

57 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?


Sign in to comment.

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
titles = [...
"All Lines";...
"Partially Within";...
"Exclusively Within"];
for i = 1:3
hold on
xlim([0 1])
ylim([0 1])
% 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
% plot lines that are partially within the boundary
if isempty(in)==0
% plot lines that are exclusively within the boundary
if isempty(out)==1
Here is the output:


Stephen Cobeldick
Stephen Cobeldick 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)

Community Treasure Hunt

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

Start Hunting!