Author Topic: One more pattern challenge  (Read 7578 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
One more pattern challenge
« on: July 06, 2019, 09:48:35 pm »
Here's a nice strange little pattern I created while playing around a bit earlier.  I thought I'd share it and see how long it'd take bplus (or anyone else) to reproduce it.

Trickiest part (for me, at least), was sorting out how to color the circles all pretty, as they are.  :P

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: One more pattern challenge
« Reply #1 on: July 06, 2019, 10:22:53 pm »
Yes! Very nice coloring on circles, but what's happening with some of the large triangles?

OK, I'll start thinking about those circles :D




Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: One more pattern challenge
« Reply #2 on: July 06, 2019, 10:28:32 pm »
To be honest, I have no idea what the heck is up with the left triangle side.  It’s  created via a series of lines and then paint filled.  As far as I can tell, there’s absolutely no reason for them to leave those black streaks in them. 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: One more pattern challenge
« Reply #3 on: July 06, 2019, 10:53:17 pm »
I tried out some code, but the pattern was too tight for my 15" monitor; so I hooked up my 27" 1950's TV/Monitor. Maybe I'm just missing a few circle statements, but this is the closet I could come to Steve's image...

  [ You are not allowed to view this attachment ]  

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: One more pattern challenge
« Reply #4 on: July 07, 2019, 12:02:25 pm »
Challenge accepted Steve! :D
I did this in about 3-4 hrs.
Code: QB64: [Select]
  1. _TITLE "Pattern Challenge For Steve"
  2. SCREEN _NEWIMAGE(700, 700, 32)
  3.     _LIMIT 40
  4. SUB _GL ()
  5.     _glLineWidth 6.0
  6.     FOR y = 1 TO -1 STEP -0.5
  7.         IF k = 0 THEN
  8.             f = 0
  9.             k = 1
  10.         ELSE
  11.             k = 0
  12.             f = 0.5
  13.         END IF
  14.         FOR x = -1 + f TO 1 + f STEP 1
  15.             pattern x, y, 0.5, 3
  16.         NEXT
  17.     NEXT
  18.     _glRotatef -30, 0, 1, 0
  19. SUB pattern (x, y, r, N)
  20.     IF N > 7 THEN EXIT SUB
  21.     gl_npoly x, y, r, 300
  22.     gl_npoly x, y, r, N
  23.     __x = ((x + r) + (x + r * COS(_PI(2) / N))) * 0.5
  24.     __y = (y + (y + r * SIN(_PI(2) / N))) * 0.5
  25.     nr = SQR((__x - x) ^ 2 + (__y - y) ^ 2)
  26.     pattern x, y, nr - (1 / 50), N + 1 'nr - (1/(d*(2/width)))
  27. SUB gl_npoly (x, y, r, NOV)
  28.     IF NOV MOD 2 = 1 THEN d = 1 / (2 * NOV)
  29.     IF NOV = 3 THEN _glColor3f 1, 1, 0
  30.     IF NOV = 4 THEN _glColor3f 0.5, 0.5, 0.5
  31.     IF NOV = 5 THEN _glColor3f 1, 0, 0
  32.     IF NOV = 6 THEN _glColor3f 1, 1, 1
  33.     IF NOV = 7 THEN _glColor3f 0, 0, 1
  34.     _glBegin _GL_LINE_LOOP
  35.     FOR i = _PI(d) TO _PI(2 + d) STEP _PI(2) / NOV
  36.         IF NOV > 7 THEN
  37.             c~& = hsb(ABS(_R2D(i)), 1, 0.6, 255)
  38.             _glColor3f _RED(c~&) / 255, _GREEN(c~&) / 255, _BLUE(c~&) / 255
  39.         END IF
  40.         _glVertex2f x + r * COS(i), y + r * SIN(i)
  41.     NEXT
  42.     _glEnd
  43. FUNCTION hsb~& (__H AS _FLOAT, __S AS _FLOAT, __B AS _FLOAT, A AS _FLOAT)
  44.     DIM H AS _FLOAT, S AS _FLOAT, B AS _FLOAT
  45.  
  46.     H = __H 'map(__H, 0, 255, 0, 360)
  47.     S = __S 'map(__S, 0, 255, 0, 1)
  48.     B = __B ' map(__B, 0, 255, 0, 1)
  49.  
  50.     IF S = 0 THEN
  51.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  52.         EXIT FUNCTION
  53.     END IF
  54.  
  55.     DIM fmx AS _FLOAT, fmn AS _FLOAT
  56.     DIM fmd AS _FLOAT, iSextant AS INTEGER
  57.     DIM imx AS INTEGER, imd AS INTEGER, imn AS INTEGER
  58.  
  59.     IF B > .5 THEN
  60.         fmx = B - (B * S) + S
  61.         fmn = B + (B * S) - S
  62.     ELSE
  63.         fmx = B + (B * S)
  64.         fmn = B - (B * S)
  65.     END IF
  66.  
  67.     iSextant = INT(H / 60)
  68.  
  69.     IF H >= 300 THEN
  70.         H = H - 360
  71.     END IF
  72.  
  73.     H = H / 60
  74.     H = H - (2 * INT(((iSextant + 1) MOD 6) / 2))
  75.  
  76.     IF iSextant MOD 2 = 0 THEN
  77.         fmd = (H * (fmx - fmn)) + fmn
  78.     ELSE
  79.         fmd = fmn - (H * (fmx - fmn))
  80.     END IF
  81.  
  82.     imx = _ROUND(fmx * 255)
  83.     imd = _ROUND(fmd * 255)
  84.     imn = _ROUND(fmn * 255)
  85.  
  86.     SELECT CASE INT(iSextant)
  87.         CASE 1
  88.             hsb~& = _RGBA32(imd, imx, imn, A)
  89.         CASE 2
  90.             hsb~& = _RGBA32(imn, imx, imd, A)
  91.         CASE 3
  92.             hsb~& = _RGBA32(imn, imd, imx, A)
  93.         CASE 4
  94.             hsb~& = _RGBA32(imd, imn, imx, A)
  95.         CASE 5
  96.             hsb~& = _RGBA32(imx, imn, imd, A)
  97.         CASE ELSE
  98.             hsb~& = _RGBA32(imx, imd, imn, A)
  99.     END SELECT
  100.  
  101.  
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 Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: One more pattern challenge
« Reply #5 on: July 07, 2019, 12:22:26 pm »
FAIL! The colors are wrong and patterns are missing inside the red pentagons.

OK, I'm just busting your balloons. You did remarkably well in just 100 lines of code to approximate the pattern and reproduce some of the colors correctly. It probably only took you an hour, to boot. (I'm on Windows 10, and an hour to boot is a good day!)

