Author Topic: Prime Numbers Fractal  (Read 4486 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Prime Numbers Fractal
« on: October 19, 2019, 02:18:43 am »
Hi everyone! :D
I have recently watched 3Blue1Brown's video on prime number where he plots (p,p) in polar Cartesian plane where p is a prime number.
I did the same thing but with a twist and it seems like I have created a new fractal.
Run the program & Enjoy!
Code: QB64: [Select]
  1. 'Prime Number Fractal By Ashish
  2. '19 Oct, 2019
  3. _TITLE "Prime Number Fractal By Ashish"
  4. SCREEN _NEWIMAGE(700, 700, 32)
  5. s = 1
  6.     s = s + 25
  7.     FOR i = 1 TO s
  8.         IF isPrime(i) THEN
  9.             xx = map(COS(i) * i, 0, s, 0, s / 16) * COS(z) + _WIDTH / 2
  10.             yy = map(SIN(i) * i, 0, s, 0, s / 16) * SIN(z) + _HEIGHT / 2
  11.             r = map(ABS(xx - 350), 0, 350, 255, 50)
  12.             g = map(ABS(yy - 350), 0, 350, 255, 50)
  13.             PSET (xx, yy), _RGB32(r, g, (r + g) / 2, 100)
  14.         END IF
  15.     NEXT
  16.     _DISPLAY
  17.     _LIMIT 48
  18.     z = z + .1
  19. LOOP UNTIL s > 10000
  20. FUNCTION isPrime (n AS INTEGER)
  21.     k = 0
  22.     IF n < 2 THEN EXIT FUNCTION
  23.     FOR i = 2 TO n
  24.         IF n MOD i = 0 THEN k = k + 1
  25.         IF k > 1 THEN EXIT FUNCTION
  26.     NEXT
  27.     IF k = 1 THEN isPrime = -1
  28. 'from p5js.bas
  29. 'https://bit.ly/p5jsbas
  30. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  31.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  32.  

Screenshot_1.png
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Prime Numbers Fractal
« Reply #1 on: October 19, 2019, 05:20:25 am »
Hi Ashish,

 
Answer.jpg

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Prime Numbers Fractal
« Reply #2 on: October 19, 2019, 10:29:07 am »
Hi Ashish,

This is nice graphic even without Prime Numbers!

Code: QB64: [Select]
  1. 'Prime Number Fractal By Ashish
  2. '19 Oct, 2019
  3. _TITLE "Prime Number Fractal By Ashish mod B+ without Prime Numbers"
  4. SCREEN _NEWIMAGE(700, 700, 32)
  5. s = 1
  6.     s = s + 10
  7.     FOR i = 1 TO s
  8.         IF RND > .8 THEN
  9.             xx = map(COS(i) * i, 0, s, 0, s / 16) * COS(z) + _WIDTH / 2
  10.             yy = map(SIN(i) * i, 0, s, 0, s / 16) * SIN(z) + _HEIGHT / 2
  11.             r = map(ABS(xx - 350), 0, 350, 255, 50)
  12.             g = map(ABS(yy - 350), 0, 350, 255, 50)
  13.             PSET (xx, yy), _RGB32(r, (r + g) / 2, g, 10)
  14.         END IF
  15.     NEXT
  16.     _DISPLAY
  17.     _LIMIT 48
  18.     z = z + .1
  19. LOOP UNTIL s > 10000
  20. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  21.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  22.  

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Prime Numbers Fractal
« Reply #3 on: October 19, 2019, 10:46:07 am »
@Petr
Thanks!
@Bplus
Cool. It resembles like the original one. I choose prime number because they less in number.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Prime Numbers Fractal
« Reply #4 on: October 19, 2019, 11:35:52 am »
Quote
because they less in number.

Ah density, modulus would have much more control of density PLUS uniforimty!

Here is increasing Modulus = m (decreasing density) gives us a little intuitive look at the drawing algorithm.
Code: QB64: [Select]
  1. 'Prime Number Fractal By Ashish
  2. '19 Oct, 2019
  3. _TITLE "Prime Number Fractal By Ashish mod B+ without Prime Numbers"
  4. SCREEN _NEWIMAGE(700, 700, 32)
  5. s = 1
  6. m = 5
  7.     m = m + 5
  8.     s = 0
  9.     CLS
  10.     DO
  11.         s = s + m
  12.         FOR i = 1 TO s
  13.             IF i MOD m = 0 THEN
  14.                 xx = map(COS(i) * i, 0, s, 0, s / 16) * COS(z) + _WIDTH / 2
  15.                 yy = map(SIN(i) * i, 0, s, 0, s / 16) * SIN(z) + _HEIGHT / 2
  16.                 r = map(ABS(xx - 350), 0, 350, 255, 50)
  17.                 g = map(ABS(yy - 350), 0, 350, 255, 50)
  18.                 PSET (xx, yy), _RGB32(r, (r + g) / 2, g, 100)
  19.             END IF
  20.         NEXT
  21.         _DISPLAY
  22.         _LIMIT 100
  23.         z = z + _PI(1 / 36)
  24.     LOOP UNTIL s > 10000
  25.     _DELAY 1
  26. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  27.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  28.  
  29.  
  30.  


Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Prime Numbers Fractal
« Reply #5 on: October 20, 2019, 03:59:29 am »
@Bplus
Wow! So, a lot of more pattern can be generated with this. Cool!
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials