Author Topic: Cresent Pattern Challenge from JB  (Read 8907 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Cresent Pattern Challenge from JB
« on: June 27, 2019, 06:55:47 pm »
Can you draw this pattern:

  [ You are not allowed to view this attachment ]  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Cresent Pattern Challenge from JB
« Reply #1 on: June 27, 2019, 08:08:50 pm »
Sure I can, and in the easiest way imaginable!
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: Cresent Pattern Challenge from JB
« Reply #2 on: June 27, 2019, 09:45:26 pm »
;-)) Pretty hefty .bas file there Steve! But that's smart thinking. :)

Maybe someone can do it in a couple less MB's.

  [ You are not allowed to view this attachment ]  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Cresent Pattern Challenge from JB
« Reply #3 on: June 27, 2019, 10:00:36 pm »
Honestly, since it’s a black and white pattern, one can turn those data statements into a simple set of on/off counters.  3 white, 13 black, 33 white... 

Instead of saving data as pixel by pixel, save that data as number of white, number of black, number of white...  (3, 13, 33...)

It’d compress down to a fraction of the size.  (Maybe even as small as your screenshot.  ;-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: Cresent Pattern Challenge from JB
« Reply #4 on: June 28, 2019, 01:43:37 am »
Hi. I tried doing this in approx. 2KB, but I can not get closer than this. ;)

 [ You are not allowed to view this attachment ]    

Code: QB64: [Select]
  1. _TITLE "Pattern Challenge Program by Ashish"
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. buffer& = _COPYIMAGE(0)
  4. CLS , _RGB(255, 255, 255)
  5. _DEST buffer&
  6. CLS , 1
  7. rad = 50
  8. FOR y = rad TO _HEIGHT STEP rad * SQR(3)
  9.     IF a = 0 THEN
  10.         a = 1
  11.         k = 0
  12.         FOR x = 0 TO _WIDTH STEP rad * 2
  13.             IF k = 0 THEN
  14.                 k = 1
  15.                 pattern x, y, rad, 0
  16.             ELSE
  17.                 k = 0
  18.                 pattern x, y, rad, 0.85
  19.             END IF
  20.         NEXT x
  21.     ELSE
  22.         a = 0
  23.         k = 0
  24.         FOR x = rad TO _WIDTH STEP rad * 2
  25.             IF k = 0 THEN
  26.                 k = 1
  27.                 pattern x, y, rad, 0
  28.             ELSE
  29.                 k = 0
  30.                 pattern x, y, rad, 0.85
  31.             END IF
  32.  
  33.         NEXT x
  34.     END IF
  35. _SOURCE buffer&
  36. FOR i = 0 TO _WIDTH - 1
  37.     FOR j = 0 TO _HEIGHT - 1
  38.         IF POINT(i, j) = _RGB(255, 255, 255) THEN PSET (i, j), _RGB(0, 0, 0)
  39. NEXT j, i
  40. _PUTIMAGE (0, 0), buffer&
  41. SUB pattern (x, y, r, off_ang)
  42.     CIRCLE (x, y), r, _RGB(254, 254, 254)
  43.     FOR i = off_ang TO _PI(2) + off_ang STEP _PI(1 / 6) '2pi/12
  44.         circ (x + (COS(i) * r + x)) / 2, (y + (SIN(i) * r + y)) / 2, r / 2, r / 2, _RGB(254, 254, 254), i, _PI + i
  45.     NEXT
  46.     FOR i = off_ang TO _PI(2) + off_ang STEP _PI(1 / 6) '2pi/12
  47.         IF n = 0 THEN
  48.             n = 1
  49.             PAINT (COS(i) * 0.8 * r + x, SIN(i) * 0.8 * r + y), _RGB(255, 255, 255), _RGB(254, 254, 254)
  50.         ELSE n = 0
  51.         END IF
  52.     NEXT
  53. SUB circ (x, y, rx, ry, c~&, theta1, theta2)
  54.     FOR i = theta1 TO theta2 STEP 0.01
  55.         LINE (COS(i) * rx + x, SIN(i) * ry + y)-(COS(i + 0.01) * rx + x, SIN(i + 0.01) * ry + y), c~&
  56.     NEXT
  57.  