Pete

 
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: One more pattern challenge
« Reply #6 on: July 07, 2019, 12:43:55 pm »
Ok. I fixed the color. It is not missing the pattern. The shapes are just overlapping each other..... and I think it must be within the rule of this
challenge.
Code: QB64: [Select]
  1. _TITLE "Pattern Challenge For Steve"
  2. SCREEN _NEWIMAGE(600, 600, 32)
  3. w = 0.02
  4.     _LIMIT 40
  5. SUB _GL ()
  6.     _glLineWidth (_WIDTH / 2) * w
  7.     _glRotatef -90, 0, 0, 1
  8.     FOR y = 1 TO -1 STEP -0.5
  9.         IF k = 0 THEN
  10.             f = 0
  11.             k = 1
  12.         ELSE
  13.             k = 0
  14.             f = 0.5
  15.         END IF
  16.         FOR x = -1 + f TO 1 + f STEP 1
  17.             pattern x, y, 0.5, 3
  18.         NEXT
  19.     NEXT
  20.  
  21. SUB pattern (x, y, r, N)
  22.     IF N > 7 THEN EXIT SUB
  23.     gl_npoly x, y, r, 300
  24.     gl_npoly x, y, r, N
  25.     __x = ((x + r) + (x + r * COS(_PI(2) / N))) * 0.5
  26.     __y = (y + (y + r * SIN(_PI(2) / N))) * 0.5
  27.     nr = SQR((__x - x) ^ 2 + (__y - y) ^ 2)
  28.     pattern x, y, nr - w, N + 1 'nr - (1/(d*(2/width)))
  29. SUB gl_npoly (x, y, r, NOV)
  30.     IF NOV MOD 2 = 1 THEN d = 2 / 3
  31.     IF NOV = 3 THEN _glColor3f 1, 1, 0
  32.     IF NOV = 4 THEN _glColor3f 0.5, 0.5, 0.5
  33.     IF NOV = 5 THEN _glColor3f 1, 0, 0
  34.     IF NOV = 6 THEN _glColor3f 1, 1, 1
  35.     IF NOV = 7 THEN _glColor3f 0, 0, 1
  36.     _glBegin _GL_LINE_LOOP
  37.     FOR i = _PI(d) TO _PI(2 + d) STEP _PI(2) / NOV
  38.         IF NOV > 7 THEN
  39.             c~& = hsb(ABS(_R2D(i)), 1, 0.6, 255)
  40.             _glColor3f _RED(c~&) / 255, _GREEN(c~&) / 255, _BLUE(c~&) / 255
  41.         END IF
  42.         _glVertex2f x + r * COS(i), y + r * SIN(i)
  43.     NEXT
  44.     _glEnd
  45. FUNCTION hsb~& (__H AS _FLOAT, __S AS _FLOAT, __B AS _FLOAT, A AS _FLOAT)
  46.     DIM H AS _FLOAT, S AS _FLOAT, B AS _FLOAT
  47.  
  48.     H = __H 'map(__H, 0, 255, 0, 360)
  49.     S = __S 'map(__S, 0, 255, 0, 1)
  50.     B = __B ' map(__B, 0, 255, 0, 1)
  51.  
  52.     IF S = 0 THEN
  53.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  54.         EXIT FUNCTION
  55.     END IF
  56.  
  57.     DIM fmx AS _FLOAT, fmn AS _FLOAT
  58.     DIM fmd AS _FLOAT, iSextant AS INTEGER
  59.     DIM imx AS INTEGER, imd AS INTEGER, imn AS INTEGER
  60.  
  61.     IF B > .5 THEN
  62.         fmx = B - (B * S) + S
  63.         fmn = B + (B * S) - S
  64.     ELSE
  65.         fmx = B + (B * S)
  66.         fmn = B - (B * S)
  67.     END IF
  68.  
  69.     iSextant = INT(H / 60)
  70.  
  71.     IF H >= 300 THEN
  72.         H = H - 360
  73.     END IF
  74.  
  75.     H = H / 60
  76.     H = H - (2 * INT(((iSextant + 1) MOD 6) / 2))
  77.  
  78.     IF iSextant MOD 2 = 0 THEN
  79.         fmd = (H * (fmx - fmn)) + fmn
  80.     ELSE
  81.         fmd = fmn - (H * (fmx - fmn))
  82.     END IF
  83.  
  84.     imx = _ROUND(fmx * 255)
  85.     imd = _ROUND(fmd * 255)
  86.     imn = _ROUND(fmn * 255)
  87.  
  88.     SELECT CASE INT(iSextant)
  89.         CASE 1
  90.             hsb~& = _RGBA32(imd, imx, imn, A)
  91.         CASE 2
  92.             hsb~& = _RGBA32(imn, imx, imd, A)
  93.         CASE 3
  94.             hsb~& = _RGBA32(imn, imd, imx, A)
  95.         CASE 4
  96.             hsb~& = _RGBA32(imd, imn, imx, A)
  97.         CASE 5
  98.             hsb~& = _RGBA32(imx, imn, imd, A)
  99.         CASE ELSE
  100.             hsb~& = _RGBA32(imx, imd, imn, A)
  101.     END SELECT
  102.  
  103.  
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 SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: One more pattern challenge
« Reply #7 on: July 07, 2019, 12:46:32 pm »
Excellent use of _GL commands, Ashish!  To be honest, I'd considered making use of them myself, to color my main circle, but I didn't.  Instead, I used a much more "BASIC" method:

