skip to Main Content

Given:

  1. Rectangular region with length l & breadth b
  2. Circle with radius r
  3. Circle is contained in rectangular region like shown in below image

See image here

Then how to generate random coordinates outside the circle contained in a rectangular region and evenly distributed? (in blue region of below image)

2

Answers


  1. Generate two random values in ranges (uniform distribution in rectangle)

    a = Math.random() * width
    b = Math.random() * height
    

    check if point lies outside the circle:

    (a-center_x)*(a-center_x)+(b-center_y)*(b-center_y) > r*r
    

    if not – repeat random generation until condition becomes true (this is rejection method)

    Login or Signup to reply.
  2. First generate a uniform distribution of x, y values in a square and reject any x,y outside the centered rectangle.

    Algorithm

    max_dimension = max(l, b)
    min_x = (max_dimension-l)/2;
    max_x = l - min_x;
    min_y = (max_dimension-b)/2;
    max_y = b - min_y;
    loop forever
      x = Math.random()*max_dimension
      if (x < min_x || x > max_x) continue;
      y = Math.random()*max_dimension
      if (y < min_y || x > max_y) continue;
      
    

    Now test if x, y inside the circle

      xc = x - l/2;
      yc = y - b/2;
      if (xc*xc + yc*yc < r*r) continue
      break
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search