Filter löschen
Filter löschen

Generate a Random Point inside a box/plane

23 Ansichten (letzte 30 Tage)
Rounak Saha Niloy
Rounak Saha Niloy am 1 Feb. 2023
Beantwortet: Aritra am 1 Feb. 2023
  1. Let's say, I have coordinates of two points as (p,q) and (x,y). I want to generate a random point within the plane bounded by these two points. How do I do that?
  2. Simialrly, I have coordinates of two points in 3D-space as (p,q,r) and (x,y,z). I want to generate a random point within the box bounded by these two points. How do I do that?

Akzeptierte Antwort

Aritra
Aritra am 1 Feb. 2023
Hi,
As per my understanding you are trying to generate a random number within a box/plane.
Note, that the following method will work only for axis aligned boxes.
The key ideas involved are:
  1. Generate random x and y coordinates independently within the limits of the box (x_min, x_max) and (y_min and y_max).
  2. For generating a random number in the range [a,b], draw a random number in interval [0,1]. Map the interval [0,1] to the interval [a,b].
You can make use of the rand(n) function to generate a random number within [0,1].
Following example illustrates the process:
a = 50;
b = 100;
r = (b-a).*rand(1) + a;
The generated value of r always lies within [50,100].
You can generate a random x-coordinate within the range of [p,x] and a random y-coordinate within the range of [q,y]. The randomly generated point will always lie within the box. For 3D space you can similarly generate the z-coordinate.
For non-aligned other boxes:
  1. Perform inverse rotations such that the box is axis aligned. Suppose box is rotated by theta, rotate points of box by –theta.
  2. Find the random point in the inverse rotated box.
  3. Rotate the random point by theta to map it to rotated(original) box.
For more details on random number generation within a specific range, refer to the following documentation:
https://in.mathworks.com/help/matlab/math/floating-point-numbers-within-specific-range.html

Weitere Antworten (1)

KSSV
KSSV am 1 Feb. 2023
A = [0 0] ; % (p,q)
B = [1 1] ; % (x,y)
a = 50;
b = 100;
r = (b-a).*rand(1000,1) + a;
x = (B(1)-A(1))*rand(100,1)+A(1) ;
y = (B(2)-A(2))*rand(100,1)+A(2) ;
box = [A ; B(1) A(2) ; B ; A(1) B(2)] ;
patch(box(:,1),box(:,2),'y')
hold on
plot(x,y,'.r')

Community Treasure Hunt

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

Start Hunting!

Translated by