« Last Edit: June 28, 2019, 01:44:46 am by Ashish »
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 Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #5 on: June 28, 2019, 05:53:18 am »
Hi BPlus. Mine is not so good as your.
Second radius is ellipse?

Code: QB64: [Select]
  1.  
  2. DIM Frames(9) AS LONG
  3.  
  4.  
  5. FOR f = 1 TO 9
  6.     R = R - _PI(2) / 9
  7.     Frames(f) = _NEWIMAGE(101, 101, 256)
  8.     _DEST Frames(f)
  9.     FOR pi = R TO R + _PI(2) STEP _PI(2) / 6
  10.         sx = 50 + COS(pi) * 25
  11.         sy = 50 + SIN(pi) * 25
  12.         sx2 = 50 + COS(pi + .3) * 25
  13.         sy2 = 50 + SIN(pi + .3) * 25
  14.         sx3 = 50 + COS(pi + _PI(2) / 6 + .2) * 20
  15.         sy3 = 50 + SIN(pi + _PI(2) / 6 + .2) * 20
  16.  
  17.         FOR c = 0 TO _PI(1.1) STEP .001
  18.             PSET (sx + COS(c + pi) * 25, sy + SIN(c + pi) * 25)
  19.             PSET (sx2 + COS(c + pi) * 25, sy2 + SIN(c + pi) * 25)
  20.         NEXT c
  21.         PAINT (sx3, sy3), 15, 15
  22.     NEXT
  23.     _CLEARCOLOR 0
  24.  
  25. SCREEN _NEWIMAGE(800, 600, 256)
  26. _PALETTECOLOR 0, &HFFFFFFFF
  27. _PALETTECOLOR 15, &HFF000000
  28.  
  29. f = 0
  30.  
  31.     CLS
  32.     g = 0
  33.     FOR y = 0 TO 600 STEP 90
  34.         g = g + 1
  35.         FOR x = -100 TO 800 STEP 96
  36.             f = f + 1
  37.             IF f > 9 THEN f = 1
  38.             IF g MOD 2 = 0 THEN p = 50 ELSE p = 0
  39.             _PUTIMAGE (x + p, y), Frames(f)
  40.         NEXT x
  41.     NEXT y
  42.     _LIMIT 20
  43.     _DISPLAY
  44.  

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #6 on: June 28, 2019, 08:27:49 am »
@Petr
That has nearly damaged my eyesight. :D
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 Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #7 on: June 28, 2019, 08:52:20 am »
Hi Ashish. Am sorry :-D  Bplus destroyed my brain :-D

FellippeHeitor

  • Guest
Re: Cresent Pattern Challenge from JB
« Reply #8 on: June 28, 2019, 09:20:58 am »
@Petr and @Ashish,

You guys are definitely going places.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #9 on: June 28, 2019, 11:08:38 am »
Code: QB64: [Select]
  1.  
  2. declare sub swirl(x, y, r)
  3. pi = 4*atn(1)
  4. r = 70
  5.  
  6.  
  7. line (0,0)-(640,480),,bf
  8.  
  9. for yy = 0 to 5
  10. for xx = 0 to 5  
  11.     swirl xx*2*r + r*(yy and 1), 0 + yy*2*r*cos(pi/6), r
  12.  
  13.  
  14. sub swirl(x, y, r)
  15.     dim a as double, b as double
  16.  
  17.     for i=0 to 6
  18.         xx = x + 0.5*r*cos(i*pi/3)
  19.         yy = y + 0.5*r*sin(i*pi/3)
  20.        
  21.         a = i*pi/3
  22.        
  23.         pset (xx + 0.5*r*cos(a), yy + 0.5*r*sin(a)), 0
  24.         for b=a to a + pi step 0.01
  25.             line -(xx + 0.5*r*cos(b), yy + 0.5*r*sin(b)), 0
  26.         next
  27.        
  28.         pset (xx + 0.5*r*cos(a), yy + 0.5*r*sin(a)), 0
  29.         for b=0 to pi step 0.01
  30.             line -(xx + 0.5*r*cos(b)*cos(a) - 0.28*r*sin(b)*sin(a), yy + 0.5*r*cos(b)*sin(a) + 0.28*r*sin(b)*cos(a)), 0
  31.         next
  32.        
  33.         paint (xx - 0.4*r*sin(a), yy + 0.4*r*cos(a)), 0
  34.     next
  35.  
