フィルターのクリア

Generate a Random Point inside a box/plane

18 ビュー (過去 30 日間)
Rounak Saha Niloy
Rounak Saha Niloy 2023 年 2 月 1 日
回答済み: Aritra 2023 年 2 月 1 日
  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?

採用された回答

Aritra
Aritra 2023 年 2 月 1 日
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

その他の回答 (1 件)

KSSV
KSSV 2023 年 2 月 1 日
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')

カテゴリ

Help Center および File ExchangeSmoothing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by