Code: QB64: [Select]
  1. DIM BlendedScreen AS LONG
  2.  
  3. ViewScreen = _NEWIMAGE(800, 800, 32): SCREEN ViewScreen
  4.  
  5.  
  6. CreateBlendedScreen 'Create a blended screen of colors
  7. SCREEN BlendedScreen 'Display that  screen
  8. SLEEP 'Pause so folks can view that screen
  9.  
  10. 'For fun, add some fontage to the screen also
  11. COLOR &HFF000000, 0 'Black with transparent background
  12. f = _LOADFONT("cour.ttf", 48)
  13. IF f > 0 THEN
  14.     _FONT f
  15.     w = _PRINTWIDTH("Hello World! We love STEVE! He's Sooo AWESOME!")
  16.     sw = _WIDTH: sh = _HEIGHT
  17.     _PRINTSTRING ((sw - w) \ 2 + 1, 0), "Hello World! We love STEVE! He's Sooo AWESOME!"
  18.  
  19. SLEEP 'Give the user the time to see our nice screen pattern which we're going to "warp" to our circle.
  20.  
  21.  
  22. SCREEN ViewScreen 'Swap back over to our main screen
  23. CreateCircle 400, 400, 400, 100 'Use that blended screen as reference so we can plot/color our circle in that same pattern
  24. SLEEP 'Pause to show the circle before ending the demo
  25.  
  26.  
  27. SUB CreateCircle (Xcenter, YCenter, radius AS LONG, width AS LONG)
  28.     SHARED BlendedScreen AS LONG
  29.     p = _PI(2) * radius
  30.     tempimage = _NEWIMAGE(p, width, 32)
  31.     _PUTIMAGE , BlendedScreen, tempimage
  32.     _SOURCE tempimage
  33.     FOR i = 0 TO p - 1 STEP .25
  34.         FOR w = 0 TO width - 1
  35.             ai = 360 / p
  36.             a = _D2R(ai * i)
  37.             x = Xcenter - SIN(a) * (radius - w)
  38.             y = YCenter + COS(a) * (radius - w)
  39.             PSET (x, y), POINT(i, w)
  40.         NEXT
  41.     NEXT
  42.     _SOURCE 0
  43.     _FREEIMAGE tempimage
  44.  
  45.  
  46. SUB CreateBlendedScreen
  47.     SHARED BlendedScreen AS LONG
  48.     IF NOT BlendedScreen THEN BlendedScreen = _NEWIMAGE(1536, 50, 32) ELSE EXIT SUB
  49.     'step from red to green
  50.     DIM kolor AS _FLOAT, i AS _FLOAT
  51.     _DEST BlendedScreen
  52.     FOR i = 0 TO 255
  53.         kolor = kolor + 1
  54.         LINE (i, 0)-STEP(0, 99), _RGB32(255, kolor, 0)
  55.     NEXT
  56.     FOR i = 256 TO 511
  57.         kolor = kolor - 1
  58.         LINE (i, 0)-STEP(0, 99), _RGB32(kolor, 255, 0)
  59.     NEXT
  60.     FOR i = 512 TO 767
  61.         kolor = kolor + 1
  62.         LINE (i, 0)-STEP(0, 99), _RGB32(0, 255, kolor)
  63.     NEXT
  64.     FOR i = 768 TO 1023
  65.         kolor = kolor - 1
  66.         LINE (i, 0)-STEP(0, 99), _RGB32(0, kolor, 255)
  67.     NEXT
  68.     FOR i = 1024 TO 1279
  69.         kolor = kolor + 1
  70.         LINE (i, 0)-STEP(0, 99), _RGB32(kolor, 0, 255)
  71.     NEXT
  72.     FOR i = 1280 TO 1535
  73.         kolor = kolor - 1
  74.         LINE (i, 0)-STEP(0, 99), _RGB32(255, 0, kolor)
  75.     NEXT
  76.     _DEST ViewScreen
  77.  

