###

» Sat Feb 18, 2012 2:34 am

newt's idea is to sample a value in an unrotated rectangle and then rotate the values arround the center.

Let see then :

sample values inside a rectangle :

[code]x = rectangle.x+random(-rectangle.Width/2,rectangle.Width/2)

y = rectangle.y+random(-rectangle.Height/2,rectangle.Height/2)[/code]

Then you rotate the value around the center

[code]xRot = rectangle.x + cos(angle(rectangle.x,rectangle.y,xSample,ySample)+rectangle.angle)*distance(rectangle.x,rectangle.y,xSample,ySample)

yRot = rectangle.y + sin(angle(rectangle.x,rectangle.y,xSample,ySample)+rectangle.angle)*distance(rectangle.x,rectangle.y,xSample,ySample)[/code]

We can simplify that by sampling the rectangle at 0,0 and then offsetting the restult

[code]x0 = random(-rectangle.Width/2,rectangle.Width/2)

y0 = random(-rectangle.Height/2,rectangle.Height/2)[/code]

And then

[code]xRot = rectangle.x + cos(angle(0,0,x0Sample,y0Sample)+rectangle.angle)*distance(0,0,x0Sample,y0Sample)

yRot = rectangle.y + sin(angle(0,0,x0Sample,y0Sample)+rectangle.angle)*distance(0,0,x0Sample,y0Sample)[/code]

So basically, sampling a random number in a rectangle is the same as chosing a random angle and distance from the center of this rectangle and applying a rotation.

In short it will be something like that:

[code]+On what you want

Local number randX=0 //random X position withing the rectangle

Local number randY=0 //random Y position withing the rectangle

Local number randA=0 //random angle before rotation

Local number randD=0 //random distance

-> System: set randX to random(-rectangle.Width/2,rectangle.Width/2)

-> System: set randY to random(-rectangle.Height/2,rectangle.Height/2)

-> System: set randA to angle(0,0,randX,randY)

-> System: set randD to distance(0,0,randX,randY)

-> System: set randX to rectangle.x + cos(randA + rectangle.angle) * randD

-> System: set randY to rectangle.y + sin(randA + rectangle.angle) * randD[/code]

Should work... Yann2012-02-18 02:37:36