On Mon, Jun 21, 2010 at 7:12 PM, Dave Howell
<groups.2009a / grandfenwick.net> wrote:
> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>> On 6/19/10, Daniel Moore <yahivin / gmail.com> wrote:
>>> The quiz this week
>>> is to generate random points uniformly distributed within a circle of
>>> a given radius and position.
>>
>> def random_point_in_a_circle(x,y,r)
>> =A0angle=3Drand*2*Math::PI
>> =A0r*=3Drand
>> =A0x+=3Dr*Math.sin(angle)
>> =A0y+=3Dr*Math.cos(angle)
>>
>> =A0return x,y
>> end
>>
>> 7 lines, 5 minutes, 0 tests or visualizations. Super-easy.
>
> And wrong, unfortunately. You're selecting a random angle, and then a ran=
dom distance from the center. This will result in way too many points at th=
e center of the circle.

I whipped up a quick little visualization:

def random_point_in_a_circle(x,y,r)
  angle=3Drand*2*Math::PI
  r*=3Drand
  x+=3Dr*Math.sin(angle)
  y+=3Dr*Math.cos(angle)

  return x,y
end

require 'java'
JFrame =3D javax.swing.JFrame
JPanel =3D javax.swing.JPanel

frame =3D JFrame.new("Random Points within a Circle")
frame.default_close_operation =3D JFrame::EXIT_ON_CLOSE
frame.set_size(400, 400)
frame.show

class RPwiaC < JPanel
  def paintComponent(graphics)
    super(graphics)

    10000.times do
      x,y =3D random_point_in_a_circle(200,200,150)
      graphics.draw_line(x-1,y-1,x+1,y+1)
      graphics.draw_line(x-1,y+1,x+1,y-1)
    end
  end
end

panel =3D RPwiaC.new
frame.add(panel)
panel.repaint
panel.revalidate