The magic all takes place inside the CreateCircle sub, where it basically reads the flat square screen, and then warps its contents and plots them onto a circular surface for us.

As you can see from the little demo above, it doesn't actually blend colors; it maps square points to a circular pattern, which allows us to bend text to fit the circle and look natural.

(If you don't have the "courier new, regular" font on your system, kindly change the line which loads it so that it'll point to a font of your choosing so the demo can display properly and in all its glory.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: One more pattern challenge
« Reply #8 on: July 07, 2019, 01:10:05 pm »
Ok. I fixed the color. It is not missing the pattern. The shapes are just overlapping each other..... and I think it must be within the rule of this
challenge.

Only thing I really noticed that seemed off is just a little math.  Your polygons are from the outer ring of your circle and not the inner ring.  ;)



Now, for some honesty on my part:  The screenshot produced wasn't the actual results which I was looking for -- it's a glitched pattern which I thought was kinda cute and decided to share.

The glitchy code to produce our perfectly imperfect pattern is this one:
Code: QB64: [Select]
  1.  
  2. DIM SHARED ViewScreen, BlendedScreen
  3. CONST Gold = &HFFFFD700~& ' _RGB32(255,215,0)
  4. CONST Silver = &HFFC0C0C0~& ' _RGB32(192,192,192)
  5.  
  6. ViewScreen = _NEWIMAGE(800, 800, 32): SCREEN ViewScreen
  7. BlendedScreen = _NEWIMAGE(1536, 100, 32)
  8. CircleScreen = _NEWIMAGE(400, 400, 32) '200 radius outer circle
  9.  
  10. CreateBlendedScreen
  11.  
  12. width = 6
  13. k(3) = Gold
  14. k(4) = Silver
  15. k(5) = &HFFFF0000 'Red
  16. k(6) = &HFFFFFFFF 'White
  17. k(7) = &HFF0000FF 'Blue
  18.  
  19. FOR x = 200 TO 600 STEP 400
  20.     FOR y = 200 TO 600 STEP 400
  21.         length = 200
  22.         FOR i = 3 TO 7
  23.             DrawPattern x, y, length, width, i, k(i)
  24.             length = (length - 2 * width) * COS(_PI / i) - 1
  25.         NEXT
  26.     NEXT
  27.  
  28.  
  29. SUB DrawPattern (Xcenter, Ycenter, Radius, Width, N, Kolor AS _UNSIGNED LONG)
  30.     CreateCircleScreen Xcenter, Ycenter, Radius, Width
  31.     DisplayImage ViewScreen, Xcenter, Ycenter, 0, 0
  32.     CreatePolygon Xcenter, Ycenter, Radius - Width, Width, N, Kolor
  33.  
  34. SUB CreatePolygon (Xcenter, YCenter, Radius AS LONG, width AS LONG, N AS LONG, Kolor AS _UNSIGNED LONG)
  35.     a = _PI(2) / N
  36.     FOR i = 0 TO 1
  37.         x = 0
  38.         IF i THEN Radius = Radius - width
  39.         FOR p = 0 TO _PI(2) STEP a
  40.             IF x THEN oldx = x: oldy = y
  41.             x = Xcenter + SIN(p) * Radius
  42.             y = YCenter + COS(p) * Radius
  43.             IF oldx THEN LINE (oldx, oldy)-(x, y), Kolor
  44.         NEXT
  45.         x2 = Xcenter + SIN(p) * Radius
  46.         y2 = YCenter + COS(p) * Radius
  47.         LINE (x, y)-(x2, y2), Kolor
  48.     NEXT
  49.     x = Xcenter + SIN(p) * (Radius + width / 2)
  50.     y = YCenter + COS(p) * (Radius + width / 2)
  51.     PAINT (x, y), Kolor
  52.  
  53. SUB CreateCircleScreen (Xcenter, YCenter, radius AS LONG, width AS LONG)
  54.     'CreateCircleScreen = _NEWIMAGE(radius * 2, radius * 2, 32)
  55.     '_DEST CreateCircleScreen
  56.     p = _PI(2) * radius
  57.     tempimage = _NEWIMAGE(p, width, 32)
  58.     _PUTIMAGE , BlendedScreen, tempimage
  59.     _SOURCE tempimage
  60.     FOR i = 0 TO p - 1 STEP .25
  61.         FOR w = 0 TO width - 1
  62.             ai = 360 / p
  63.             a = _D2R(ai * i)
  64.             x = Xcenter + SIN(a) * (radius - w)
  65.             y = YCenter + COS(a) * (radius - w)
  66.             PSET (x, y), POINT(i, w)
  67.         NEXT
  68.     NEXT
  69.     _SOURCE ViewScreen
  70.     ' _DEST ViewScreen
  71.     _FREEIMAGE tempimage
  72.  
  73.  
  74. SUB CreateBlendedScreen
  75.     'step from red to green
  76.     DIM kolor AS _FLOAT, i AS _FLOAT
  77.     _DEST BlendedScreen
  78.     FOR i = 0 TO 255
  79.         kolor = kolor + 1
  80.         LINE (i, 0)-STEP(0, 99), _RGB32(255, kolor, 0)
  81.     NEXT
  82.     FOR i = 256 TO 511
  83.         kolor = kolor - 1
  84.         LINE (i, 0)-STEP(0, 99), _RGB32(kolor, 255, 0)
  85.     NEXT
  86.     FOR i = 512 TO 767
  87.         kolor = kolor + 1
  88.         LINE (i, 0)-STEP(0, 99), _RGB32(0, 255, kolor)
  89.     NEXT
  90.     FOR i = 768 TO 1023
  91.         kolor = kolor - 1
  92.         LINE (i, 0)-STEP(0, 99), _RGB32(0, kolor, 255)
  93.     NEXT
  94.     FOR i = 1024 TO 1279
  95.         kolor = kolor + 1
  96.         LINE (i, 0)-STEP(0, 99), _RGB32(kolor, 0, 255)
  97.     NEXT
  98.     FOR i = 1280 TO 1535
  99.         kolor = kolor - 1
  100.         LINE (i, 0)-STEP(0, 99), _RGB32(255, 0, kolor)
  101.     NEXT
  102.     _DEST ViewScreen
  103.  
  104.  
  105. SUB DisplayImage (Image AS LONG, x AS INTEGER, y AS INTEGER, angle AS SINGLE, mode AS _BYTE)
  106.     'Image is the image handle which we use to reference our image.
  107.     'x,y is the X/Y coordinates where we want the image to be at on the screen.
  108.     'angle is the angle which we wish to rotate the image.
  109.     'mode determines HOW we place the image at point X,Y.
  110.     'Mode 0 we center the image at point X,Y
  111.     'Mode 1 we place the Top Left corner of oour image at point X,Y
  112.     'Mode 2 is Bottom Left
  113.     'Mode 3 is Top Right
  114.     'Mode 4 is Bottom Right
  115.  
  116.  
  117.     DIM px(3) AS INTEGER, py(3) AS INTEGER, w AS INTEGER, h AS INTEGER
  118.     DIM sinr AS SINGLE, cosr AS SINGLE, i AS _BYTE
  119.     w = _WIDTH(Image): h = _HEIGHT(Image)
  120.     SELECT CASE mode
  121.         CASE 0 'center
  122.             px(0) = -w \ 2: py(0) = -h \ 2: px(3) = w \ 2: py(3) = -h \ 2
  123.             px(1) = -w \ 2: py(1) = h \ 2: px(2) = w \ 2: py(2) = h \ 2
  124.         CASE 1 'top left
  125.             px(0) = 0: py(0) = 0: px(3) = w: py(3) = 0
  126.             px(1) = 0: py(1) = h: px(2) = w: py(2) = h
  127.         CASE 2 'bottom left
  128.             px(0) = 0: py(0) = -h: px(3) = w: py(3) = -h
  129.             px(1) = 0: py(1) = 0: px(2) = w: py(2) = 0
  130.         CASE 3 'top right
  131.             px(0) = -w: py(0) = 0: px(3) = 0: py(3) = 0
  132.             px(1) = -w: py(1) = h: px(2) = 0: py(2) = h
  133.         CASE 4 'bottom right
  134.             px(0) = -w: py(0) = -h: px(3) = 0: py(3) = -h
  135.             px(1) = -w: py(1) = 0: px(2) = 0: py(2) = 0
  136.     END SELECT
  137.     sinr = SIN(angle / 57.2957795131): cosr = COS(angle / 57.2957795131)
  138.     FOR i = 0 TO 3
  139.         x2 = (px(i) * cosr + sinr * py(i)) + x: y2 = (py(i) * cosr - px(i) * sinr) + y
  140.         px(i) = x2: py(i) = y2
  141.     NEXT
  142.     _MAPTRIANGLE (0, 0)-(0, h - 1)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  143.     _MAPTRIANGLE (0, 0)-(w - 1, 0)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))

