Author Topic: Double Pendulum  (Read 5124 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Double Pendulum
« on: March 04, 2018, 05:48:06 am »
Hi Everyone! :)
This program traces the motion of the double pendulum to get beautiful patterns.
Press 'Space' for new settings.
Code: QB64: [Select]
  1. 'Coded By Ashish on 4 March, 2018
  2.  
  3. _TITLE "Double Pendulum [Press Space for New Settings]"
  4.  
  5. SCREEN _NEWIMAGE(800, 600, 32)
  6.  
  7. TYPE vec2
  8.     x AS SINGLE
  9.     y AS SINGLE
  10.  
  11. TYPE pendlm
  12.     pos AS vec2
  13.     r AS SINGLE
  14.     ang AS DOUBLE
  15.     angInc AS DOUBLE
  16.     angSize AS DOUBLE
  17.  
  18. DIM SHARED pendulum(1) AS pendlm
  19. change_settings:
  20. pendulum(0).r = p5random(250, 350)
  21. pendulum(0).pos.x = 400
  22. pendulum(0).pos.y = 0
  23. pendulum(0).angInc = p5random(0, _PI(2))
  24. pendulum(0).angSize = p5random(_PI(.1), _PI(.6))
  25.  
  26. pendulum(1).angInc = p5random(0, _PI(2))
  27. pendulum(1).r = p5random(100, 300)
  28. pendulum(1).angSize = p5random(_PI(.1), _PI(.9))
  29. px = 0: py = 0: a = 0: f = 0
  30.  
  31. tracer& = _NEWIMAGE(_WIDTH, _HEIGHT, 32)
  32.     CLS
  33.     k& = _KEYHIT
  34.     IF a < 255 THEN a = a + 1
  35.     IF k& = ASC(" ") THEN _FREEIMAGE tracer&: GOTO change_settings
  36.     tx1 = COS(pendulum(0).ang) * pendulum(0).r + pendulum(0).pos.x
  37.     ty1 = SIN(pendulum(0).ang) * pendulum(0).r + pendulum(0).pos.y
  38.  
  39.     pendulum(0).ang = SIN(pendulum(0).angInc) * pendulum(0).angSize + _PI(.5)
  40.     pendulum(1).pos.x = tx1: pendulum(1).pos.y = ty1
  41.  
  42.     tx2 = COS(pendulum(1).ang) * pendulum(1).r + pendulum(1).pos.x
  43.     ty2 = SIN(pendulum(1).ang) * pendulum(1).r + pendulum(1).pos.y
  44.  
  45.     pendulum(1).ang = SIN(pendulum(1).angInc) * pendulum(0).angSize + pendulum(0).ang
  46.  
  47.     g = SIN(pendulum(0).angInc) * 128 + 128
  48.     b = COS(pendulum(1).angInc) * 128 + 128
  49.  
  50.     _DEST tracer&
  51.     COLOR _RGBA(0, g, b, a)
  52.     IF f > 40 THEN
  53.         thickLine px, py, tx2, ty2, 2
  54.     END IF
  55.     _DEST 0
  56.     COLOR _RGB(255, 0, 0)
  57.  
  58.     LINE (pendulum(0).pos.x, pendulum(0).pos.y)-(tx1, ty1)
  59.     CircleFill tx1, ty1, 30, _RGB(50, 50, 50)
  60.     LINE (pendulum(1).pos.x, pendulum(1).pos.y)-(tx2, ty2)
  61.     CircleFill tx2, ty2, 30, _RGB(50, 50, 50)
  62.     _PUTIMAGE , tracer&
  63.     _LIMIT 60
  64.  
  65.     _DISPLAY
  66.     pendulum(0).angInc = pendulum(0).angInc + .03
  67.     pendulum(1).angInc = pendulum(1).angInc + .05
  68.     px = tx2: py = ty2
  69.  
  70.     f = f + 1
  71.  
  72. 'By Fellippe
  73. SUB thickLine (x1 AS SINGLE, y1 AS SINGLE, x2 AS SINGLE, y2 AS SINGLE, lineWeight%)
  74.     DIM a AS SINGLE, x0 AS SINGLE, y0 AS SINGLE
  75.     DIM prevDest AS LONG, prevColor AS _UNSIGNED LONG
  76.     STATIC colorSample AS LONG
  77.  
  78.     IF colorSample = 0 THEN
  79.         colorSample = _NEWIMAGE(1, 1, 32)
  80.     END IF
  81.  
  82.     prevDest = _DEST
  83.     prevColor = _DEFAULTCOLOR
  84.     _DEST colorSample
  85.     PSET (0, 0), prevColor
  86.     _DEST prevDest
  87.  
  88.     a = _ATAN2(y2 - y1, x2 - x1)
  89.     a = a + _PI / 2
  90.     x0 = 0.5 * lineWeight% * COS(a)
  91.     y0 = 0.5 * lineWeight% * SIN(a)
  92.  
  93.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), colorSample TO(x1 - x0, y1 - y0)-(x1 + x0, y1 + y0)-(x2 + x0, y2 + y0), , _SMOOTH
  94.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), colorSample TO(x1 - x0, y1 - y0)-(x2 + x0, y2 + y0)-(x2 - x0, y2 - y0), , _SMOOTH
  95.  
  96.  
  97. SUB CircleFill (CX AS LONG, CY AS LONG, R AS LONG, C AS _UNSIGNED LONG)
  98.     'This sub from here: http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=1848.msg17254#msg17254
  99.     DIM Radius AS LONG
  100.     DIM RadiusError AS LONG
  101.     DIM X AS LONG
  102.     DIM Y AS LONG
  103.  
  104.     Radius = ABS(R)
  105.     RadiusError = -Radius
  106.     X = Radius
  107.     Y = 0
  108.  
  109.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  110.  
  111.     ' Draw the middle span here so we don't draw it twice in the main loop,
  112.     ' which would be a problem with blending turned on.
  113.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  114.  
  115.     WHILE X > Y
  116.  
  117.         RadiusError = RadiusError + Y * 2 + 1
  118.  
  119.         IF RadiusError >= 0 THEN
  120.  
  121.             IF X <> Y + 1 THEN
  122.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  123.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  124.             END IF
  125.  
  126.             X = X - 1
  127.             RadiusError = RadiusError - X * 2
  128.  
  129.         END IF
  130.  
  131.         Y = Y + 1
  132.  
  133.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  134.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  135.  
  136.     WEND
  137.  
  138.  
  139. 'taken from p5js.bas
  140. 'https://bit.ly/p5jsbas
  141. FUNCTION p5random! (mn!, mx!)
  142.     IF mn! > mx! THEN
  143.         SWAP mn!, mx!
  144.     END IF
  145.     p5random! = RND * (mx! - mn!) + mn!
  146.  
  147.  
