Author Topic: Pattern Challenge 2  (Read 6547 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Pattern Challenge 2
« Reply #15 on: July 14, 2019, 07:36:02 am »
Fantastic, Ashish!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Pattern Challenge 2
« Reply #16 on: July 14, 2019, 10:22:51 am »
I've figured out how to win this challenge... Just say: "KNOT IT!"

@Ashish: That's the worst shoe lace tying tutorial I've ever seen.

Pete :D
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: Pattern Challenge 2
« Reply #17 on: July 14, 2019, 11:19:00 am »
Hi Ashish,

I still have that from:
Quote
'Coded in QB64 by Ashish on 9 March, 2018
'http://paulbourke.net/geometry/knots/
_TITLE "3D Knot [Press space for next knot]"

Still looks great!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Pattern Challenge 2
« Reply #18 on: July 19, 2019, 04:28:50 pm »
No more eyeballing, let the program do the work!

Alas, the program is extremely temperamental. But still able to graph multiple Angle Functions:
Code: QB64: [Select]
  1. _TITLE "Knot Function of Angle" ' B+ 2019-07-18
  2. ' another attempt to get intersect points by computer rather than eyeball
  3.  
  4. CONST xmax = 740, ymax = 740, pi = 3.14159265
  5. SCREEN _NEWIMAGE(xmax, ymax, 32)
  6. DIM x0, y0, r0, rThick, rFill 'for knot  r is for equation, r1 is for thickness of rings, r2 is fill thicknes
  7.  
  8. x0 = xmax / 2: y0 = ymax / 2: r0 = 120: rThick = 4: rFill = 2
  9. x0 = xmax / 2: y0 = ymax / 2: r0 = 100: rThick = 8: rFill = 4
  10. knot xmax / 4, ymax / 4, 60, 15, 8, &HFF009900, &HFF0000FF, 1
  11. knot 3 * xmax / 4, ymax / 4 + 35, 100, 8, 4, &HFFFFFF00, &HFFFF0000, 2
  12. knot xmax / 4, ymax * 3 / 4, 98, 4, 1, &HFFFFFFFF, &HFF009900, 3
  13. knot xmax * 3 / 4, ymax * 3 / 4, 90, 4, 2, &HFF0000FF, &HFF00BBBB, 4
  14.  
  15. SUB knot (xc, yc, r, r1, r2, border AS _UNSIGNED LONG, fill AS _UNSIGNED LONG, functionNum)
  16.  
  17.     DIM a, x AS INTEGER, y AS INTEGER, stepper 'for plotting
  18.  
  19.     stepper = 1 / (2 * pi * r) 'how close to step   'orig 2*pi
  20.     'first pass draw in outline
  21.     FOR a = 0 TO 2 * pi - 2 * stepper STEP stepper 'collect data
  22.         SELECT CASE functionNum
  23.             CASE 1: FofA xc, yc, r, a, x, y
  24.             CASE 2: FofA2 xc, yc, r, a, x, y
  25.             CASE 3: FofA3 xc, yc, r, a, x, y
  26.             CASE 4: FofA4 xc, yc, r, a, x, y
  27.         END SELECT
  28.         IF counts(x, y) <> 0 THEN
  29.             IF ABS(ang(x, y) - a) > pi / 12 THEN 'not too close   pi/12 orig
  30.                 counts(x, y) = counts(x, y) + 1 'hopefully
  31.                 ang(x, y) = a 'update xy position with latest angle
  32.             END IF
  33.         ELSE
  34.             counts(x, y) = 1: ang(x, y) = a
  35.             fcirc x, y, r1, border
  36.         END IF
  37.     NEXT
  38.  
  39.     DIM intersects(100, 1) AS INTEGER, ii AS INTEGER, i, flag
  40.     'next pass draw in fill
  41.     FOR y = 0 TO ymax
  42.         FOR x = 0 TO xmax
  43.             IF counts(x, y) THEN fcirc x, y, r2, fill
  44.             IF counts(x, y) > 1 THEN
  45.                 IF ii > 0 THEN
  46.                     flag = 0
  47.                     FOR i = 0 TO ii
  48.                         IF ((intersects(i, 0) - x) ^ 2 + (intersects(i, 1) - y) ^ 2) ^ .5 < 2 THEN flag = 1: EXIT FOR
  49.                     NEXT
  50.                     IF flag = 0 THEN
  51.                         intersects(ii, 0) = x
  52.                         intersects(ii, 1) = y
  53.                         ii = ii + 1
  54.                     END IF
  55.                 ELSE
  56.                     intersects(ii, 0) = x
  57.                     intersects(ii, 1) = y
  58.                     ii = ii + 1
  59.                 END IF
  60.             END IF
  61.         NEXT
  62.     NEXT
  63.  
  64.     'For each intersect there is am angle it goes over and an angle it goes under
  65.     ' looking at figure from angle = 0, it alternates over, under, over, under....;
  66.     ' its the over angles that need the bridges. How to find the over angles?
  67.  
  68.     DIM lasta, toggle, b, xx AS INTEGER, yy AS INTEGER
  69.     FOR a = 0 TO 2 * pi - 2 * stepper STEP stepper 'collect data
  70.         SELECT CASE functionNum
  71.             CASE 1: FofA xc, yc, r, a, x, y
  72.             CASE 2: FofA2 xc, yc, r, a, x, y
  73.             CASE 3: FofA3 xc, yc, r, a, x, y
  74.             CASE 4: FofA4 xc, yc, r, a, x, y
  75.         END SELECT
  76.  
  77.         FOR i = 0 TO ii - 1
  78.             IF x = intersects(i, 0) AND y = intersects(i, 1) AND ABS(a - lasta) > pi / 24 THEN
  79.                 toggle = (toggle + 1) MOD 2
  80.                 IF toggle THEN 'save every other angle as positive for the overpasses
  81.                     'build a bridge
  82.                     FOR b = a - 3 * pi / 96 TO a + 3 * pi / 96 STEP stepper
  83.                         SELECT CASE functionNum
  84.                             CASE 1: FofA xc, yc, r, b, xx, yy
  85.                             CASE 2: FofA2 xc, yc, r, b, xx, yy
  86.                             CASE 3: FofA3 xc, yc, r, b, xx, yy
  87.                             CASE 4: FofA4 xc, yc, r, b, xx, yy
  88.                         END SELECT
  89.                         fcirc xx, yy, r1, border
  90.                     NEXT
  91.                     FOR b = a - 4 * pi / 96 TO a + 4 * pi / 96 STEP stepper
  92.                         SELECT CASE functionNum
  93.                             CASE 1: FofA xc, yc, r, b, xx, yy
  94.                             CASE 2: FofA2 xc, yc, r, b, xx, yy
  95.                             CASE 3: FofA3 xc, yc, r, b, xx, yy
  96.                             CASE 4: FofA4 xc, yc, r, b, xx, yy
  97.                         END SELECT
  98.                         fcirc xx, yy, r2, fill
  99.                     NEXT
  100.                 END IF
  101.                 lasta = a
  102.                 EXIT FOR
  103.             END IF
  104.         NEXT
  105.     NEXT
  106.  
  107. SUB FofA (xc, yc, r, a AS SINGLE, xReturn AS INTEGER, yReturn AS INTEGER)
  108.     xReturn = INT(xc + r * (COS(a) + COS(4 * a) / .7 + SIN(2 * a) / 12))
  109.     yReturn = INT(yc + r * (SIN(a) + SIN(4 * a) / .7 + COS(2 * a) / 12))
  110.  
  111. SUB FofA2 (xc, yc, r, a AS SINGLE, xReturn AS INTEGER, yReturn AS INTEGER)
  112.     xReturn = xc + r * (COS(a) + COS(5 * a) / 1.6 + SIN(2 * a) / 3)
  113.     yReturn = yc + r * (SIN(a) + SIN(5 * a) / 1.6 + COS(2 * a) / 3)
  114.  
  115. SUB FofA3 (xc, yc, r, a AS SINGLE, xReturn AS INTEGER, yReturn AS INTEGER)
  116.     xReturn = xc + r * (COS(a) + COS(7 * a) / 2 + SIN(2 * a) / 3)
  117.     yReturn = yc + r * (SIN(a) + SIN(7 * a) / 2 + COS(2 * a) / 3)
  118.  
  119. 'for function number 4, the following works best
  120.  
  121. SUB FofA4 (xc, yc, r, a AS SINGLE, xReturn AS INTEGER, yReturn AS INTEGER)
  122.     xReturn = xc + r * (COS(a) + COS(4 * a) / 2.9 + SIN(6 * a) / 2.1) '2.6 2.1 works
  123.     yReturn = yc + r * (SIN(a) + SIN(4 * a) / 2.9 + COS(6 * a) / 2.1) ' 2.7 2.2 better? 2.8 2.3 OK too  2.9 2.1 is it
  124.  
  125. '''''' ================= failed: bridges too close or passes do not alternate , too many loops?
  126. '''''  Toggle commented function blocks to see some fancy loops tried
  127.  
  128.  
  129. 'SUB FofA4 (xc, yc, r, a AS SINGLE, xReturn AS INTEGER, yReturn AS INTEGER)
  130. '    xReturn = xc + r * (COS(a) + COS(9 * a) / 2.5 + SIN(3 * a) / 2.6)
  131. '    yReturn = yc + r * (SIN(a) + SIN(9 * a) / 2.5 + COS(3 * a) / 2.6)
  132. 'END SUB
  133.  
  134. 'SUB FofA4 (xc, yc, r, a AS SINGLE, xReturn AS INTEGER, yReturn AS INTEGER)
  135. '    xReturn = xc + r * (COS(a) + COS(3 * a) / 2 + SIN(11 * a) / 2.7)
  136. '    yReturn = yc + r * (SIN(a) + SIN(3 * a) / 2 + COS(11 * a) / 2.7)
  137. 'END SUB
  138.  
  139. 'SUB FofA4 (xc, yc, r, a AS SINGLE, xReturn AS INTEGER, yReturn AS INTEGER)
  140. '    xReturn = xc + r * (COS(a) + COS(9 * a) / 2 + SIN(5 * a) / 2.5)
  141. '    yReturn = yc + r * (SIN(a) + SIN(9 * a) / 2 + COS(5 * a) / 2.5)
  142. 'END SUB
  143.  
  144. 'SUB FofA4 (xc, yc, r, a AS SINGLE, xReturn AS INTEGER, yReturn AS INTEGER)
  145. '    xReturn = xc + r * (COS(a) + COS(5 * a) / 2.7 + SIN(6 * a) / 2)
  146. '    yReturn = yc + r * (SIN(a) + SIN(5 * a) / 2.7 + COS(6 * a) / 2)
  147. 'END SUB
  148.  
  149. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  150.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  151.     DIM X AS INTEGER, Y AS INTEGER
  152.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  153.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  154.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  155.     WHILE X > Y
  156.         RadiusError = RadiusError + Y * 2 + 1
  157.         IF RadiusError >= 0 THEN
  158.             IF X <> Y + 1 THEN
  159.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  160.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  161.             END IF
  162.             X = X - 1
  163.             RadiusError = RadiusError - X * 2
  164.         END IF
  165.         Y = Y + 1
  166.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  167.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  168.     WEND
  169.  
  170.  

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Pattern Challenge 2
« Reply #19 on: July 20, 2019, 08:47:12 am »
Fantastic Job Bplus!
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 TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
« Last Edit: July 20, 2019, 02:14:16 pm by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Pattern Challenge 2
« Reply #21 on: July 21, 2019, 09:16:48 am »
Thanks guys!

TempodiBasic, I thought I said, "No more eyeballing,.." :D

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Programming isn't difficult, only it's  consuming time and coffee

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Pattern Challenge 2
« Reply #23 on: July 21, 2019, 02:37:33 pm »
That's great, TempodiBasic. This view of women is an innovative connection for me.