# [QUIZ] Random Points within a Circle (#234)

Discussion in 'Ruby' started by Daniel Moore, Jun 19, 2010.

1. ### Daniel MooreGuest

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this
quiz until 48 hours have elapsed from the time this message was
sent.

2. Support Ruby Quiz by submitting ideas and responses
as often as you can.

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem
the original quiz message, if you can.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Suggestions?: http://rubyquiz.strd6.com/suggestions

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Random Points within a Circle (#234)

Greetings Rubyists,

Generating random numbers is a useful feature and Ruby provides us
with `Kernel.rand` to generate numbers uniformly distributed between
zero and one. If we want to generate random numbers with other
distributions, then we'll need to do a little work. The quiz this week
is to generate random points uniformly distributed within a circle of

This quiz is relatively simple, great if you are new to Ruby or
strapped for time. Remember, it is contributions from people like
*you* that make Ruby Quiz succeed!

Have fun!

--
-Daniel
http://rubyquiz.strd6.com

Daniel Moore, Jun 19, 2010

2. ### Benoit DalozeGuest

Hi !

On 19 June 2010 20:05, Daniel Moore <> wrote:
> ## Random Points within a Circle (#234)
>
> Greetings Rubyists,
>
> Generating random numbers is a useful feature and Ruby provides us
> with `Kernel.rand` to generate numbers uniformly distributed between
> zero and one. If we want to generate random numbers with other
> distributions, then we'll need to do a little work. The quiz this week
> is to generate random points uniformly distributed within a circle of
> a given radius and position.
>
> This quiz is relatively simple, great if you are new to Ruby or
> strapped for time. Remember, it is contributions from people like
> *you* that make Ruby Quiz succeed!
>
> Have fun!
>
> -Daniel

So here is my solution in a hundred lines, with an ImageMagick visualisation:

http://gist.github.com/446707

I had an intuition doing some Math.sqrt about the distance, and it
revealed to be exact

Enjoy,
Benoit Daloze

Benoit Daloze, Jun 21, 2010

3. ### Caleb ClausenGuest

On 6/19/10, Daniel Moore <> 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)
angle=rand*2*Math:I
r*=rand
x+=r*Math.sin(angle)
y+=r*Math.cos(angle)

return x,y
end

7 lines, 5 minutes, 0 tests or visualizations. Super-easy.

Caleb Clausen, Jun 21, 2010
4. ### Dave HowellGuest

On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:

> On 6/19/10, Daniel Moore <> wrote:
>> The quiz this week
>> is to generate random points uniformly distributed within a circle of
>> a given radius and position.

>=20
> def random_point_in_a_circle(x,y,r)
> angle=3Drand*2*Math:I
> r*=3Drand
> x+=3Dr*Math.sin(angle)
> y+=3Dr*Math.cos(angle)
>=20
> return x,y
> end
>=20
> 7 lines, 5 minutes, 0 tests or visualizations. Super-easy.

And wrong, unfortunately. You're selecting a random angle, and then a =
random distance from the center. This will result in way too many points =
at the center of the circle.=20
=20

Dave Howell, Jun 22, 2010
5. ### Guest

On Mon, Jun 21, 2010 at 7:12 PM, Dave Howell
<> wrote:
> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>> On 6/19/10, Daniel Moore <> 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:I
>> =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:I
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
panel.repaint
panel.revalidate

, Jun 22, 2010
6. ### Guest

On Mon, Jun 21, 2010 at 9:12 PM, <> wrote:
> On Mon, Jun 21, 2010 at 7:12 PM, Dave Howell
> <> wrote:
>> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>>> On 6/19/10, Daniel Moore <> 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:I
>>> =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 ra=

ndom distance from the center. This will result in way too many points at t=
he center of the circle.
>
> I whipped up a quick little visualization:

Quick hack to animate the above method side by side with an alternate metho=
d:

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(700, 400)
frame.show

