Author Topic: A mod of Archimedes Spiral  (Read 4798 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
A mod of Archimedes Spiral
« on: September 17, 2019, 01:01:16 pm »
I just did this for another forum doing an Archimedes Spiral thread :D
Code: QB64: [Select]
  1. _TITLE "Draw Spinner and Web" 'B+ started 2019-06-15
  2. ' Draw Spinner and web mod 2019-09-17
  3. DIM sc&, sx, sy, sh 'spider stuff
  4. DIM a, tn, ta, tcx, tcy, tr, tx, ty, tdx, tdy, oldx, oldy 'web stuff
  5.  
  6.  
  7. SCREEN _NEWIMAGE(xmax, ymax, 32)
  8. '_SCREENMOVE 300, 20
  9.  
  10. _DEST sc&
  11. tn = 13: ta = _PI(2 / 13): tcx = .75 * xmax: tcy = .33 * ymax
  12. FOR a = 1 TO tn
  13.     LINE (tcx + xmax * COS(a * ta), tcy + xmax * SIN(a * ta))-(tcx + xmax * COS(a * ta + _PI), tcy + xmax * SIN(a * ta + _PI)), &H88FFFFFF
  14.  
  15. tx = tcx: ty = tcy: tdx = 5: tdy = 0
  16. sx = tcx: sy = tcy
  17. a = a + ta: tr = tr + ta
  18. tx = tcx + tr * COS(a): ty = tcy + tr * SIN(a)
  19. sh = _ATAN2(ty - sy, tx - sx)
  20. tdx = 1 * COS(sh): tdy = 1 * SIN(sh)
  21.     'update web
  22.     IF ((sx - tx) ^ 2 + (sy - ty) ^ 2) ^ .5 < 5 THEN 'setup next target x, y nad new spider heading
  23.         oldx = sx: oldy = sy
  24.         sx = tx: sy = ty
  25.         _DEST sc&
  26.         LINE (oldx, oldy)-(sx, sy), &H66FFFFFF
  27.         _DEST 0
  28.         a = a + ta: tr = tr + 3 * ta
  29.         tx = tcx + tr * COS(a): ty = tcy + tr * SIN(a)
  30.         sh = _ATAN2(ty - sy, tx - sx)
  31.         tdx = 1 * COS(sh): tdy = 1 * SIN(sh)
  32.     ELSE
  33.         'save image wo spider
  34.         oldx = sx: oldy = sy
  35.         sx = sx + tdx: sy = sy + tdy
  36.         _DEST sc&
  37.         LINE (oldx, oldy)-(sx, sy), &H66FFFFFF
  38.         _DEST 0
  39.     END IF
  40.     _PUTIMAGE , sc&
  41.     '_PRINTSTRING (500, 200), STR$(tx) + STR$(ty) + STR$(tdx) + STR$(tdy) + STR$(sh)
  42.     'place spider
  43.     drawSpinner sx, sy, 1, sh, &H88221100
  44.     _DISPLAY
  45.     _LIMIT 30
  46.  
  47.  
  48. SUB drawSpinner (x AS INTEGER, y AS INTEGER, scale AS SINGLE, heading AS SINGLE, c AS _UNSIGNED LONG)
  49.     DIM x1, x2, x3, x4, y1, y2, y3, y4, r, a, a1, a2, lg, d, rd, red, blue, green
  50.     STATIC switch AS INTEGER
  51.     switch = switch + 2
  52.     switch = switch MOD 16 + 1
  53.     red = _RED32(c): green = _GREEN32(c): blue = _BLUE32(c)
  54.     r = 10 * scale
  55.     x1 = x + r * COS(heading): y1 = y + r * SIN(heading)
  56.     r = 2 * r 'lg lengths
  57.     FOR lg = 1 TO 8
  58.         IF lg < 5 THEN
  59.             a = heading + .9 * lg * _PI(1 / 5) + (lg = switch) * _PI(1 / 10)
  60.         ELSE
  61.             a = heading - .9 * (lg - 4) * _PI(1 / 5) - (lg = switch) * _PI(1 / 10)
  62.         END IF
  63.         x2 = x1 + r * COS(a): y2 = y1 + r * SIN(a)
  64.         drawLink x1, y1, 3 * scale, x2, y2, 2 * scale, _RGB32(red + 20, green + 10, blue + 5)
  65.         IF lg = 1 OR lg = 2 OR lg = 7 OR lg = 8 THEN d = -1 ELSE d = 1
  66.         a1 = a + d * _PI(1 / 12)
  67.         x3 = x2 + r * 1.5 * COS(a1): y3 = y2 + r * 1.5 * SIN(a1)
  68.         drawLink x2, y2, 2 * scale, x3, y3, scale, _RGB32(red + 35, green + 17, blue + 8)
  69.         rd = INT(RND * 8) + 1
  70.         a2 = a1 + d * _PI(1 / 8) * rd / 8
  71.         x4 = x3 + r * 1.5 * COS(a2): y4 = y3 + r * 1.5 * SIN(a2)
  72.         drawLink x3, y3, scale, x4, y4, scale, _RGB32(red + 50, green + 25, blue + 12)
  73.     NEXT
  74.     r = r * .5
  75.     fcirc x1, y1, r, _RGB32(red - 20, green - 10, blue - 5)
  76.     x2 = x1 + (r + 1) * COS(heading - _PI(1 / 12)): y2 = y1 + (r + 1) * SIN(heading - _PI(1 / 12))
  77.     fcirc x2, y2, r * .2, &HFF000000
  78.     x2 = x1 + (r + 1) * COS(heading + _PI(1 / 12)): y2 = y1 + (r + 1) * SIN(heading + _PI(1 / 12))
  79.     fcirc x2, y2, r * .2, &HFF000000
  80.     r = r * 2
  81.     x1 = x + r * .9 * COS(heading + _PI): y1 = y + r * .9 * SIN(heading + _PI)
  82.     TiltedEllipseFill 0, x1, y1, r, .7 * r, heading + _PI, _RGB32(red, green, blue)
  83.  
  84. SUB drawLink (x1, y1, r1, x2, y2, r2, c AS _UNSIGNED LONG)
  85.     DIM a, a1, a2, x3, x4, x5, x6, y3, y4, y5, y6
  86.     a = _ATAN2(y2 - y1, x2 - x1)
  87.     a1 = a + _PI(1 / 2)
  88.     a2 = a - _PI(1 / 2)
  89.     x3 = x1 + r1 * COS(a1): y3 = y1 + r1 * SIN(a1)
  90.     x4 = x1 + r1 * COS(a2): y4 = y1 + r1 * SIN(a2)
  91.     x5 = x2 + r2 * COS(a1): y5 = y2 + r2 * SIN(a1)
  92.     x6 = x2 + r2 * COS(a2): y6 = y2 + r2 * SIN(a2)
  93.     fquad x3, y3, x4, y4, x5, y5, x6, y6, c
  94.     fcirc x1, y1, r1, c
  95.     fcirc x2, y2, r2, c
  96.  
  97. 'need 4 non linear points (not all on 1 line) list them clockwise so x2, y2 is opposite of x4, y4
  98. SUB fquad (x1 AS INTEGER, y1 AS INTEGER, x2 AS INTEGER, y2 AS INTEGER, x3 AS INTEGER, y3 AS INTEGER, x4 AS INTEGER, y4 AS INTEGER, c AS _UNSIGNED LONG)
  99.     ftri x1, y1, x2, y2, x4, y4, c
  100.     ftri x3, y3, x4, y4, x1, y1, c
  101.  
  102. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  103.     DIM a&
  104.     a& = _NEWIMAGE(1, 1, 32)
  105.     _DEST a&
  106.     PSET (0, 0), K
  107.     _DEST 0
  108.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  109.     _FREEIMAGE a& '<<< this is important!
  110.  
  111. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  112.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  113.     DIM X AS INTEGER, Y AS INTEGER
  114.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  115.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  116.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  117.     WHILE X > Y
  118.         RadiusError = RadiusError + Y * 2 + 1
  119.         IF RadiusError >= 0 THEN
  120.             IF X <> Y + 1 THEN
  121.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  122.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  123.             END IF
  124.             X = X - 1
  125.             RadiusError = RadiusError - X * 2
  126.         END IF
  127.         Y = Y + 1
  128.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  129.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  130.     WEND
  131.  
  132. SUB TiltedEllipseFill (destHandle&, x0, y0, a, b, ang, c AS _UNSIGNED LONG)
  133.     DIM max AS INTEGER, mx2 AS INTEGER, i AS INTEGER, j AS INTEGER, k AS SINGLE, lasti AS SINGLE, lastj AS SINGLE
  134.     DIM prc AS _UNSIGNED LONG, tef AS LONG
  135.     prc = _RGB32(255, 255, 255, 255)
  136.     IF a > b THEN max = a + 1 ELSE max = b + 1
  137.     mx2 = max + max
  138.     tef = _NEWIMAGE(mx2, mx2)
  139.     _DEST tef
  140.     _SOURCE tef 'point wont read without this!
  141.     FOR k = 0 TO 6.2832 + .05 STEP .1
  142.         i = max + a * COS(k) * COS(ang) + b * SIN(k) * SIN(ang)
  143.         j = max + a * COS(k) * SIN(ang) - b * SIN(k) * COS(ang)
  144.         IF k <> 0 THEN
  145.             LINE (lasti, lastj)-(i, j), prc
  146.         ELSE
  147.             PSET (i, j), prc
  148.         END IF
  149.         lasti = i: lastj = j
  150.     NEXT
  151.     DIM xleft(mx2) AS INTEGER, xright(mx2) AS INTEGER, x AS INTEGER, y AS INTEGER
  152.     FOR y = 0 TO mx2
  153.         x = 0
  154.         WHILE POINT(x, y) <> prc AND x < mx2
  155.             x = x + 1
  156.         WEND
  157.         xleft(y) = x
  158.         WHILE POINT(x, y) = prc AND x < mx2
  159.             x = x + 1
  160.         WEND
  161.         WHILE POINT(x, y) <> prc AND x < mx2
  162.             x = x + 1
  163.         WEND
  164.         IF x = mx2 THEN xright(y) = xleft(y) ELSE xright(y) = x
  165.     NEXT
  166.     _DEST destHandle&
  167.     FOR y = 0 TO mx2
  168.         IF xleft(y) <> mx2 THEN LINE (xleft(y) + x0 - max, y + y0 - max)-(xright(y) + x0 - max, y + y0 - max), c, BF
  169.     NEXT
  170.     _FREEIMAGE tef
  171.  
  172.  

Arachnid Spirals.PNG
* Arachnid Spirals.PNG (Filesize: 67.83 KB, Dimensions: 1297x767, Views: 265)

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: A mod of Archimedes Spiral
« Reply #1 on: September 19, 2019, 04:28:43 am »
Thanks B+Mark !
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: A mod of Archimedes Spiral
« Reply #2 on: September 19, 2019, 05:52:16 am »
It is very good.  A possible improvement would be to get the rear of the spider's abdomen (where the spinnerets are) to trace out the forming web, just to ultimately please the arachnologists.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: A mod of Archimedes Spiral
« Reply #3 on: September 19, 2019, 08:54:18 am »
Wow! Spider looks realistic.
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 Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: A mod of Archimedes Spiral
« Reply #4 on: September 19, 2019, 10:48:49 am »
Nice program, B+! Super!
QB64 is the best!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A mod of Archimedes Spiral
« Reply #5 on: September 19, 2019, 12:02:56 pm »
Thanks guys,

You know spiders get into everything, my dreams, my peace of mind, my



Oh no!!!

spiders get into every.PNG
* spiders get into every.PNG (Filesize: 59.69 KB, Dimensions: 1154x665, Views: 256)