The actual pattern I was looking for is this one (which requires only a single line remarked out):
Code: [Select]
_DEFINE A-Z AS _FLOAT

DIM SHARED ViewScreen, BlendedScreen
CONST Gold = &HFFFFD700~& ' _RGB32(255,215,0)
CONST Silver = &HFFC0C0C0~& ' _RGB32(192,192,192)

ViewScreen = _NEWIMAGE(800, 800, 32): SCREEN ViewScreen
BlendedScreen = _NEWIMAGE(1536, 100, 32)
CircleScreen = _NEWIMAGE(400, 400, 32) '200 radius outer circle

CreateBlendedScreen

width = 6
DIM k(3 TO 7) AS _UNSIGNED LONG
k(3) = Gold
k(4) = Silver
k(5) = &HFFFF0000 'Red
k(6) = &HFFFFFFFF 'White
k(7) = &HFF0000FF 'Blue

FOR x = 200 TO 600 STEP 400
    FOR y = 200 TO 600 STEP 400
        length = 200
        FOR i = 3 TO 7
            DrawPattern x, y, length, width, i, k(i)
            length = (length - 2 * width) * COS(_PI / i) - 1
        NEXT
    NEXT
NEXT

SLEEP
END

SUB DrawPattern (Xcenter, Ycenter, Radius, Width, N, Kolor AS _UNSIGNED LONG)
    CreateCircleScreen Xcenter, Ycenter, Radius, Width
    'DisplayImage ViewScreen, Xcenter, Ycenter, 0, 0
    CreatePolygon Xcenter, Ycenter, Radius - Width, Width, N, Kolor