« Last Edit: June 28, 2019, 11:15:54 am by _vince »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #10 on: June 28, 2019, 11:19:33 am »
@Petr and @Ashish,

You guys are definitely going places.

Yeppers!

Ashish, your trailing edge is a radius of a circle as the leading edge, a result I failed to achieve, nice!

Petr, your code has 2 lines more than mine and yours moves! also nice!

Oh Vince just posted! Dang! wow, nice concise code!!!



My trailing edge is not a radius of a circle, I'm not even sure it is ellipse.
If leading edge y = SQR(radius^2 - x^2) then trailing edge is a fraction of y, so I can make the crescent as thick or thin as I want. EDIT minus not plus

Yeah Petr, now I've got to get the cresents twirling too! ;-)) Plus.... :)


PS sorry for my poor spelling of crescent (the spell checker worked!)
« Last Edit: June 28, 2019, 01:04:11 pm by bplus »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #11 on: June 28, 2019, 11:34:35 am »
@Vince
Perfect one!
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
Re: Cresent Pattern Challenge from JB
« Reply #12 on: June 28, 2019, 11:39:29 am »
Hi guys
for  now I'm debugging my idea

to get the slices  putting a circle up to another circle.. :-)

I must stop my idea for now. Job is calling!

here bugged code (only forn now)
Code: QB64: [Select]
  1. CONST d = 100
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. _TITLE "Cresent"
  4. PAINT (1, 1), _RGB32(255, 255, 255)
  5. DIM NumX AS INTEGER, NumY AS INTEGER, i AS INTEGER, i2 AS INTEGER
  6. NumX = _CEIL(_WIDTH / d): NumY = _CEIL(_HEIGHT / d)
  7. FOR i = 0 TO NumX
  8.     FOR i2 = 0 TO NumY
  9.         IF i2 MOD 2 = 0 THEN k = 0 ELSE k = d / 4
  10.         x = (i * d) + k
  11.         y = i2 * d
  12.         r = d / 2
  13.         c~& = _RGB32(0, 0, 0)
  14.         CIRCLE (x, y), r, c~&
  15.         MakeSlice x, y, r, c~&
  16.     NEXT i2
  17.  
  18. SUB MakeSlice (xp AS INTEGER, yp AS INTEGER, r AS INTEGER, col AS _UNSIGNED LONG)
  19.     DIM a AS INTEGER, ax AS INTEGER, ay AS INTEGER, ar AS INTEGER, col2 AS _UNSIGNED LONG
  20.     ar = INT(r / 2)
  21.     FOR a = 1 TO 12 STEP 1
  22.         ax = SIN(15 * a) * ar
  23.         ay = COS(15 * a) * ar
  24.         IF a MOD 2 = 0 THEN col2 = col ELSE col2 = _RGB32(255, 255, 255)
  25.         CIRCLE (ax + xp, ay + yp), ar, col
  26.         PAINT STEP(0, 0), col2, col
  27.      NEXT

Here partial result:
  [ You are not allowed to view this attachment ]  

