🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.pascal.delphi.misc
Article #729

Re: Buffon's Needles

#729
From: Dr John Stockton
Date: Mon, 28 Jun 1999 00:00
65 lines
3792 bytes
JRS:  In article <j+NWi+AgBrd3EwbC@merlyn.demon.co.uk> of Mon, 28 Jun
1999 00:14:40 in news:comp.lang.pascal.delphi.misc, Dr John Stockton
<jrs@merlyn.demon.co.uk> wrote:
> ...
>Yes, *that* should work - actually, ISTM that one could make P1 = (0,0),
>randomly generate P2 = (X,Y) within a square around that, accept those
>P2 within the inscribed circle and not too near the centre, and use
>ATAN2(Y, X).
> ...



If one randomly generates TWO points within a square, repeating until
they are neither too near or too distant, then (except in the limit as
"too far" tends to zero, or the square tends to infinite) the slope of
the line joining the first to the second IS NOT as if drawn randomly
from a uniform distribution covering all directions.  In those limits,
the proportion of points accepted tends to zero, which is inefficient.

If one randomly generates ONE point within a square, repeating until the
centre is neither too near or too distant, then the slope of the line
joining the centre to the point IS as if drawn randomly from a uniform
distribution covering all directions - too distant is greater than half
the side of the square.

Test program for square Abs(X)<1, Abs(Y)<1; BP7, ATAN2 from Terje
Mathisen, note "(*..*)" comment :


function atan2(y : extended; x : extended): Extended ; Assembler;
asm  fld [y] ; fld [x] ; fpatan  end ;

var R2, X1, X2, Y1, Y2 : extended ;
A : array [-24..24] of longint ;
J, K, L : longint ;
const JM = 30000000 ;

begin Writeln('---') ; FillChar(A, SizeOf(A), 0) ; J := 0 ;
repeat
  X1 := 2*random-1 ; Y1 := 2*random-1 ;    (* use this line *)
  X1 := 0 ; Y1 := 0 ;                      (* or this one *)
  X2 := 2*random-1 ; Y2 := 2*random-1 ;
  R2 := Sqr(X1-X2) + Sqr(Y1-Y2) ;
  if (R2<1.0) and (R2>0.0001) then begin
    K := Round(7.321 * Atan2(Y1-Y2, X1-X2)) ;
    Inc(A[K]) ; Inc(J) end ;
  until J=JM ;
K := 0 ;
for J := Low(A) to High(A) do if A[J]>K then K := A[J] ;
for J := Low(A) to High(A) do Writeln(J*180/23:0:3,
  '*':Round(A[J]*(60/K))) ;
Write(^G) ; Readln end.


As it also works compiled with D3 "DCC32 -cc", that ATAN2 must do in D3.


Do let us know if an error can be seen in the above program, or if with
two random points it does not test what you intended, on varying the
constant sides of the R2 comparisons.  7.231 and 60 merely scale for
display.
 
-- 
 � John Stockton, Surrey, UK. jrs@merlyn.demon.co.uk / JR.Stockton@physics.org �
  Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Technical Details
Message-ID: <7+KQSfA9R7d3EwYX@merlyn.demon.co.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <7ks8ls$mgk$1@bgtnsc03.worldnet.att.net> <Im0c0MABmjc3Ew2K@merlyn.demon.co.uk> <zZIi7jAzW8c3EwDD@merlyn.demon.co.uk> <7l357k$4m6$1@nnrp1.deja.com> <3zADx9AXVWd3EwJU@merlyn.demon.co.uk> <7l61qb$u9u$1@nnrp1.deja.com> <j+NWi+AgBrd3EwbC@merlyn.demon.co.uk>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 10baee,d24982a2aa52a35f
X-Google-Attributes: gid10baee,public
X-Google-Thread: 10a726,d24982a2aa52a35f
X-Google-Attributes: gid10a726,public
X-Google-Thread: 105ac8,d24982a2aa52a35f
X-Google-Attributes: gid105ac8,public
X-Google-Thread: 10a7f1,d24982a2aa52a35f
X-Google-Attributes: gid10a7f1,public
From: Dr John Stockton <jrs@merlyn.demon.co.uk>
Subject: Re: Buffon's Needles
Date: 1999/06/28
Message-ID: <7+KQSfA9R7d3EwYX@merlyn.demon.co.uk>
X-Deja-AN: 494854064
Content-Transfer-Encoding: 8bit
X-NNTP-Posting-Host: merlyn.demon.co.uk:158.152.233.145
References: <7ks8ls$mgk$1@bgtnsc03.worldnet.att.net> <Im0c0MABmjc3Ew2K@merlyn.demon.co.uk> <zZIi7jAzW8c3EwDD@merlyn.demon.co.uk> <7l357k$4m6$1@nnrp1.deja.com> <3zADx9AXVWd3EwJU@merlyn.demon.co.uk> <7l61qb$u9u$1@nnrp1.deja.com> <j+NWi+AgBrd3EwbC@merlyn.demon.co.uk>
Content-Type: text/plain;charset=iso-8859-1
X-Complaints-To: abuse@demon.net
X-Trace: news.demon.co.uk 930593451 nnrp-09:3745 NO-IDENT merlyn.demon.co.uk:158.152.233.145
Organization: Home, Surrey, UK
MIME-Version: 1.0
Newsgroups: comp.lang.pascal.delphi.misc,alt.comp.borland-delphi,de.comp.lang.pascal.delphi,fr.comp.lang.pascal