END SUB

SUB CreatePolygon (Xcenter, YCenter, Radius AS LONG, width AS LONG, N AS LONG, Kolor AS _UNSIGNED LONG)
    a = _PI(2) / N
    FOR i = 0 TO 1
        x = 0
        IF i THEN Radius = Radius - width
        FOR p = 0 TO _PI(2) STEP a
            IF x THEN oldx = x: oldy = y
            x = Xcenter + SIN(p) * Radius
            y = YCenter + COS(p) * Radius
            IF oldx THEN LINE (oldx, oldy)-(x, y), Kolor
        NEXT
        x2 = Xcenter + SIN(p) * Radius
        y2 = YCenter + COS(p) * Radius
        LINE (x, y)-(x2, y2), Kolor
    NEXT
    x = Xcenter + SIN(p) * (Radius + width / 2)
    y = YCenter + COS(p) * (Radius + width / 2)
    PAINT (x, y), Kolor
END SUB

SUB CreateCircleScreen (Xcenter, YCenter, radius AS LONG, width AS LONG)
    'CreateCircleScreen = _NEWIMAGE(radius * 2, radius * 2, 32)
    '_DEST CreateCircleScreen
    p = _PI(2) * radius
    tempimage = _NEWIMAGE(p, width, 32)
    _PUTIMAGE , BlendedScreen, tempimage
    _SOURCE tempimage
    FOR i = 0 TO p - 1 STEP .25
        FOR w = 0 TO width - 1
            ai = 360 / p
            a = _D2R(ai * i)
            x = Xcenter + SIN(a) * (radius - w)
            y = YCenter + COS(a) * (radius - w)
            PSET (x, y), POINT(i, w)
        NEXT
    NEXT
    _SOURCE ViewScreen
    ' _DEST ViewScreen
    _FREEIMAGE tempimage