class RPwiaC < JPanel
def initialize times, *procs
super()

@times =3D times
@procs =3D procs
end

def paintComponent(graphics)
super(graphics)

@times.times do
@procs.each_with_index do |proc, i|
x,y =3D proc.call(200 + (300 * i),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
end

panel =3D RPwiaC.new(
5000,
lambda{|x,y,r|
angle=3Drand*2*Math:I
r*=3Drand
x+=3Dr*Math.sin(angle)
y+=3Dr*Math.cos(angle)
return x,y
},
lambda{|x,y,r|
loop {
a =3D x + rand(2*r) - r
b =3D y + rand(2*r) - r
d =3D Math.sqrt((x - a) ** 2 + (y - b) ** 2)
return a,b if d < r
}
}
)

panel.revalidate

loop { panel.repaint }

, Jun 22, 2010
7. ### Caleb ClausenGuest

On 6/21/10, Dave Howell <> wrote:
>
> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>> 7 lines, 5 minutes, 0 tests or visualizations. Super-easy.

>
> And wrong, unfortunately. You're selecting a random angle, and then a random
> distance from the center. This will result in way too many points at the
> center of the circle.

I guess this is why Benoit had that sqrt in there. I don't quite get
why it's necessary.

I kinda like Yaser's solution.

Caleb Clausen, Jun 22, 2010
8. ### Benoit DalozeGuest

On 22 June 2010 00:30, Yaser Sulaiman <> wrote:
> It's not as advanced as Daloze's solution, and it definitely can (should?=

)
> be enhanced, but my solution is available at http://gist.github.com/44755=

4.
> Any feedback is welcomed.

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).
A quick tip a friend showed me: Math.sqrt(a**2 + b**2) =3D> 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"

On 22 June 2010 01:12, Dave Howell <> wrote:
>
> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>
>> On 6/19/10, Daniel Moore <> 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:I
>> =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.
>

That was also my initial thought, and I think for most of us.

Caleb:
> I guess this is why Benoit had that sqrt in there. I don't quite get
> why it's necessary.

Indeed, please look at my solution and remove the "sqrt", and you will
see most of the points will be in the center .
The output will look like:
min: 1, moy, 1.13, max: 122 # This is way too much
max Point: (-2.0,4.0) # which is the center

The image would then look like a point in the center.
I 'felt' it would result in that because the points near the center
will be much closer to each other, as it will be as much points at any
distance.

Brabuhr's visualization is even *way* better to see it (especially the
animated one) !

