From: Yaser Sulaiman Date: 2010-06-25T03:24:55+09:00 Subject: Re: [QUIZ] Random Points within a Circle (#234) --0016368e2ac8d4e3940489caaac7 Content-Type: text/plain; charset=ISO-8859-1 2010/6/22 Benoit Daloze > I would say it is a good try, and the distinction Vector/Point is > interesting, while being a problem with DRY (you repeat the > calculation of the distance). > Thanks. I don't know how did I miss it, but it kinda suddenly hit me: I can use Point.distance_to instead of Vector.length. After this realization, I decided to drop the Vector class altogether - although I have to admit that talking about generating random vectors sounds "cooler" :P A quick tip a friend showed me: Math.sqrt(a**2 + b**2) => Math.hypot(a, b) > In the end, you manually add the offset to x and y. As you have > Vector/Point, the Point+Vector should be defined and then the 4 last > lines would be: "center + v" > Because I'm not using Vectors anymore, Point + Point now performs the equivalent 2D translation. Also, Point.distance_to now uses Math.hypot. On Wed, Jun 23, 2010 at 2:25 AM, Colin Bartlett wrote: > Yaser's solution is a "Monte Carlo" simulation? My initial reaction was > that > Yaser's method might not end up with the correct distribution, but on > further thought it (I think): > 1. Generates points uniformly in a square of side r. > 2. Redistributes the points uniformly in a square of side 2*r. (I wondered > what the "Flip coin to reflect" bits were doing until I realised that.) > 3. Ignores any points outside the circle. Which should leave points > uniformly distributed (by area) inside the circle. > So Yaser's solution does have the correct distribution of points. And shows > that sometimes a way to generate a correct distribution is to use an > incorrect distribution and then ignore some values generated from the > incorrect distribution. > I have modified the comments in the code to better reflect the used approach, which you have elaborate on here quite nicely. (Strictly speaking, is the test that "v.length > 0" necessary, and should > the other test be "v.length <= radius"? It won't make much difference > though.) > v.length > 0 was necessary because of the way I was using the until loop (after initializing v to a null vector, v.length is 0 and the second test, v.length < radius, evaluates to true). Anyways, I'm now using the begin-end-until loop with a single test. Regarding using < vs. <=, I think it depends on whether points on the circumference (perimeter) of the circle are considered to be "within" the circle or not. Check the updated code @ http://gist.github.com/447554. Thank you all for your valuable feedback! Regards, Yaser --0016368e2ac8d4e3940489caaac7--