END SUB


SUB CreateBlendedScreen
    'step from red to green
    DIM kolor AS _FLOAT, i AS _FLOAT
    _DEST BlendedScreen
    FOR i = 0 TO 255
        kolor = kolor + 1
        LINE (i, 0)-STEP(0, 99), _RGB32(255, kolor, 0)
    NEXT
    FOR i = 256 TO 511
        kolor = kolor - 1
        LINE (i, 0)-STEP(0, 99), _RGB32(kolor, 255, 0)
    NEXT
    FOR i = 512 TO 767
        kolor = kolor + 1
        LINE (i, 0)-STEP(0, 99), _RGB32(0, 255, kolor)
    NEXT
    FOR i = 768 TO 1023
        kolor = kolor - 1
        LINE (i, 0)-STEP(0, 99), _RGB32(0, kolor, 255)
    NEXT
    FOR i = 1024 TO 1279
        kolor = kolor + 1
        LINE (i, 0)-STEP(0, 99), _RGB32(kolor, 0, 255)
    NEXT
    FOR i = 1280 TO 1535
        kolor = kolor - 1
        LINE (i, 0)-STEP(0, 99), _RGB32(255, 0, kolor)
    NEXT
    _DEST ViewScreen
END SUB


SUB DisplayImage (Image AS LONG, x AS INTEGER, y AS INTEGER, angle AS SINGLE, mode AS _BYTE)
    'Image is the image handle which we use to reference our image.
    'x,y is the X/Y coordinates where we want the image to be at on the screen.
    'angle is the angle which we wish to rotate the image.
    'mode determines HOW we place the image at point X,Y.
    'Mode 0 we center the image at point X,Y
    'Mode 1 we place the Top Left corner of oour image at point X,Y
    'Mode 2 is Bottom Left
    'Mode 3 is Top Right
    'Mode 4 is Bottom Right


    DIM px(3) AS INTEGER, py(3) AS INTEGER, w AS INTEGER, h AS INTEGER
    DIM sinr AS SINGLE, cosr AS SINGLE, i AS _BYTE
    w = _WIDTH(Image): h = _HEIGHT(Image)
    SELECT CASE mode
        CASE 0 'center
            px(0) = -w \ 2: py(0) = -h \ 2: px(3) = w \ 2: py(3) = -h \ 2
            px(1) = -w \ 2: py(1) = h \ 2: px(2) = w \ 2: py(2) = h \ 2
        CASE 1 'top left
            px(0) = 0: py(0) = 0: px(3) = w: py(3) = 0
            px(1) = 0: py(1) = h: px(2) = w: py(2) = h
        CASE 2 'bottom left
            px(0) = 0: py(0) = -h: px(3) = w: py(3) = -h
            px(1) = 0: py(1) = 0: px(2) = w: py(2) = 0
        CASE 3 'top right
            px(0) = -w: py(0) = 0: px(3) = 0: py(3) = 0
            px(1) = -w: py(1) = h: px(2) = 0: py(2) = h
        CASE 4 'bottom right
            px(0) = -w: py(0) = -h: px(3) = 0: py(3) = -h
            px(1) = -w: py(1) = 0: px(2) = 0: py(2) = 0
    END SELECT
    sinr = SIN(angle / 57.2957795131): cosr = COS(angle / 57.2957795131)
    FOR i = 0 TO 3
        x2 = (px(i) * cosr + sinr * py(i)) + x: y2 = (py(i) * cosr - px(i) * sinr) + y
        px(i) = x2: py(i) = y2
    NEXT
    _MAPTRIANGLE (0, 0)-(0, h - 1)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
    _MAPTRIANGLE (0, 0)-(w - 1, 0)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
