How to I generate random numbers inside a rectangle at an angle?

 채택된 답변

Roger Stafford
Roger Stafford 2014년 3월 21일

2 개 추천

Let (x0,y0) be coordinates of one of the rectangle's corners, and let one of its sides stretching from this corner to another corner be of length a and making an angle of theta radians with respect to the x-axis. Let another side be of length b stretching from (x0,y0) at a counterclockwise right angle to the first side. Finally suppose you want n random points within that rectangle. Then do this:
X = a*rand(n,1);
Y = b*rand(n,1);
x = x0 + X*cos(theta) - Y*sin(theta);
y = y0 + X*sin(theta) + Y*cos(theta);
plot(x,y,'y.')
axis equal % <-- Keep the x and y axes scale factors equal

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Scatter Plots에 대해 자세히 알아보기

태그

질문:

2014년 3월 21일

답변:

2014년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by