Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

random numbers inside a rectangle at an angle

Asked by VASISTAKRISHNA on 21 Mar 2014
Latest activity Answered by Roger Stafford on 21 Mar 2014

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

1 Comment

Azzi Abdelmalek on 21 Mar 2014

What angle?

VASISTAKRISHNA

Tags

Products

No products are associated with this question.

1 Answer

Answer by Roger Stafford on 21 Mar 2014
Accepted answer

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 Comments

Roger Stafford

Contact us