Screenshot_1.png
* Screenshot_1.png (Filesize: 19.71 KB, Dimensions: 801x623, Views: 509)
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 _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Double Pendulum
« Reply #1 on: March 04, 2018, 03:34:02 pm »
Nice work, really nice effect.  Remind me of this program:

Code: QB64: [Select]
  1. defsng a-z
  2. pi = 3.141593
  3. r = 100
  4. a1 = -pi/2
  5. a2 = -pi/2
  6. h=0.001
  7. m=1000
  8. g=1000
  9. sw = 640
  10. sh = 480
  11.  
  12. p1 = _newimage(sw,sh,12)
  13. screen _newimage(sw,sh,12)
  14.  
  15.         a1p = (6/(m*r^2))*(2*m*v1-3*cos(a1-a2)*m*v2)/(16-9*(cos(a1-a2))^2)
  16.         a1k1 = a1p
  17.         a1k2 = a1p + h*a1k1/2
  18.         a1k3 = a1p + h*a1k2/2
  19.         a1k4 = a1p + h*a1k3
  20.         a2p = (6/(m*r^2))*(8*m*v2-3*cos(a1-a2)*m*v1)/(16-9*(cos(a1-a2))^2)
  21.         a2k1 = a2p
  22.         a2k2 = a2p + h*a2k1/2
  23.         a2k3 = a2p + h*a2k2/2
  24.         a2k4 = a2p + h*a2k3
  25.         na1=a1+h*(a1k1+2*a1k2+2*a1k3+a1k4)/6
  26.         na2=a2+h*(a2k1+2*a2k2+2*a2k3+a2k4)/6
  27.         v1p = -0.5*r^2*(a1p*a2p*sin(a1-a2)+3*g*sin(a1)/r)
  28.         v1k1 = v1p
  29.         v1k2 = v1p + h*v1k1/2
  30.         v1k3 = v1p + h*v1k2/2
  31.         v1k4 = v1p + h*v1k3
  32.         v2p = -0.5*r^2*(-a1p*a2p*sin(a1-a2)+g*sin(a2)/r)
  33.         v2k1 = v2p
  34.         v2k2 = v2p + h*v2k1/2
  35.         v2k3 = v2p + h*v2k2/2
  36.         v2k4 = v2p + h*v2k3
  37.         nv1=v1+h*(v1k1+2*v1k2+2*v1k3+v1k4)/6
  38.         nv2=v2+h*(v2k1+2*v2k2+2*v2k3+v2k4)/6
  39.         a1=na1
  40.         a2=na2
  41.         v1=nv1
  42.         v2=nv2
  43.  
  44.         _dest p1
  45.         pset (sw/2+r*cos(a1-pi/2)+r*cos(a2-pi/2), sh/2-r*sin(a1-pi/2)-r*sin(a2-pi/2)),12
  46.         _dest 0
  47.         _putimage, p1
  48.  
  49.         line (sw/2, sh/2)-step(r*cos(a1-pi/2), -r*sin(a1-pi/2))
  50.         circle step(0,0),5
  51.         line -step(r*cos(a2-pi/2), -r*sin(a2-pi/2))
  52.         circle step(0,0),5
  53.  
  54.         _limit 200
  55.         _display
  56.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Double Pendulum
« Reply #2 on: March 04, 2018, 05:21:03 pm »
Nice V,

Ashish brings out the best in us. :)

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Double Pendulum
« Reply #3 on: March 05, 2018, 04:22:40 am »
@v
Wow! Cool! Your version is very much accurate. :)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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