And finally, why the sqrt? I just felt like I should have as many
points in each area, and the area of a circle is =F0r^2, so let's reduce
this r^2 and becomes r. (Also I wanted the random distance to be
likely further from the center, and as rand returns between 0 and 1,
we have to do ^(1/2) and not ^2.

- Benoit

Benoit Daloze, Jun 22, 2010
9. ### Guest

2010/6/22 Benoit Daloze <>:
> On 22 June 2010 01:12, Dave Howell <> wrote:
>> On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
>>> On 6/19/10, Daniel Moore <> wrote:
>>>> The quiz this week
>>>> is to generate random points uniformly distributed within a circle of
>>>> a given radius and position.

>
> Caleb:
>> I guess this is why Benoit had that sqrt in there. I don't quite get
>> why it's necessary.

>
> The image would then look like a point in the center.
> I 'felt' it would result in that because the points near the center
> will be much closer to each other, as it will be as much points at any
> distance.
>
> And finally, why the sqrt? I just felt like I should have as many
> points in each area, and the area of a circle is =F0r^2, so let's reduce
> this r^2 and becomes r. (Also I wanted the random distance to be
> likely further from the center, and as rand returns between 0 and 1,
> we have to do ^(1/2) and not ^2.

Found an explanation:
http://www.anderswallin.net/2009/05/uniform-random-points-in-a-circle-using=
-polar-coordinates/

, Jun 22, 2010
10. ### Caleb ClausenGuest

Caleb Clausen, Jun 22, 2010
11. ### timrGuest

Re: Random Points within a Circle (#234)

This is actually a pretty interesting puzzle. Correct solutions should
have points that are distributed as a hump Gaussian-like curve when
histogram plotted over the y rand and x range. My first solution had a
humped-histogram over the X ranges but was evenly distributed over the
Y ranges. Back to the drawing board. The algorithm I ended up using
makes points distributed over the Y and X range, but only keeps them
if they are within the circle. Used R to plot. Code below:

class Circle
def initialize(r,x,y)
@r = r
@y = y
@x = x
end
#get random x and y from within circle space
def internal_rand
#to randomly select rise run values
rise = rand(0)*@r*rand_sign
run = rand(0)*@r*rand_sign
#validate that point lies within circle, about 20% fail
if Math.sqrt(run**2+rise**2)<@r
[@x+run,@y+rise]
else
internal_rand
end
end
private
def rand_sign
rand(2) == 0? -1 : 1
end
end

#test
c1 = Circle.new(4,10,10)
xs = []; ys = []
1000.times do
point = c1.internal_rand
xs << point.first
ys << point.last
end

plotted in R using:
x = c(pasted in xs from ruby output)
y = c(pasted in ys from ruby output)
plot(x,y)
library(plotrix)
draw.circle(10,10,4) #evenly distribute points within circle
hist(x, breaks=20) #Gaussian histogram bar distribution
hist(y, breaks=20) #Gaussian histogram bar distribution

On Jun 19, 11:05 am, Daniel Moore <> wrote:
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> The three rules of Ruby Quiz:
>
> 1.  Please do not post any solutions or spoiler discussion for this
> quiz until 48 hours have elapsed from the time this message was
> sent.
>
> 2.  Support Ruby Quiz by submitting ideas and responses
> as often as you can.
>
> 3.  Enjoy!
>
> Suggestion:  A [QUIZ] in the subject of emails about the problem
> the original quiz message, if you can.
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>
>
> Suggestions?:http://rubyquiz.strd6.com/suggestions
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Random Points within a Circle (#234)
>
> Greetings Rubyists,
>
> Generating random numbers is a useful feature and Ruby provides us
> with `Kernel.rand` to generate numbers uniformly distributed between
> zero and one. If we want to generate random numbers with other
> distributions, then we'll need to do a little work. The quiz this week
> is to generate random points uniformly distributed within a circle of
> a given radius and position.
>
> This quiz is relatively simple, great if you are new to Ruby or
> strapped for time. Remember, it is contributions from people like
> *you* that make Ruby Quiz succeed!
>
> Have fun!
>
> --
> -Danielhttp://rubyquiz.strd6.com

timr, Jun 22, 2010
12. ### Colin BartlettGuest

[Note: parts of this message were removed to make it a legal post.]

(Apologies for this oldish "re-post": it seems that changing from googlemail
to gmail makes emails not recognised by ruby-talk.)

On Tue, Jun 22, 2010 at 3:41 AM, Caleb Clausen <> wrote:

> On 6/21/10, Dave Howell <> wrote:
> >
> > On Jun 21, 2010, at 15:45 , Caleb Clausen wrote:
> >> 7 lines, 5 minutes, 0 tests or visualizations. Super-easy.

> >
> > And wrong, unfortunately. You're selecting a random angle, and then a

> random
> > distance from the center. This will result in way too many points at the
> > center of the circle.

>
> I guess this is why Benoit had that sqrt in there. I don't quite get
> why it's necessary.
>

> I kinda like Yaser's solution.
>

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.
(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.)

** The rest of this has now been covered by other posts, but the links might
be interesting.

The random angle bit in your method is OK.
For the square root: the area of an annulus radius r to r+e is
pi * ( (r+e)**2 - r**2 ) = pi * 2*r*e + pi * e**2
So, if I treat e as an infinitesimal and neglect the e**2 (and hope that no
real mathematicians are looking, or claim I'm using
Abraham Robinson's http://en.wikipedia.org/wiki/Abraham_robinson
Non-Standard Analysis http://en.wikipedia.org/wiki/Non-standard_analysis
and hope that nobody asks me any awkward questions about how that really
works)
the area of the annulus is (approximately) pi * 2*r*e.
(Think of it as a (very) "bent" rectangle of length pi * 2*r with a "width"
of e.)

If you use a uniform distribution for the radius to the random point then on
average you'll be putting the same number of points in an annulus (a0) of
width e very close to the centre of the circle as in an annulus (a1) of
width e very close to the circumference of the circle. But the annulus (a1)
has a much larger area than the annulus (a0), so the density of points will
be greater in annulus (a0) than annulus (a1). Which is why Dave Howell made
his comment.

So if we want to use a method which selects a random angle and a random
distance from the centre of the circle, for the random distance from the
centre of the circle we need a 0 to 1 distribution which isn't uniform but
which has more weight for higher than lower values in 0 to 1. When I saw
this quiz I thought of using the random angle and distance method, and after
a bit of work guessed that the correct distribution for the distance was
probably to take the square root of random numbers generated from a uniform
0 to 1 distribution. BUT: I couldn't immediately see a way to prove that! So
I didn't post anything! I suspect Benoit can *prove* that taking the square
root gives the correct distribution for the radius!

def uniform_random_point_in_
circle( r, x, y )
# circle radius r, centre at x, y
r_to_point = Math.sqrt( Kernel.rand ) * r
radians_to_point = 2 * Math:I * Kernel.rand
return x + r_to_point * Math.cos( radians_to_point ),
y + r_to_point * Math.sin( radians_to_point )
end

Colin Bartlett, Jun 23, 2010
13. ### Caleb ClausenGuest

On 6/22/10, Colin Bartlett <> wrote:
> The random angle bit in your method is OK.
> For the square root: the area of an annulus radius r to r+e is
> pi * ( (r+e)**2 - r**2 ) = pi * 2*r*e + pi * e**2
> So, if I treat e as an infinitesimal and neglect the e**2 (and hope that no
> real mathematicians are looking, or claim I'm using
> Abraham Robinson's http://en.wikipedia.org/wiki/Abraham_robinson
> Non-Standard Analysis http://en.wikipedia.org/wiki/Non-standard_analysis
> and hope that nobody asks me any awkward questions about how that really
> works)
> the area of the annulus is (approximately) pi * 2*r*e.
> (Think of it as a (very) "bent" rectangle of length pi * 2*r with a "width"
> of e.)
>
> If you use a uniform distribution for the radius to the random point then on
> average you'll be putting the same number of points in an annulus (a0) of
> width e very close to the centre of the circle as in an annulus (a1) of
> width e very close to the circumference of the circle. But the annulus (a1)
> has a much larger area than the annulus (a0), so the density of points will
> be greater in annulus (a0) than annulus (a1). Which is why Dave Howell made
> his comment.

That was beautiful! The best explanation yet. Thank you.

I keep wondering the use of sqrt will cause weird float rounding
errors; that is will there be valid [float, float] pairs which are in
the circle but can never be be found by an algorithm that includes
Math.sqrt(rand)? Maybe someone with a better grip on the math has an

Caleb Clausen, Jun 23, 2010
14. ### Martin BoeseGuest

On Sun, 20 Jun 2010 03:05:58 +0900
Daniel Moore <> wrote:
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Random Points within a Circle (#234)
>
> Greetings Rubyists,
>
> Generating random numbers is a useful feature and Ruby provides us
> with `Kernel.rand` to generate numbers uniformly distributed between
> zero and one. If we want to generate random numbers with other
> distributions, then we'll need to do a little work. The quiz this week
> is to generate random points uniformly distributed within a circle of
> a given radius and position.
>
> This quiz is relatively simple, great if you are new to Ruby or
> strapped for time. Remember, it is contributions from people like
> *you* that make Ruby Quiz succeed!
>
> Have fun!

Inspired by the other solutions here are two possible algorithms with a
little benchmark and Tk visualization (call like ruby circlepoints.rb
[n_points=5000] [a || b]).

Martin

--------------------

# Solution A: Randomly select a point in a square and retry if out of
# the circle
n_points.times do
begin
x, y = [-1+2*rand, -1+2*rand]
end while Math.sqrt(x**2+y**2).abs > 1
end
end

# Solution B: Random angle and distance
n_points.times do
angle = rand*2*Math:I
r = Math.sqrt(rand)
end
end

require 'tk'
require 'benchmark'

canvas = TkCanvas.new

n_points = (ARGV[0] || 5000).to_i

# select an algorithm
method = ARGV.include?('b') ? :mk_points_b : :mk_points_a

bench = Benchmark.measure do
radius + x + 1, radius + y + 1, 'fill' => 'black')
end
end

puts "Using: #{method}"
puts bench.to_s

canvas.pack
Tk.mainloop

Martin Boese, Jun 23, 2010
15. ### Yaser SulaimanGuest

[Note: parts of this message were removed to make it a legal post.]

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"

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

Regards,
Yaser

Yaser Sulaiman, Jun 24, 2010
16. ### Lars HaugsethGuest

Lars Haugseth, Jun 25, 2010
17. ### Lars HaugsethGuest

* Benoit Daloze <> wrote:
>
> I had an intuition doing some Math.sqrt about the distance, and it
> revealed to be exact

I bet you didn't do that right from the start, before seeing the
first results? At least I didn't.

--
Lars Haugseth

Lars Haugseth, Jun 25, 2010
18. ### Benoit DalozeGuest

On 24 June 2010 21:24, Yaser Sulaiman <> wrote:
> [...] although I have to admit that
> talking about generating random vectors sounds "cooler"

Yes, and a Point + Point is not as meaningful

> Because I'm not using Vectors anymore, Point + Point now performs
> the equivalent 2D translation. Also, Point.distance_to now uses Math.hypot.

because it returns @y.
So you probably want to return self if you accept the Point objects to
be mutable,
or create a new Point, which is a bit safer, but creates a new object
(in both cases you could definitely get rid of this awful "return p" )

Also, @@origin should not be modified, and then should be a constant,
and be #freeze if you want Point to be mutable.

On 25 June 2010 03:05, Lars Haugseth <> wrote:
> * Benoit Daloze <> wrote:
>>
>> I had an intuition doing some Math.sqrt about the distance, and it
>> revealed to be exact

>
> I bet you didn't do that right from the start, before seeing the
> first results? At least I didn't.
> --
> Lars Haugseth

I thought while writing it (without sqrt) that it was going wrong, and
the next day I thought to sqrt.
The night is a good adviser

B.D.

Benoit Daloze, Jun 25, 2010
19. ### Yaser SulaimanGuest

[Note: parts of this message were removed to make it a legal post.]

On Fri, Jun 25, 2010 at 1:57 PM, Benoit Daloze <> wrote:

> Cool, but I believe your implementation of Point#+ is somehow bad,
> because it returns @y.
> So you probably want to return self if you accept the Point objects to
> be mutable,
> or create a new Point, which is a bit safer, but creates a new object
> (in both cases you could definitely get rid of this awful "return p" )
>
>

The _awful_ "return p" is now gone
I want Point objects to be mutable, so Point#+ returns self
after modifying the x and y coordinates.

> Also, @@origin should not be modified, and then should be a constant,
> and be #freeze if you want Point to be mutable.

Good point. The origin is now a "frozen" class constant.

Yaser Sulaiman, Jun 25, 2010
20. ### timrGuest

timr, Jun 27, 2010