I'll change circle with fillcircle of library of QB64 and I'll search the right number of circle to draw
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: Cresent Pattern Challenge from JB
« Reply #13 on: June 28, 2019, 01:57:54 pm »
Very nice, _vince!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #14 on: June 28, 2019, 02:48:58 pm »
Yes, Vince code is really nice compared to mine used to create the screen shot in original post:
Code: QB64: [Select]
  1. CONST xmax = 800, ymax = 600
  2. _TITLE "Cresent" 'mod B+ makeover started 2019-06-27
  3. SCREEN _NEWIMAGE(xmax, ymax, 32)
  4. _SCREENMOVE 300, 40
  5.  
  6. DIM polyR, stepX, stepY, y, x, xoff
  7. COLOR &HFF000000, &HFFFFFFFF
  8. polyR = 32
  9. stepX = polyR * 4
  10. stepY = polyR * 2 * SQR(3)
  11. FOR y = 16 TO ymax - 16 STEP stepY
  12.     xoff = (xoff + 1) MOD 2
  13.     FOR x = 0 TO xmax + 7 * polyR STEP stepX
  14.         cresentX6 x - xoff * 1.5 * stepX, y
  15.     NEXT
  16.  
  17. SUB cresentX6 (x0, y0)
  18.     DIM r, x, y, py, x1, y1, x2, y2, a, dist1, a1, x3, y3, dist2, a2, x4, y4
  19.     r = 32
  20.     FOR x = -r TO r STEP .005
  21.         y = SQR(r ^ 2 - x ^ 2)
  22.         py = .53 * y
  23.         x1 = x + x0 + r
  24.         y1 = y + y0
  25.         x2 = x1
  26.         y2 = py + y0
  27.         LINE (x1, y1)-(x2, y2)
  28.         FOR a = 0 TO 2 * _PI STEP _PI(1 / 3)
  29.             dist1 = SQR((x1 - x0) ^ 2 + (y1 - y0) ^ 2)
  30.             a1 = _ATAN2(y1 - y0, x1 - x0)
  31.             x3 = x0 + dist1 * COS(a1 + a)
  32.             y3 = y0 + dist1 * SIN(a1 + a)
  33.  
  34.             dist2 = SQR((x2 - x0) ^ 2 + (y2 - y0) ^ 2)
  35.             a2 = _ATAN2(y2 - y0, x2 - x0)
  36.             x4 = x0 + dist2 * COS(a2 + a)
  37.             y4 = y0 + dist2 * SIN(a2 + a)
  38.             LINE (x3, y3)-(x4, y4)
  39.         NEXT
  40.     NEXT
  41.  
  42.  

I was working up something to post at JB forum but JB does not have PAINT (nor POINT).

I have reworked Vince code without PAINT so I can show it off at JB! ;-))
Code: QB64: [Select]
  1. _DEFINE A-Z AS _FLOAT : _TITLE "Crescent by Vince B+ Mod W/O PAINT" 'b+ 2019-06-28
  2. CONST K = &HFFFFFFFF, pi = _PI, r = 70
  3. SCREEN _NEWIMAGE(700, 623, 32)
  4. FOR yy = 0 TO 4
  5.     FOR xx = 0 TO 5
  6.         swirl xx * 2 * r + r * (yy AND 1), 0 + yy * 2 * r * COS(pi / 6)
  7.     NEXT
  8. SUB swirl (x, y)
  9.     FOR i = 0 TO 5
  10.         xx = x + 0.5 * r * COS(i * pi / 3) - r  'Edit: this and next, removed variables not used
  11.         yy = y + 0.5 * r * SIN(i * pi / 3) + r
  12.         a = i * pi / 3
  13.         FOR b = a TO a + pi STEP .0001
  14.             c = b - a
  15.             LINE (xx + 0.5 * r * COS(b), yy + 0.5 * r * SIN(b))-(xx + 0.5 * r * COS(c) * COS(a) - 0.28 * r * SIN(c) * SIN(a), yy + 0.5 * r * COS(c) * SIN(a) + 0.28 * r * SIN(c) * COS(a)), K
  16.         NEXT
  17.     NEXT
  18.  
I reversed the B&W to save a line, to get lines to 20:
  [ You are not allowed to view this attachment ]  
« Last Edit: June 28, 2019, 06:10:48 pm by bplus »