user1707325
user1707325

Reputation: 1

How to get a random point inside a rectangle near the center?

I know how to get a random point within a rectangle, But know I need to know how to get a random point that will have more of a chance of getting selected if it is near the center. It should be some form of exponential equation, but I can not figure it out.

Does anyone have any ideas?

Upvotes: 0

Views: 223

Answers (3)

Ivan Kochurkin
Ivan Kochurkin

Reputation: 4481

Are you mean normal distribution over polar coordinates with center of the rectangle center?

Normal distribution

enter image description here

enter image description here

Upvotes: 0

Jim
Jim

Reputation: 879

It sounds like you are trying to calculate something like bullet-spread in a game.

Since you know the rectangle that you want all of the hits generated in, you also know the center of that rectangle.

I would recommend that you calculate a random value for both the horizontal and vertical offsets from this point. By applying a function to the random values you are getting, you can shape them to appear more clustered.

I don't know if performance is a tight concern for you, but the square root is one such function you could apply to cluster your values closer to zero. Other alternatives include curves such as y = ln(x).

Upvotes: 1

ddyer
ddyer

Reputation: 1788

consider your random values as polar coordinates, instead of cartesian.

Upvotes: 0

Related Questions