END SUB

Line 36 is remarked out, as I changed my methodology in the midst of my coding.  At first, I was going to take blended lines as well, rotate them and place them at their midpoints so they'd fit in the proper slots, but then I decided not to worry with it.  I would've had to map segments of the overall blended colors to each side, to run the blended pattern across the whole polygon, and I just didn't think it was worth the hassle.  All I needed was a nice little pattern set so I could print out some colorful CD/DVD labels, and to be honest, I was just too lazy to go through the effort to blend the  lines as well.  :P

In the steps of removing the polygon line blending (and use of DisplayImage for rotational ease at the center point), I tested what I *thought* should produce my desired pattern -- only to blink stupidly and say, "What the Taco Bell????????"...

... And thus, I shared the glitch, just 'cause I found it rather amusing.  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: One more pattern challenge
« Reply #9 on: July 07, 2019, 01:11:30 pm »
Steve,
Cool to know you were using this method for circle. I think you already posted a similar demo for printing text in this way somewhere before....
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 SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: One more pattern challenge
« Reply #10 on: July 07, 2019, 01:16:39 pm »
Steve,
Cool to know you were using this method for circle. I think you already posted a similar demo for printing text in this way somewhere before....

Aye, a CircleText demo routine, which I've shared before. You've got a good memory.  ;)

Here's another trick which I like to do, using this method:  Slowly reduce the radius as you plot the square around onto your circle.  Doing so, you can make a spiral, and if you animate it by scrolling the text across the square source page, you can make it seem as if you're flushing the text down a toilet... 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: One more pattern challenge
« Reply #11 on: July 07, 2019, 01:16:54 pm »
Only thing I really noticed that seemed off is just a little math.  Your polygons are from the outer ring of your circle and not the inner ring.  ;)
I am already subtracting radius with line width but it is not working as expected. See on line 32 of my second code.
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 SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: One more pattern challenge
« Reply #12 on: July 07, 2019, 01:29:23 pm »
I am already subtracting radius with line width but it is not working as expected. See on line 32 of my second code.

In sub pattern, subtract the width of the circle before drawing the polygon.

Code: QB64: [Select]
  1. SUB pattern (x, y, r, N)
  2.     IF N > 7 THEN EXIT SUB
  3.     gl_npoly x, y, r, 300
  4.     r = r - w 'Add this little line into the code so the polygon draws to the inner edge of the circle and not the outer.  
  5.     gl_npoly x, y, r, N
  6.     __x = ((x + r) + (x + r * COS(_PI(2) / N))) * 0.5
  7.     __y = (y + (y + r * SIN(_PI(2) / N))) * 0.5
  8.     nr = SQR((__x - x) ^ 2 + (__y - y) ^ 2)
  9.     pattern x, y, nr - w, N + 1 'nr - (1/(d*(2/width)))
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: One more pattern challenge
« Reply #13 on: July 07, 2019, 02:06:39 pm »
Oh, it's a sub pattern! Apparently I failed the challenge, because I was using a test pattern!

Well the code is interesting to look at, but I tend to not get involved in these types of challenges, because I just can't think of a practical use for it in the type of apps I'm interested in.

Pete
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: One more pattern challenge
« Reply #14 on: July 07, 2019, 04:21:21 pm »
I scaled and overlapped different, took too long to get those color rings going (completely different method) and match the color of triangles. What do you call that Steve? :D

Edit: oops missing stuff on right side, fixed.
« Last Edit: July 07, 2019, 04:29:28 pm by bplus »