Author Topic: Visualizing _PI challenge  (Read 6370 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

FellippeHeitor

  • Guest
Visualizing _PI challenge
« on: March 15, 2019, 10:37:15 am »
Anyone would be willing to take on the challenge to recreate this or even reinvent it? https://www.reddit.com/r/dataisbeautiful/comments/b13h1d/adding_to_the_pi_visualizations_oc/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Visualizing _PI challenge
« Reply #1 on: March 15, 2019, 11:02:41 am »
Hi Fellippe,

Is there some sort of calculation of Pi going on or is this just bouncing from digit to digit of a Pi expansion?

Repeating digits could use a little loop, internal or external.
« Last Edit: March 15, 2019, 11:04:26 am by bplus »

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Visualizing _PI challenge
« Reply #2 on: March 15, 2019, 11:37:32 am »
... And when you have taken up this challenge, unless you produce pi to more than 31 trillion digits, we shall think that you have hardly tried at all.  (To any mathematicians out there, why do mathematicians do that sort of thing and who knows if they're correct?).

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Visualizing _PI challenge
« Reply #3 on: March 15, 2019, 12:28:29 pm »
It looks interesting. To calculate pi, we could use the formula π = 2 x (2 x 2 x 4 x 4 x 6 x 6 ...) / (1 x 3 x 3 x 5 x 5 x 7 ...)
but a sample example uses only a few decimal places to visualize ...

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. TYPE nrs
  4.     x AS INTEGER
  5.     y AS INTEGER
  6. DIM N(10) AS nrs
  7.  
  8. CONST CX = 400
  9. CONST CY = 300
  10. pi$ = "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273"
  11.  
  12. FOR p = -60 TO 264 STEP 36
  13.     q = _D2R(p)
  14.     N(w).x = CX + COS(q) * 150
  15.     N(w).y = CY + SIN(q) * 150
  16.     w = w + 1
  17.     CIRCLE (CX + COS(q) * 150, CY + SIN(q) * 150), 3
  18.     _PRINTSTRING (CX + COS(q) * 170, CY + SIN(q) * 170), _TRIM$(STR$(num))
  19.     num = num + 1
  20.  
  21. CIRCLE (400, 300), 150
  22.  
  23. FOR sh = 1 TO LEN(pi$)
  24.     IF ASC(pi$, sh) >= 48 AND ASC(pi$, sh) <= 57 THEN
  25.         v$ = MID$(pi$, sh, 1)
  26.         LINE (ox, oy)-(ox2, oy2), _RGB32(127, 127, 127)
  27.          i = ASC(pi$, sh) - 48
  28.         LOCATE 1, 1: PRINT MID$(pi$, 1, sh)
  29.         ox = N(oldN).x: oy = N(oldN).y: ox2 = N(i).x: oy2 = N(i).y
  30.         LINE (N(oldN).x, N(oldN).y)-(N(i).x, N(i).y), _RGB32(255, 255, 255): oldN = i
  31.         oldN = i
  32.         _DELAY .2
  33.     END IF
  34. NEXT sh
  35.  
« Last Edit: March 15, 2019, 12:34:06 pm by Petr »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Visualizing _PI challenge
« Reply #4 on: March 15, 2019, 12:46:30 pm »
There is one more way - the infinite division using MOD, but the fraction 22/7 is inaccurate, because there is a wrong result in the third decimal place. Another fraction is 355/113, but there will also be an error in the seventh decimal place.

FellippeHeitor

  • Guest
Re: Visualizing _PI challenge
« Reply #5 on: March 15, 2019, 12:53:51 pm »
Wow, that was fast, Petr. Good job!

@bplus
I don't know if they're calculating as they go or if it's just fetching from a precalculated list of digits. I just liked the visualization very much when I fist came across it.

@Qwerkey
It's indeed silly to try to break any supercomputer's record in findind the next digits of pi... Maybe mathematicians just wanna have fun in the end too (and measure dick).

I'm looking to take part in the challenge myself. I'll keep this thread as a post-it reminder for me to do it eventually.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Visualizing _PI challenge
« Reply #6 on: March 15, 2019, 12:58:30 pm »
Thank you Fellippe, it was a welcome change, such a rebound from the 3D maze editor that I am writing.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Visualizing _PI challenge
« Reply #7 on: March 15, 2019, 01:08:36 pm »
I think the Spigot algorithm would be perfect for this https://rosettacode.org/wiki/Pi

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Visualizing _PI challenge
« Reply #8 on: March 15, 2019, 01:15:32 pm »
I haven't figured out how far my string math routines would go, I just know if it was accurate and infinite, it would take about 40 years to surpass the record pi calculation to date, of 31.4 trillion digits. Anyone know where I can find a computer that will last 40 years, without a %^&^$# Windows update?

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Visualizing _PI challenge
« Reply #9 on: March 15, 2019, 02:14:22 pm »
Hey Petr, nice and fast.

I had basic scheme worked out when I saw you had already posted. So I better have some better features!

I do. :)

This draws an outer loop around digit when digit repeats sequentially (you have to wait awhile for 7), plus it has the pointer moving between points...
Code: QB64: [Select]
  1. _TITLE "Visualizing Pi" 'B+ 2019-03-15
  2.  
  3. 'Featuring a circle loop around the a repeated digit.
  4. ' 3 first, 8, 9, 4, 1, 6, 2, 5, 0 (300+ digits), 7 (560 digits) EDIT
  5.  
  6. 'ref  http://www.math.com/tables/constants/pi.htm  First 100 digits
  7. CONST pi100 = "3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067"
  8. 'ref same as above 1000 digits, see if can circle all digits pi shortened to 560 digits
  9. CONST pi = "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277"
  10. 'PRINT INSTR(pi, "77") ' last number to get circled for repeating at 560
  11.  
  12. CONST xmax = 800
  13. CONST ymax = 720
  14. CONST x0 = 400
  15. CONST y0 = 420
  16. CONST radius = 240
  17. CONST r2 = 20
  18.  
  19. TYPE vectorType
  20.     x AS SINGLE
  21.     y AS SINGLE
  22.  
  23. SCREEN _NEWIMAGE(xmax, ymax, 32)
  24. _SCREENMOVE 300, 20
  25. s2& = _NEWIMAGE(xmax, ymax, 32) 'to update drawing lines in isolation
  26. t = TIMER(.001)
  27. ca = _PI(2 / 10)
  28. DIM vdigit(0 TO 9) AS vectorType
  29. DIM outer(0 TO 9) AS vectorType
  30. CIRCLE (x0, y0), radius
  31. FOR i = 0 TO 9
  32.     vdigit(i).x = x0 + radius * COS(ca * i - 2 * ca)
  33.     vdigit(i).y = y0 + radius * SIN(ca * i - 2 * ca)
  34.     CIRCLE (vdigit(i).x, vdigit(i).y), 2
  35.     x = x0 + (radius + 18) * COS(ca * i - 2 * ca) - 2
  36.     y = y0 + (radius + 18) * SIN(ca * i - 2 * ca) - 6
  37.     _PRINTSTRING (x, y), LTRIM$(STR$(i))
  38.     outer(i).x = x0 + (radius + r2) * COS(ca * i - 2 * ca)
  39.     outer(i).y = y0 + (radius + r2) * SIN(ca * i - 2 * ca)
  40. _PUTIMAGE , 0, s2&
  41.  
  42. digitPointer = 1
  43. cx = x0: cy = y0: speed = 10
  44. WHILE digitPointer <= LEN(pi)
  45.     d = VAL(MID$(pi, digitPointer, 1))
  46.     targetx = vdigit(d).x: targety = vdigit(d).y
  47.     targetangle = _ATAN2(targety - cy, targetx - cx)
  48.     rc~& = _RGB32(255 * RND, 255 * RND, 255 * RND)
  49.     IF lastd = d THEN
  50.         _DEST s2&
  51.         CIRCLE (outer(d).x, outer(d).y), r2, rc~&
  52.         _DEST 0
  53.         CLS
  54.         _PUTIMAGE , s2&, 0
  55.         CIRCLE (cx, cy), 2, &HFFFFFFFF, BF
  56.         LOCATE 1, 1: PRINT MID$(pi, 1, digitPointer - 1)
  57.         _DISPLAY
  58.     ELSE
  59.         DO
  60.             dist = SQR((targetx - cx) ^ 2 + (targety - cy) ^ 2)
  61.             IF dist < speed THEN
  62.                 newX = targetx
  63.                 newY = targety
  64.             ELSE
  65.                 newX = cx + speed * COS(targetangle)
  66.                 newY = cy + speed * SIN(targetangle)
  67.             END IF
  68.             _DEST s2&
  69.             LINE (cx, cy)-(newX, newY), rc~&
  70.             _DEST 0
  71.             CLS
  72.             _PUTIMAGE , s2&, 0
  73.             CIRCLE (newX, newY), 2, &HFFFFFFFF, BF
  74.             cx = newX: cy = newY
  75.             LOCATE 1, 1: PRINT MID$(pi, 1, digitPointer - 1)
  76.             _DISPLAY
  77.             _LIMIT 60
  78.         LOOP UNTIL cx = targetx AND cy = targety
  79.     END IF
  80.     digitPointer = digitPointer + 1
  81.     lastd = d
  82. LOCATE 1, 1: PRINT MID$(pi, 1, digitPointer)
  83. PRINT TIMER(.001) - t; " secs"
  84.  


« Last Edit: March 15, 2019, 02:25:15 pm by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Visualizing _PI challenge
« Reply #10 on: March 15, 2019, 02:31:17 pm »
Hi BPlus!  It's very nice. I even discovered the BF parameter for Circle with your program, I haven't seen it yet. Motion in circle is also very interesting.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Visualizing _PI challenge
« Reply #11 on: March 15, 2019, 02:34:44 pm »
Hi BPlus!  It's very nice. I even discovered the BF parameter for Circle with your program, I haven't seen it yet. Motion in circle is also very interesting.

HA! Twice!
Code: QB64: [Select]
  1. CIRCLE (cx, cy), 2, &HFFFFFFFF, BF  
  2.  

I started with a line for a solid pointer, forgot to erase that BF!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Visualizing _PI challenge
« Reply #12 on: March 15, 2019, 02:37:40 pm »
Quote
I started with a line for a solid pointer, forgot to erase that BF!

Really? I thought I discovered something new! :-D

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Visualizing _PI challenge
« Reply #13 on: March 15, 2019, 04:36:30 pm »
hello bplus
here's spigot sub for you
Code: QB64: [Select]
  1. DIM pi(100) AS _BYTE
  2. spigot pi()
  3.  
  4. PRINT LTRIM$(STR$(pi(1))); ".";
  5. FOR i = 2 TO UBOUND(pi)
  6.     PRINT LTRIM$(STR$(pi(i)));
  7.  
  8. SUB spigot (pi() AS _BYTE)
  9.     DIM N AS LONG
  10.     N = UBOUND(pi)
  11.     DIM ln AS LONG
  12.     ln = INT((10 * N) / 3) + 1
  13.     DIM A(0 TO ln - 1) AS LONG
  14.     DIM npi AS LONG
  15.     DIM i AS LONG, j AS LONG, k AS LONG
  16.     npi = 0
  17.     FOR i = 0 TO ln - 1
  18.         A(i) = 2
  19.     NEXT
  20.  
  21.     DIM nines AS LONG
  22.     DIM predigit AS LONG
  23.     nines = 0
  24.     predigit = 0
  25.     FOR j = 1 TO N
  26.         DIM q AS LONG
  27.         q = 0
  28.         FOR i = ln TO 1 STEP -1
  29.             DIM x AS LONG
  30.             x = (10 * A((i - 1))) + (q * i)
  31.             A((i - 1)) = x MOD ((2 * i) - 1)
  32.             q = x \ ((2 * i) - 1)
  33.         NEXT
  34.         A(0) = q MOD 10
  35.         q = q \ 10
  36.  
  37.         IF 9 = q THEN
  38.             nines = nines + 1
  39.         ELSEIF 10 = q THEN
  40.             pi(npi) = predigit + 1: npi = npi + 1
  41.             FOR k = 0 TO nines - 1
  42.                 pi(npi) = 0: npi = npi + 1
  43.             NEXT
  44.             predigit = 0
  45.             nines = 0
  46.         ELSE
  47.             pi(npi) = predigit: npi = npi + 1
  48.             predigit = q
  49.  
  50.             IF 0 <> nines THEN
  51.                 FOR k = 0 TO nines - 1
  52.                     pi(npi) = 9: npi = npi + 1
  53.                 NEXT
  54.                 nines = 0
  55.             END IF
  56.         END IF
  57.     NEXT
  58.     pi(npi) = predigit
  59.  
« Last Edit: March 17, 2019, 03:25:47 am by jack »

Marked as best answer by on November 19, 2024, 04:20:03 am

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Visualizing _PI challenge
« Reply #14 on: March 15, 2019, 05:53:27 pm »
  • Undo Best Answer
  • Thanks Jack I will check it out.

    Update: That compares very well with the reference Pi I was using. I used the following code as a check:
    Code: QB64: [Select]
    1. 'from Jack 2019-03-15 QB64 forum "Visualizing Pi" B+ mod to check Pi with reference Pi.
    2. _TITLE "Compare Spigot to Pi Ref digit by digit"
    3. SCREEN _NEWIMAGE(600, 720, 32)
    4. _SCREENMOVE 300, 20
    5. CONST piRef = "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277"
    6. DIM pi%(561)
    7. spigot pi%()
    8.  
    9. FOR i = 1 TO UBOUND(pi%)
    10.     PRINT i; " digit spigot vrs ref "; pi%(i); " vrs "; MID$(piRef, i, 1)
    11.     IF i MOD 30 = 29 THEN
    12.         INPUT "OK, press enter "; wate$
    13.         CLS
    14.     END IF
    15.  
    16. SUB spigot (pi%())
    17.     DIM N%
    18.     N% = UBOUND(pi%)
    19.     DIM ln AS LONG
    20.     ln = INT((10 * N%) / 3) + 1
    21.     DIM A(0 TO ln - 1) AS LONG
    22.     DIM npi AS LONG
    23.     DIM i AS INTEGER, j AS INTEGER, k AS INTEGER
    24.     npi = 0
    25.     FOR i = 0 TO ln - 1
    26.         A(i) = 2
    27.     NEXT
    28.  
    29.     DIM nines AS LONG
    30.     DIM predigit AS LONG
    31.     nines = 0
    32.     predigit = 0
    33.     FOR j = 1 TO N%
    34.         DIM q AS LONG
    35.         q = 0
    36.         FOR i = ln TO 1 STEP -1
    37.             DIM x AS LONG
    38.             x = (10 * A((i - 1))) + (q * i)
    39.             A((i - 1)) = x MOD ((2 * i) - 1)
    40.             q = x \ ((2 * i) - 1)
    41.         NEXT
    42.         A(0) = q MOD 10
    43.         q = q \ 10
    44.  
    45.         IF 9 = q THEN
    46.             nines = nines + 1
    47.         ELSEIF 10 = q THEN
    48.             pi%(npi) = predigit + 1: npi = npi + 1
    49.             FOR k = 0 TO nines - 1
    50.                 pi%(npi) = 0: npi = npi + 1
    51.             NEXT
    52.             predigit = 0
    53.             nines = 0
    54.         ELSE
    55.             pi%(npi) = predigit: npi = npi + 1
    56.             predigit = q
    57.  
    58.             IF 0 <> nines THEN
    59.                 FOR k = 0 TO nines - 1
    60.                     pi%(npi) = 9: npi = npi + 1
    61.                 NEXT
    62.                 nines = 0
    63.             END IF
    64.         END IF
    65.     NEXT
    66.     pi%(npi) = predigit
    67.  
    68.  
    « Last Edit: March 15, 2019, 07:14:11 pm by bplus »