Author Topic: Star Patterns  (Read 5578 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.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Star Patterns
« on: April 17, 2019, 07:54:11 am »
Watching Shiffmans Islamic Star Patterns Coding Challenge #54.1, I came up with much shorter method for Rectangles that might be generalized to any regular polygon.
&index=70

Instead of sliders, you can use the up / down arrow keys to increase / decrease the inner radius of the 4 point star within the rectangle and use the left / right arrows to decrease / increase the distance from side midpoints that the lines of star end on rectangle tile.

Code: QB64: [Select]
  1. _TITLE "Rectangular Star Tiling: Arrow up/down inner radius, Arrow left/right midpoint distance" 'B+ 2019-04-16
  2. ' Trying to duplicate results shown here by Daniel Shiffman
  3. ' [youtube]https://www.youtube.com/watch?v=sJ6pMLp_IaI&list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH[/youtube]&index=70
  4. ' but using a completely different method for drawing the tile
  5.  
  6. CONST xmax = 800
  7. CONST ymax = 600
  8. SCREEN _NEWIMAGE(xmax, ymax, 32)
  9.  
  10. wd = 100: ht = 100: rd = 20: dm = 20
  11. WHILE _KEYDOWN(27) = 0
  12.     KH& = _KEYHIT
  13.     SELECT CASE KH&
  14.         CASE 18432 'up
  15.             IF rd + 1 <= min(wd / 2, ht / 2) THEN rd = rd + 1
  16.         CASE 20480 'down
  17.             IF rd - 1 >= 0 THEN rd = rd - 1
  18.         CASE 19200 'left
  19.             IF dm - 1 >= 0 THEN dm = dm - 1
  20.         CASE 19712 'right
  21.             IF dm + 1 <= min(wd / 2, ht / 2) THEN dm = dm + 1
  22.     END SELECT
  23.     CLS
  24.     FOR y = 0 TO ymax STEP ht
  25.         FOR x = 0 TO xmax STEP wd
  26.             drawStarTile x, y, wd, ht, rd, dm
  27.         NEXT
  28.     NEXT
  29.     T$ = "Rectangular Star Tiling: Arrow up/down inner radius = " + _TRIM$(STR$(rd)) + ", Arrow left/right midpoint distance = " + _TRIM$(STR$(dm))
  30.     _TITLE T$
  31.     _DISPLAY
  32.     _LIMIT 60
  33.  
  34. FUNCTION min (n1, n2)
  35.     IF n1 < n2 THEN min = n1 ELSE min = n2
  36.  
  37.  
  38. SUB drawStarTile (x, y, w, h, r, d)
  39.     'this just draws 8 lines from 4 inner circle points to 2 places on each side of rectangle
  40.  
  41.     'some helpers
  42.     pd2 = _PI / 2: pd4 = _PI / 4 'pi
  43.     mpx = x + w / 2: mpy = y + h / 2 'rect midpoint
  44.  
  45.     'inner circle points
  46.     icx1 = mpx + r * COS(pd4)
  47.     icy1 = mpy + r * SIN(pd4)
  48.     icx2 = mpx + r * COS(pd2 + pd4)
  49.     icy2 = mpy + r * SIN(pd2 + pd4)
  50.     icx3 = mpx + r * COS(2 * pd2 + pd4)
  51.     icy3 = mpy + r * SIN(2 * pd2 + pd4)
  52.     icx4 = mpx + r * COS(3 * pd2 + pd4)
  53.     icy4 = mpy + r * SIN(3 * pd2 + pd4)
  54.  
  55.     'outer rectangle points, yeah some variables are redundant
  56.     'right side
  57.     x1 = x + w
  58.     y1 = y + h / 2 - d
  59.     x2 = x + w
  60.     y2 = y + h / 2 + d
  61.     'bottom
  62.     x3 = x + w / 2 + d
  63.     y3 = y + h
  64.     x4 = x + w / 2 - d
  65.     y4 = y + h
  66.     'left
  67.     x5 = x
  68.     y5 = y + h / 2 + d
  69.     x6 = x
  70.     y6 = y + h / 2 - d
  71.     'and top
  72.     x7 = x + w / 2 - d
  73.     y7 = y
  74.     x8 = x + w / 2 + d
  75.     y8 = y
  76.  
  77.     'draw rect
  78.     LINE (x, y)-STEP(w, h), _RGBA(255, 0, 0, 50), B 'here is rect
  79.  
  80.     'draw star
  81.     LINE (icx1, icy1)-(x4, y4)
  82.     LINE (icx1, icy1)-(x1, y1)
  83.     LINE (icx2, icy2)-(x3, y3)
  84.     LINE (icx2, icy2)-(x6, y6)
  85.     LINE (icx3, icy3)-(x5, y5)
  86.     LINE (icx3, icy3)-(x8, y8)
  87.     LINE (icx4, icy4)-(x7, y7)
  88.     LINE (icx4, icy4)-(x2, y2)
  89.  
  90.  

A fun bit of code.

I think this method could be generalized for any regular polygon but you can only tile with 3, 4 and 6 sided polygons (I think).

« Last Edit: April 17, 2019, 07:56:27 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Star Patterns
« Reply #1 on: April 17, 2019, 01:29:31 pm »
Yes! generalized to draw any N sided polygon star tile and setting up the square grid again only diagonally:
Code: QB64: [Select]
  1. _TITLE "Regular Polygon Star Tiling" 'B+ 2019-04-17
  2. ' Trying to duplicate results shown here by Daniel Shiffman
  3. ' [youtube]https://www.youtube.com/watch?v=sJ6pMLp_IaI&list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH[/youtube]&index=70
  4. ' but using a completely different method for drawing the tile
  5. ' 2019-04-17 Yes! the star tile can be generalized to any N sided regular polygon!
  6.  
  7. CONST xmax = 800
  8. CONST ymax = 600
  9. SCREEN _NEWIMAGE(xmax, ymax, 32)
  10.  
  11. FOR i = 3 TO 9
  12.     CLS
  13.     PRINT i; " sided polygon, poly radius 200, inner star radius 60, dist from side midpoints 40."
  14.     drawRegPolyStar 400, 300, 200, i, 60, 40 'OK !
  15.     IF i = 9 THEN
  16.         INPUT "Press enter for a grid of square polygons ", wate$
  17.     ELSE
  18.         INPUT "Press enter for next ", wate$
  19.     END IF
  20.  
  21. 'grid height
  22. polyRadius = 100
  23. griddist = polyRadius * COS(0)
  24. rd = 10
  25. dm = 20
  26.  
  27. WHILE _KEYDOWN(27) = 0
  28.     KH& = _KEYHIT
  29.     SELECT CASE KH&
  30.         CASE 18432 'up
  31.             IF rd + 1 <= polyRadius - 2 THEN rd = rd + 1
  32.         CASE 20480 'down
  33.             IF rd - 1 >= 0 THEN rd = rd - 1
  34.         CASE 19200 'left
  35.             IF dm - 1 >= 0 THEN dm = dm - 1
  36.         CASE 19712 'right
  37.             IF dm + 1 <= SQR(2) * polyRadius / 2 THEN dm = dm + 1
  38.     END SELECT
  39.     CLS
  40.     xoff = 0
  41.     FOR y = 0 TO ymax STEP griddist
  42.         xoff = (xoff + 1) MOD 2
  43.         FOR x = 0 TO xmax STEP 2 * griddist
  44.             drawRegPolyStar x + xoff * griddist, y, polyRadius, 4, rd, dm
  45.         NEXT
  46.     NEXT
  47.  
  48.     T$ = "Square Star Tiling: Arrow up/down inner radius = " + _TRIM$(STR$(rd)) + ", Arrow left/right midpoint distance = " + _TRIM$(STR$(dm))
  49.     _TITLE T$
  50.     _DISPLAY
  51.     _LIMIT 10
  52.  
  53.  
  54. SUB drawRegPolyStar (cx, cy, pRadius, nSides, innerStarRadius, midPtDist)
  55.     DIM tilePtsX(1 TO nSides), tilePtsY(1 TO nSides)
  56.     DIM innerStarX(1 TO nSides), innerStarY(1 TO nSides)
  57.  
  58.     pA = _PI(2 / nSides)
  59.     FOR i = 1 TO nSides
  60.         tilePtsX(i) = cx + pRadius * COS(pA * i)
  61.         tilePtsY(i) = cy + pRadius * SIN(pA * i)
  62.         'on the same line the innerStar pts
  63.         innerStarX(i) = cx + innerStarRadius * COS(pA * i)
  64.         innerStarY(i) = cy + innerStarRadius * SIN(pA * i)
  65.         'CIRCLE (innerStarX(i), innerStarY(i)), 3, _RGB32(255, 255, 0)
  66.         'draw tile
  67.         IF i > 1 THEN
  68.             LINE (tilePtsX(i), tilePtsY(i))-(tilePtsX(i - 1), tilePtsY(i - 1)), _RGB32(255, 0, 0, 200)
  69.             IF i = nSides THEN
  70.                 LINE (tilePtsX(i), tilePtsY(i))-(tilePtsX(1), tilePtsY(1)), _RGB32(255, 0, 0, 200)
  71.             END IF
  72.         END IF
  73.         '_DELAY .5
  74.     NEXT
  75.  
  76.     'from each innerStarPt 2 lines connect to side midpoints
  77.     'lets calc all the midpoints +/- midPtDist
  78.     DIM mpdX(1 TO 2 * nSides), mpdY(1 TO 2 * nSides)
  79.     FOR i = 1 TO nSides
  80.         IF i - 1 = 0 THEN ei = nSides ELSE ei = i - 1
  81.         mx = (tilePtsX(ei) + tilePtsX(i)) / 2
  82.         my = (tilePtsY(ei) + tilePtsY(i)) / 2
  83.         'check
  84.         'CIRCLE (mx, my), 2, _RGB32(0, 0, 255)
  85.         '_DELAY .5
  86.  
  87.         'from each mx, my we need a point midPtDist along the angle from mx, my to the ei index point
  88.         a = _ATAN2(tilePtsY(ei) - my, tilePtsX(ei) - mx)
  89.         mdx = mx + midPtDist * COS(a)
  90.         mdy = my + midPtDist * SIN(a)
  91.         'the other point is 180 degrees in opposite direction
  92.         mdx2 = mx + midPtDist * COS(a - _PI)
  93.         mdy2 = my + midPtDist * SIN(a - _PI)
  94.         'check
  95.         'CIRCLE (mdx, mdy), 2, _RGB32(255, 255, 0)
  96.         'CIRCLE (mdx2, mdy2), 2, _RGB32(255, 0, 255)
  97.  
  98.         'OK store all these points for drawing lines later
  99.         mpdX(2 * i - 1) = mdx: mpdY(2 * i - 1) = mdy
  100.         mpdX(2 * i) = mdx2: mpdY(2 * i) = mdy2
  101.  
  102.     NEXT
  103.  
  104.     'from each point in inner star Radius draw 2 lines to the poly edges
  105.     FOR i = 1 TO nSides
  106.         'now figure the pattern: sequence maps are to 2*i +2 and to 2*i - 1
  107.         IF 2 * i + 2 > 2 * nSides THEN map = 2 * i + 2 - 2 * nSides ELSE map = 2 * i + 2
  108.         LINE (innerStarX(i), innerStarY(i))-(mpdX(map), mpdY(map))
  109.  
  110.         IF 2 * i - 1 < 1 THEN map = 2 * i - 1 + 2 * nSides ELSE map = 2 * i - 1
  111.         LINE (innerStarX(i), innerStarY(i))-(mpdX(map), mpdY(map))
  112.         '_DELAY .5
  113.     NEXT
  114.  
  115.  
  116.  

Some trouble setting up for hexagonal grid and allot of redundant calculation can be eliminated, probably by drawing one tile and placing a copy where ever needed or just sharing the arrays that contain the points calculations.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Star Patterns
« Reply #2 on: April 17, 2019, 08:27:48 pm »
Hexagonal Star Tiling:
Code: QB64: [Select]
  1. _TITLE "Hexagonal Star Tiling" 'B+ 2019-04-17
  2. ' Trying to duplicate results shown here by Daniel Shiffman
  3. ' [youtube]https://www.youtube.com/watch?v=sJ6pMLp_IaI&list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH[/youtube]&index=70
  4. ' but using a completely different method for drawing the tile
  5. ' 2019-04-17 Yes! the star tile can be generalized to any N sided regular polygon!
  6. ' 2019-04-17 This version try Hexagonal Tiling
  7.  
  8. CONST xmax = 800
  9. CONST ymax = 600
  10. SCREEN _NEWIMAGE(xmax, ymax, 32)
  11.  
  12. 'grid height
  13. polyRadius = 100
  14. gridheight = polyRadius * SQR(3) / 2
  15. rd = 10
  16. dm = 20
  17. WHILE _KEYDOWN(27) = 0
  18.     KH& = _KEYHIT
  19.     SELECT CASE KH&
  20.         CASE 18432 'up
  21.             IF rd + 1 <= polyRadius - 2 THEN rd = rd + 1
  22.         CASE 20480 'down
  23.             IF rd - 1 >= 0 THEN rd = rd - 1
  24.         CASE 19200 'left
  25.             IF dm - 1 >= 0 THEN dm = dm - 1
  26.         CASE 19712 'right
  27.             IF dm + 1 <= .5 * polyRadius THEN dm = dm + 1
  28.     END SELECT
  29.     CLS
  30.     xoff = 0
  31.     FOR y = 0 TO ymax + gridheight STEP gridheight
  32.         xoff = (xoff + 1) MOD 2
  33.         FOR x = 0 TO xmax STEP 3 * polyRadius
  34.             drawRegPolyStar x + xoff * 1.5 * polyRadius, y, polyRadius, 6, rd, dm
  35.         NEXT
  36.     NEXT
  37.     T$ = "Hexagonal Star Tiling: Arrow up/down inner radius = " + _TRIM$(STR$(rd)) + ", Arrow left/right midpoint distance = " + _TRIM$(STR$(dm))
  38.     _TITLE T$
  39.     _DISPLAY
  40.     _LIMIT 10
  41.  
  42. SUB drawRegPolyStar (cx, cy, pRadius, nSides, innerStarRadius, midPtDist)
  43.     DIM tilePtsX(1 TO nSides), tilePtsY(1 TO nSides)
  44.     DIM innerStarX(1 TO nSides), innerStarY(1 TO nSides)
  45.  
  46.     pA = _PI(2 / nSides)
  47.     FOR i = 1 TO nSides
  48.         tilePtsX(i) = cx + pRadius * COS(pA * i)
  49.         tilePtsY(i) = cy + pRadius * SIN(pA * i)
  50.         'on the same line the innerStar pts
  51.         innerStarX(i) = cx + innerStarRadius * COS(pA * i)
  52.         innerStarY(i) = cy + innerStarRadius * SIN(pA * i)
  53.         'CIRCLE (innerStarX(i), innerStarY(i)), 3, _RGB32(255, 255, 0)
  54.         'draw tile
  55.         IF i > 1 THEN
  56.             LINE (tilePtsX(i), tilePtsY(i))-(tilePtsX(i - 1), tilePtsY(i - 1)), _RGB32(255, 0, 0, 200)
  57.             IF i = nSides THEN
  58.                 LINE (tilePtsX(i), tilePtsY(i))-(tilePtsX(1), tilePtsY(1)), _RGB32(255, 0, 0, 200)
  59.             END IF
  60.         END IF
  61.         '_DELAY .5
  62.     NEXT
  63.  
  64.     'from each innerStarPt 2 lines connect to side midpoints
  65.     'lets calc all the midpoints +/- midPtDist
  66.     DIM mpdX(1 TO 2 * nSides), mpdY(1 TO 2 * nSides)
  67.     FOR i = 1 TO nSides
  68.         IF i - 1 = 0 THEN ei = nSides ELSE ei = i - 1
  69.         mx = (tilePtsX(ei) + tilePtsX(i)) / 2
  70.         my = (tilePtsY(ei) + tilePtsY(i)) / 2
  71.         'check
  72.         'CIRCLE (mx, my), 2, _RGB32(0, 0, 255)
  73.         '_DELAY .5
  74.  
  75.         'from each mx, my we need a point midPtDist along the angle from mx, my to the ei index point
  76.         a = _ATAN2(tilePtsY(ei) - my, tilePtsX(ei) - mx)
  77.         mdx = mx + midPtDist * COS(a)
  78.         mdy = my + midPtDist * SIN(a)
  79.         'the other point is 180 degrees in opposite direction
  80.         mdx2 = mx + midPtDist * COS(a - _PI)
  81.         mdy2 = my + midPtDist * SIN(a - _PI)
  82.         'check
  83.         'CIRCLE (mdx, mdy), 2, _RGB32(255, 255, 0)
  84.         'CIRCLE (mdx2, mdy2), 2, _RGB32(255, 0, 255)
  85.  
  86.         'OK store all these points for drawing lines later
  87.         mpdX(2 * i - 1) = mdx: mpdY(2 * i - 1) = mdy
  88.         mpdX(2 * i) = mdx2: mpdY(2 * i) = mdy2
  89.  
  90.     NEXT
  91.  
  92.     'from each point in inner star Radius draw 2 lines to the poly edges
  93.     FOR i = 1 TO nSides
  94.         'now figure the pattern: sequence maps are to 2*i +2 and to 2*i - 1
  95.         IF 2 * i + 2 > 2 * nSides THEN map = 2 * i + 2 - 2 * nSides ELSE map = 2 * i + 2
  96.         LINE (innerStarX(i), innerStarY(i))-(mpdX(map), mpdY(map))
  97.  
  98.         IF 2 * i - 1 < 1 THEN map = 2 * i - 1 + 2 * nSides ELSE map = 2 * i - 1
  99.         LINE (innerStarX(i), innerStarY(i))-(mpdX(map), mpdY(map))
  100.         '_DELAY .5
  101.     NEXT
  102.  
  103.  
  104.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Star Patterns
« Reply #3 on: April 17, 2019, 08:54:05 pm »
Draw one tile and rubber stamp the grid with the image to skip recalculations for every tile:
Code: QB64: [Select]
  1. _TITLE "Hexagonal Star Tiling 2" 'B+ 2019-04-17
  2. ' Trying to duplicate results shown here by Daniel Shiffman
  3. ' [youtube]https://www.youtube.com/watch?v=sJ6pMLp_IaI&list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH[/youtube]&index=70
  4. ' but using a completely different method for drawing the tile
  5. ' 2019-04-17 Yes! the star tile can be generalized to any N sided regular polygon!
  6. ' 2019-04-17 This version try Hexagonal Tiling.
  7. ' 2019-04-17 Hexagonal Star Tiling 2, prep one tile and rubber stamp the grid with image.
  8.  
  9. CONST xmax = 800
  10. CONST ymax = 600
  11. SCREEN _NEWIMAGE(xmax, ymax, 32)
  12. DIM SHARED tile&
  13.  
  14. 'grid height
  15. polyRadius = 100
  16. gridheight = polyRadius * SQR(3) / 2
  17.  
  18. rd = 10
  19. dm = 20
  20. prepTile polyRadius, 6, rd, dm
  21. WHILE _KEYDOWN(27) = 0
  22.     KH& = _KEYHIT
  23.     SELECT CASE KH&
  24.         CASE 18432 'up
  25.             IF rd + 1 <= polyRadius - 2 THEN rd = rd + 1: prepTile polyRadius, 6, rd, dm
  26.         CASE 20480 'down
  27.             IF rd - 1 >= 0 THEN rd = rd - 1: prepTile polyRadius, 6, rd, dm
  28.         CASE 19200 'left
  29.             IF dm - 1 >= 0 THEN dm = dm - 1: prepTile polyRadius, 6, rd, dm
  30.         CASE 19712 'right
  31.             IF dm + 1 <= .5 * polyRadius THEN dm = dm + 1: prepTile polyRadius, 6, rd, dm
  32.     END SELECT
  33.     CLS
  34.     xoff = 0
  35.     FOR y = -polyRadius TO ymax + gridheight STEP gridheight
  36.         xoff = (xoff + 1) MOD 2
  37.         FOR x = -polyRadius TO xmax STEP 3 * polyRadius
  38.             _PUTIMAGE (x + xoff * 1.5 * polyRadius, y), tile&, 0
  39.             'drawRegPolyStar x + xoff * 1.5 * polyRadius, y, polyRadius, 6, rd, dm
  40.         NEXT
  41.     NEXT
  42.     T$ = "Square Star Tiling: Arrow up/down inner radius = " + _TRIM$(STR$(rd)) + ", Arrow left/right midpoint distance = " + _TRIM$(STR$(dm))
  43.     _TITLE T$
  44.     _DISPLAY
  45.     _LIMIT 60
  46.  
  47. SUB prepTile (pRadius, nSides, innerStarRadius, midPtDist)
  48.     IF tile& THEN _FREEIMAGE tile&
  49.     tile& = _NEWIMAGE(2 * pRadius, 2 * pRadius, 32)
  50.     _DEST tile&
  51.     drawRegPolyStar pRadius, pRadius, pRadius, nSides, innerStarRadius, midPtDist
  52.     _DEST 0
  53.  
  54. SUB drawRegPolyStar (cx, cy, pRadius, nSides, innerStarRadius, midPtDist)
  55.     DIM tilePtsX(1 TO nSides), tilePtsY(1 TO nSides)
  56.     DIM innerStarX(1 TO nSides), innerStarY(1 TO nSides)
  57.  
  58.     pA = _PI(2 / nSides)
  59.     FOR i = 1 TO nSides
  60.         tilePtsX(i) = cx + pRadius * COS(pA * i)
  61.         tilePtsY(i) = cy + pRadius * SIN(pA * i)
  62.         'on the same line the innerStar pts
  63.         innerStarX(i) = cx + innerStarRadius * COS(pA * i)
  64.         innerStarY(i) = cy + innerStarRadius * SIN(pA * i)
  65.         'CIRCLE (innerStarX(i), innerStarY(i)), 3, _RGB32(255, 255, 0)
  66.         'draw tile
  67.         IF i > 1 THEN
  68.             LINE (tilePtsX(i), tilePtsY(i))-(tilePtsX(i - 1), tilePtsY(i - 1)), _RGB32(255, 0, 0, 200)
  69.             IF i = nSides THEN
  70.                 LINE (tilePtsX(i), tilePtsY(i))-(tilePtsX(1), tilePtsY(1)), _RGB32(255, 0, 0, 200)
  71.             END IF
  72.         END IF
  73.         '_DELAY .5
  74.     NEXT
  75.  
  76.     'from each innerStarPt 2 lines connect to side midpoints
  77.     'lets calc all the midpoints +/- midPtDist
  78.     DIM mpdX(1 TO 2 * nSides), mpdY(1 TO 2 * nSides)
  79.     FOR i = 1 TO nSides
  80.         IF i - 1 = 0 THEN ei = nSides ELSE ei = i - 1
  81.         mx = (tilePtsX(ei) + tilePtsX(i)) / 2
  82.         my = (tilePtsY(ei) + tilePtsY(i)) / 2
  83.         'check
  84.         'CIRCLE (mx, my), 2, _RGB32(0, 0, 255)
  85.         '_DELAY .5
  86.  
  87.         'from each mx, my we need a point midPtDist along the angle from mx, my to the ei index point
  88.         a = _ATAN2(tilePtsY(ei) - my, tilePtsX(ei) - mx)
  89.         mdx = mx + midPtDist * COS(a)
  90.         mdy = my + midPtDist * SIN(a)
  91.         'the other point is 180 degrees in opposite direction
  92.         mdx2 = mx + midPtDist * COS(a - _PI)
  93.         mdy2 = my + midPtDist * SIN(a - _PI)
  94.         'check
  95.         'CIRCLE (mdx, mdy), 2, _RGB32(255, 255, 0)
  96.         'CIRCLE (mdx2, mdy2), 2, _RGB32(255, 0, 255)
  97.  
  98.         'OK store all these points for drawing lines later
  99.         mpdX(2 * i - 1) = mdx: mpdY(2 * i - 1) = mdy
  100.         mpdX(2 * i) = mdx2: mpdY(2 * i) = mdy2
  101.  
  102.     NEXT
  103.  
  104.     'from each point in inner star Radius draw 2 lines to the poly edges
  105.     FOR i = 1 TO nSides
  106.         'now figure the pattern: sequence maps are to 2*i +2 and to 2*i - 1
  107.         IF 2 * i + 2 > 2 * nSides THEN map = 2 * i + 2 - 2 * nSides ELSE map = 2 * i + 2
  108.         LINE (innerStarX(i), innerStarY(i))-(mpdX(map), mpdY(map))
  109.  
  110.         IF 2 * i - 1 < 1 THEN map = 2 * i - 1 + 2 * nSides ELSE map = 2 * i - 1
  111.         LINE (innerStarX(i), innerStarY(i))-(mpdX(map), mpdY(map))
  112.         '_DELAY .5
  113.     NEXT
  114.  
  115.  
  116.  

EDIT: change main loop _LIMIT
« Last Edit: April 17, 2019, 09:01:57 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Star Patterns
« Reply #4 on: April 18, 2019, 10:46:26 am »
Hi Bplus
Thanks to share!
I find very attonishing how Math is our better meter to misure and to reproduce the world.
What should Pitagora think about this?
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Star Patterns
« Reply #5 on: April 18, 2019, 11:44:04 am »
Hi Bplus
Thanks to share!
I find very attonishing how Math is our better meter to misure and to reproduce the world.
What should Pitagora think about this?

What would Pythagoras think of computers and programming? He would love QB64!

Quote
There is geometry in the humming of the strings, there is music in the spacing of the spheres.
Read more at: https://www.brainyquote.com/authors/pythagoras

Offline qb4ever

  • Newbie
  • Posts: 40
  • LOCATE 15,15: COLOR 14: PRINT "Hello World!"
    • View Profile
Re: Star Patterns
« Reply #6 on: April 18, 2019, 06:26:33 pm »
What would Pythagoras think of computers and programming? He would love QB64!

Sure !

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Star Patterns
« Reply #7 on: April 19, 2019, 09:27:56 am »
What would Pythagoras think of computers and programming? He would love QB64!
He would be PI eyed
QB64 is the best!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Star Patterns
« Reply #8 on: April 19, 2019, 11:44:22 am »
Nice effect, 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

Marked as best answer by bplus on September 04, 2019, 07:29:29 am

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Star Patterns
« Reply #9 on: April 19, 2019, 01:27:20 pm »
Thanks Ashish,

Here is screen saver style dynamic tiling:
Code: QB64: [Select]
  1. _TITLE "Hexagonal Star Tiling 3" 'B+ 2019-04-19
  2. ' Trying to duplicate results shown here by Daniel Shiffman
  3. ' [youtube]https://www.youtube.com/watch?v=sJ6pMLp_IaI&list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH[/youtube]&index=70
  4. ' but using a completely different method for drawing the tile
  5. ' 2019-04-17 Yes! the star tile can be generalized to any N sided regular polygon!
  6. ' 2019-04-17 This version try Hexagonal Tiling.
  7. ' 2019-04-17 Hexagonal Star Tiling 2, prep one tile and rubber stamp the grid with image.
  8. ' 2019-04-18 Go for a dynamic tile, image constantly changing
  9.  
  10. CONST xmax = 1380 'bigger than your screen can hold
  11. CONST ymax = 800
  12. SCREEN _NEWIMAGE(xmax, ymax, 32)
  13. '_SCREENMOVE _MIDDLE
  14.  
  15. DIM SHARED tile&, polyRadius, triColor AS _UNSIGNED LONG
  16.  
  17. polyRadius = 60
  18. gridheight = polyRadius * SQR(3) / 2
  19. triColor = _RGB32(0, 0, 255)
  20. rd = 10
  21. dm = 20
  22. prepTile polyRadius, rd, dm
  23. rDir = 1: dDir = 1
  24. WHILE _KEYDOWN(27) = 0
  25.     IF rDir = 1 THEN
  26.         IF rd + 1 <= polyRadius * .5 THEN
  27.             rd = rd + 1: prepTile polyRadius, rd, dm
  28.         ELSE
  29.  
  30.             IF RND > .8 THEN
  31.                 polyRadius = rand(20, 200)
  32.                 triColor = _RGB32(128 * RND + 127, 128 * RND + 127, 128 * RND + 127)
  33.                 rDir = -1: dm = RND * polyRadius * .5: rd = RND * polyRadius * .5 \ 1
  34.                 COLOR , _RGB32(128 * RND, 128 * RND, 128 * RND)
  35.             ELSE
  36.                 rDir = -1
  37.             END IF
  38.         END IF
  39.     END IF
  40.     IF rDir = -1 THEN
  41.         IF rd - 1 >= 0 THEN
  42.             rd = rd - 1: prepTile polyRadius, rd, dm
  43.         ELSE
  44.             IF RND > .8 THEN
  45.                 triColor = _RGB32(128 * RND, 128 * RND, 128 * RND)
  46.                 polyRadius = rand(20, 200)
  47.                 rDir = 1: dm = RND * polyRadius * .5: rd = RND * polyRadius * .5 \ 1
  48.                 COLOR , _RGB32(128 * RND + 127, 128 * RND + 127, 128 * RND + 127)
  49.             ELSE
  50.                 rDir = 1
  51.             END IF
  52.         END IF
  53.     END IF
  54.  
  55.     CLS
  56.     gridheight = polyRadius * SQR(3) / 2
  57.     xoff = 0
  58.     FOR y = -polyRadius TO ymax + gridheight STEP gridheight
  59.         xoff = (xoff + 1) MOD 2
  60.         FOR x = -polyRadius TO xmax STEP 3 * polyRadius
  61.             _PUTIMAGE (x + xoff * 1.5 * polyRadius, y), tile&, 0
  62.         NEXT
  63.     NEXT
  64.     _DISPLAY
  65.     _LIMIT .1 * polyRadius
  66.  
  67. SUB prepTile (pRadius, innerStarRadius, midPtDist)
  68.     IF tile& THEN _FREEIMAGE tile&
  69.     tile& = _NEWIMAGE(2 * pRadius, 2 * pRadius, 32)
  70.     _DEST tile&
  71.     drawRegPolyStar pRadius, pRadius, pRadius, 6, innerStarRadius, midPtDist, triColor
  72.     _DEST 0
  73.  
  74. SUB drawRegPolyStar (cx, cy, pRadius, nSides, innerStarRadius, midPtDist, c1 AS _UNSIGNED LONG)
  75.     DIM tilePtsX(1 TO nSides), tilePtsY(1 TO nSides)
  76.     DIM innerStarX(1 TO nSides), innerStarY(1 TO nSides)
  77.  
  78.     pA = _PI(2 / nSides)
  79.     FOR i = 1 TO nSides
  80.         tilePtsX(i) = cx + pRadius * COS(pA * i)
  81.         tilePtsY(i) = cy + pRadius * SIN(pA * i)
  82.         'on the same line the innerStar pts
  83.         innerStarX(i) = cx + innerStarRadius * COS(pA * i)
  84.         innerStarY(i) = cy + innerStarRadius * SIN(pA * i)
  85.         'CIRCLE (innerStarX(i), innerStarY(i)), 3, _RGB32(255, 255, 0)
  86.         'draw tile
  87.         IF i > 1 THEN
  88.             LINE (tilePtsX(i), tilePtsY(i))-(tilePtsX(i - 1), tilePtsY(i - 1)), _RGB32(255, 0, 0, 200)
  89.             IF i = nSides THEN
  90.                 LINE (tilePtsX(i), tilePtsY(i))-(tilePtsX(1), tilePtsY(1)), _RGB32(255, 0, 0, 200)
  91.             END IF
  92.         END IF
  93.         '_DELAY .5
  94.     NEXT
  95.  
  96.     'from each innerStarPt 2 lines connect to side midpoints
  97.     'lets calc all the midpoints +/- midPtDist
  98.     DIM mpdX(1 TO 2 * nSides), mpdY(1 TO 2 * nSides)
  99.     FOR i = 1 TO nSides
  100.         IF i - 1 = 0 THEN ei = nSides ELSE ei = i - 1
  101.         mx = (tilePtsX(ei) + tilePtsX(i)) / 2
  102.         my = (tilePtsY(ei) + tilePtsY(i)) / 2
  103.         'check
  104.         'CIRCLE (mx, my), 2, _RGB32(0, 0, 255)
  105.         '_DELAY .5
  106.  
  107.         'from each mx, my we need a point midPtDist along the angle from mx, my to the ei index point
  108.         a = _ATAN2(tilePtsY(ei) - my, tilePtsX(ei) - mx)
  109.         mdx = mx + midPtDist * COS(a)
  110.         mdy = my + midPtDist * SIN(a)
  111.         'the other point is 180 degrees in opposite direction
  112.         mdx2 = mx + midPtDist * COS(a - _PI)
  113.         mdy2 = my + midPtDist * SIN(a - _PI)
  114.         'check
  115.         'CIRCLE (mdx, mdy), 2, _RGB32(255, 255, 0)
  116.         'CIRCLE (mdx2, mdy2), 2, _RGB32(255, 0, 255)
  117.  
  118.         'OK store all these points for drawing lines later
  119.         mpdX(2 * i - 1) = mdx: mpdY(2 * i - 1) = mdy
  120.         mpdX(2 * i) = mdx2: mpdY(2 * i) = mdy2
  121.  
  122.     NEXT
  123.     COLOR c1
  124.     'from each point in inner star Radius draw 2 lines to the poly edges
  125.     FOR i = 1 TO nSides
  126.         'now figure the pattern: sequence maps are to 2*i +2 and to 2*i - 1
  127.         IF 2 * i + 2 > 2 * nSides THEN map = 2 * i + 2 - 2 * nSides ELSE map = 2 * i + 2
  128.         LINE (innerStarX(i), innerStarY(i))-(mpdX(map), mpdY(map))
  129.  
  130.         IF 2 * i - 1 < 1 THEN map2 = 2 * i - 1 + 2 * nSides ELSE map2 = 2 * i - 1
  131.         LINE (innerStarX(i), innerStarY(i))-(mpdX(map2), mpdY(map2))
  132.  
  133.         ftri innerStarX(i), innerStarY(i), mpdX(map), mpdY(map), mpdX(map2), mpdY(map2), c1
  134.         '_DELAY .5
  135.     NEXT
  136.  
  137.  
  138. FUNCTION rand% (lo%, hi%)
  139.     rand% = INT(RND * (hi% - lo% + 1)) + lo%
  140.  
  141. ' found at [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]:    http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=14425.0
  142. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  143.     a& = _NEWIMAGE(1, 1, 32)
  144.     _DEST a&
  145.     PSET (0, 0), K
  146.     _DEST tile&
  147.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  148.     _FREEIMAGE a& '<<< this is important!
  149.  
  150.