QB64.org Forum

Active Forums => Programs => Topic started by: bplus on May 29, 2021, 01:24:17 pm

Title: Swizzle
Post by: bplus on May 29, 2021, 01:24:17 pm
A quickie:
Code: QB64: [Select]
  1. _Title "Swizzle" ' b+ 2021-05-29
  2. Const Xmax = 600, Ymax = 600, cxy = 300, Pi = _Pi
  3. Screen _NewImage(Xmax, Ymax, 32)
  4. _Delay .25
  5. Dim vScreenR(Xmax, Ymax), vScreenG(Xmax, Ymax), vScreenB(Xmax, Ymax)
  6. restart:
  7. r = Rnd * Rnd: g = Rnd * Rnd: b = Rnd * Rnd
  8. For x = 0 To Xmax
  9.     Line (x, 0)-(x, Ymax), _RGB32(128 + 128 * Sin(r * x), 128 + 128 * Sin(g * x), 128 + 128 * Sin(b * x))
  10.     For y = 0 To Ymax
  11.         vScreenR(x, y) = 128 + 128 * Sin(r * x)
  12.         vScreenG(x, y) = 128 + 128 * Sin(g * x)
  13.         vScreenB(x, y) = 128 + 128 * Sin(b * x)
  14.     Next
  15. swizzle = Rnd * .5 + .8
  16. _Title "Swizzle @" + _Trim$(Str$(swizzle))
  17. For radius = 1 To 200
  18.     For a = 0 To 2 * Pi Step 1 / (2 * Pi * radius)
  19.         x = Int(cxy + radius * Cos(a))
  20.         y = Int(cxy + radius * Sin(a))
  21.         r = vScreenR(x, y)
  22.         g = vScreenG(x, y)
  23.         b = vScreenB(x, y)
  24.         PSet (cxy + radius * Cos(a + radius ^ swizzle * Pi / 180), cxy + radius * Sin(a + radius ^ swizzle * Pi / 180)), _RGB32(r, g, b)
  25.     Next
  26. GoTo restart
  27.  

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Swizzle
Post by: bplus on May 29, 2021, 01:36:14 pm
Here's a variation:
Code: QB64: [Select]
  1. _Title "Swizzle YY" ' b+ 2021-05-29
  2. Const Xmax = 600, Ymax = 600, cxy = 300, Pi = _Pi
  3. Screen _NewImage(Xmax, Ymax, 32)
  4. _Delay .25
  5. Dim vScreenR(Xmax, Ymax), vScreenG(Xmax, Ymax), vScreenB(Xmax, Ymax)
  6.  
  7. For x = 0 To Xmax
  8.     If x < (.5 * Xmax) Then
  9.         r = 0: g = 0: b = 0
  10.     Else
  11.         r = 255: g = 255: b = 255
  12.     End If
  13.     Line (x, 0)-(x, Ymax), _RGB32(r, g, b)
  14.     For y = 0 To Ymax
  15.         vScreenR(x, y) = r
  16.         vScreenG(x, y) = g
  17.         vScreenB(x, y) = b
  18.     Next
  19. swizzle = 1.
  20. _Title "Swizzle @" + _Trim$(Str$(swizzle))
  21. For radius = 1 To 180
  22.     For a = 0 To 2 * Pi Step 1 / (2 * Pi * radius)
  23.         x = Int(cxy + radius * Cos(a))
  24.         y = Int(cxy + radius * Sin(a))
  25.         r = vScreenR(x, y)
  26.         g = vScreenG(x, y)
  27.         b = vScreenB(x, y)
  28.         PSet (cxy + radius * Cos(a + radius ^ swizzle * Pi / 180), cxy + radius * Sin(a + radius ^ swizzle * Pi / 180)), _RGB32(r, g, b)
  29.     Next
  30.  
  31.  
  32.  

Remind anybody of anything?

EDIT: adjust radius
Title: Re: Swizzle
Post by: SMcNeill on May 29, 2021, 02:22:48 pm
The picture remind me of my little screen eater demo I did a few years back, which basically simulated a black hole in the center of the screen which rotated and flushed the image on the screen down the drain.  ;)
Title: Re: Swizzle
Post by: Dav on May 29, 2021, 02:48:32 pm
Nice effect!  I just had to give it spin...

- Dav

Edit: Changed _LIMIT to 15, went too fast before.
 
Code: QB64: [Select]
  1. _TITLE "Swizzle" ' b+ 2021-05-29
  2. CONST Xmax = 600, Ymax = 600, cxy = 300, Pi = _PI
  3. SCREEN _NEWIMAGE(Xmax, Ymax, 32)
  4. _DELAY .25
  5. DIM vScreenR(Xmax, Ymax), vScreenG(Xmax, Ymax), vScreenB(Xmax, Ymax)
  6.  
  7. r = RND * RND: g = RND * RND: b = RND * RND
  8. FOR x = 0 TO Xmax
  9.     LINE (x, 0)-(x, Ymax), _RGB32(128 + 128 * SIN(r * x), 128 + 128 * SIN(g * x), 128 + 128 * SIN(b * x))
  10.     FOR y = 0 TO Ymax
  11.         vScreenR(x, y) = 128 + 128 * SIN(r * x)
  12.         vScreenG(x, y) = 128 + 128 * SIN(g * x)
  13.         vScreenB(x, y) = 128 + 128 * SIN(b * x)
  14.     NEXT
  15. swizzle = RND * .5 + .8
  16.  
  17. FOR radius = 1 TO 200
  18.     FOR a = 0 TO 2 * Pi STEP 1 / (2 * Pi * radius)
  19.         x = INT(cxy + radius * COS(a))
  20.         y = INT(cxy + radius * SIN(a))
  21.         r = vScreenR(x, y)
  22.         g = vScreenG(x, y)
  23.         b = vScreenB(x, y)
  24.         PSET (cxy + radius * COS(a + radius ^ swizzle * Pi / 180), cxy + radius * SIN(a + radius ^ swizzle * Pi / 180)), _RGB32(r, g, b)
  25.     NEXT
  26.  
  27.     s& = _COPYIMAGE(_DISPLAY)
  28.     RotoZoom _WIDTH / 2, _HEIGHT / 2, s&, 1, a
  29.     a = a + 1: IF a >= 360 THEN a = a - 360
  30.     _FREEIMAGE s&
  31.     _DISPLAY: _LIMIT 15
  32.  
  33.  
  34. SUB RotoZoom (X AS LONG, Y AS LONG, Image AS LONG, Scale AS SINGLE, Rotation AS SINGLE)
  35.     DIM px(3) AS SINGLE: DIM py(3) AS SINGLE
  36.     W& = _WIDTH(Image&): H& = _HEIGHT(Image&)
  37.     px(0) = -W& / 2: py(0) = -H& / 2: px(1) = -W& / 2: py(1) = H& / 2
  38.     px(2) = W& / 2: py(2) = H& / 2: px(3) = W& / 2: py(3) = -H& / 2
  39.     sinr! = SIN(-Rotation / 57.2957795131): cosr! = COS(-Rotation / 57.2957795131)
  40.     FOR i& = 0 TO 3
  41.         x2& = (px(i&) * cosr! + sinr! * py(i&)) * Scale + X: y2& = (py(i&) * cosr! - px(i&) * sinr!) * Scale + Y
  42.         px(i&) = x2&: py(i&) = y2&
  43.     NEXT
  44.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, H& - 1)-(W& - 1, H& - 1), Image& TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  45.     _MAPTRIANGLE _SEAMLESS(0, 0)-(W& - 1, 0)-(W& - 1, H& - 1), Image& TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  46.  
  47.  
Title: Re: Swizzle
Post by: bplus on May 29, 2021, 05:31:56 pm
Man doing copies of copies really gets raggedy with Rotozoom, I modified Dav's maybe what he was intending?
Code: QB64: [Select]
  1. _Title "Swizzle Spin" ' b+ 2021-05-29 mod Dav mod b+
  2. Const Xmax = 600, Ymax = 600, cxy = 300, Pi = _Pi
  3. Screen _NewImage(Xmax, Ymax, 32)
  4. _Delay .25
  5. Dim vScreenR(Xmax, Ymax), vScreenG(Xmax, Ymax), vScreenB(Xmax, Ymax)
  6. restart:
  7. r = Rnd * Rnd: g = Rnd * Rnd: b = Rnd * Rnd
  8. For x = 0 To Xmax
  9.     Line (x, 0)-(x, Ymax), _RGB32(128 + 128 * Sin(r * x), 128 + 128 * Sin(g * x), 128 + 128 * Sin(b * x))
  10.     For y = 0 To Ymax
  11.         vScreenR(x, y) = 128 + 128 * Sin(r * x)
  12.         vScreenG(x, y) = 128 + 128 * Sin(g * x)
  13.         vScreenB(x, y) = 128 + 128 * Sin(b * x)
  14.     Next
  15. swizzle = Rnd * .5 + .8
  16.  
  17. For radius = 1 To 200
  18.     For a = 0 To 2 * Pi Step 1 / (2 * Pi * radius)
  19.         x = Int(cxy + radius * Cos(a))
  20.         y = Int(cxy + radius * Sin(a))
  21.         r = vScreenR(x, y)
  22.         g = vScreenG(x, y)
  23.         b = vScreenB(x, y)
  24.         PSet (cxy + radius * Cos(a + radius ^ swizzle * Pi / 180), cxy + radius * Sin(a + radius ^ swizzle * Pi / 180)), _RGB32(r, g, b)
  25.     Next
  26. s& = _NewImage(Xmax * 2.2, Ymax * 2.2, 32)
  27. _PutImage , 0, s&
  28.     RotoZoom cxy, cxy, s&, 1, a
  29.     a = a - 3: If a < -360 Then a = 0: _FreeImage s&: GoTo restart
  30.     _Display: _Limit 30
  31.  
  32.  
  33. Sub RotoZoom (X As Long, Y As Long, Image As Long, Scale As Single, Rotation As Single)
  34.     Dim px(3) As Single: Dim py(3) As Single
  35.     W& = _Width(Image&): H& = _Height(Image&)
  36.     px(0) = -W& / 2: py(0) = -H& / 2: px(1) = -W& / 2: py(1) = H& / 2
  37.     px(2) = W& / 2: py(2) = H& / 2: px(3) = W& / 2: py(3) = -H& / 2
  38.     sinr! = Sin(-Rotation / 57.2957795131): cosr! = Cos(-Rotation / 57.2957795131)
  39.     For i& = 0 To 3
  40.         x2& = (px(i&) * cosr! + sinr! * py(i&)) * Scale + X: y2& = (py(i&) * cosr! - px(i&) * sinr!) * Scale + Y
  41.         px(i&) = x2&: py(i&) = y2&
  42.     Next
  43.     _MapTriangle _Seamless(0, 0)-(0, H& - 1)-(W& - 1, H& - 1), Image& To(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  44.     _MapTriangle _Seamless(0, 0)-(W& - 1, 0)-(W& - 1, H& - 1), Image& To(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  45.  
  46.  
  47.  
  48.  
Title: Re: Swizzle
Post by: johnno56 on May 29, 2021, 05:34:33 pm
Way cool.... Just the thing I need to see at 7:30am on a Sunday morning! Good thing I have my coffee at hand to refocus my optic nerves... LOL

Nay. Cool effect... Does it come in blue? lol
Title: Re: Swizzle
Post by: Dav on May 29, 2021, 07:38:09 pm
That's it, bplus!  Very Cool.  Yeah I wanted to get it there, but didn't know exactly how to do it.  Thanks for showing.  I'm definitely saving that one...

- Dav
Title: Re: Swizzle
Post by: Richard Frost on May 29, 2021, 08:13:35 pm
Distorting spacetime creates marbles!
Title: Re: Swizzle
Post by: OldMoses on May 29, 2021, 09:19:52 pm
This sort of thing amazes me. I am just about able to solve a right triangle with trig commands...
Title: Re: Swizzle
Post by: Ashish on May 30, 2021, 04:14:42 am
@bplus very nice effect! I find it more beautiful when swizzle get values slightly less than 1.
One Question.. Why you have used 2D array... since the color only depends on x,  you can just make it a 1D array.
Title: Re: Swizzle
Post by: bplus on May 30, 2021, 11:19:38 am
Very good points @Ashish here are some changes towards those points:
Code: QB64: [Select]
  1. _Title "Swizzle 2" ' b+ 2021-05-30  Make use of 2D Arrays
  2. Const Xmax = 600, Ymax = 600, cxy = 300, Pi = _Pi
  3. Screen _NewImage(Xmax, Ymax, 32)
  4. _Delay .25
  5. Dim vScreenR(Xmax, Ymax), vScreenG(Xmax, Ymax), vScreenB(Xmax, Ymax)
  6. restart:
  7. r = Rnd * Rnd * .25: g = Rnd * Rnd * .25: b = Rnd * Rnd * .25
  8. For x = 0 To .5 * Xmax
  9.     Line (x, x)-(Xmax - x, Ymax - x), _RGB32(128 + 128 * Sin(r * x), 128 + 128 * Sin(g * x), 128 + 128 * Sin(b * x)), B
  10.     For y = x To Ymax - x
  11.         vScreenR(x, y) = 128 + 128 * Sin(r * x)
  12.         vScreenG(x, y) = 128 + 128 * Sin(g * x)
  13.         vScreenB(x, y) = 128 + 128 * Sin(b * x)
  14.         vScreenR(Xmax - x, y) = 128 + 128 * Sin(r * x)
  15.         vScreenG(Xmax - x, y) = 128 + 128 * Sin(g * x)
  16.         vScreenB(Xmax - x, y) = 128 + 128 * Sin(b * x)
  17.     Next
  18.     For y = x To Xmax - x
  19.         vScreenR(y, x) = 128 + 128 * Sin(r * x)
  20.         vScreenG(y, x) = 128 + 128 * Sin(g * x)
  21.         vScreenB(y, x) = 128 + 128 * Sin(b * x)
  22.         vScreenR(Xmax - y, Ymax - x) = 128 + 128 * Sin(r * x)
  23.         vScreenG(Xmax - y, Ymax - x) = 128 + 128 * Sin(g * x)
  24.         vScreenB(Xmax - y, Ymax - x) = 128 + 128 * Sin(b * x)
  25.     Next
  26. swizzle = Rnd * .2 + .9
  27. _Title "Swizzle @" + _Trim$(Str$(swizzle))
  28. For radius = 1 To 300
  29.     For a = 0 To 2 * Pi Step 1 / (2 * Pi * radius)
  30.         x = Int(cxy + radius * Cos(a))
  31.         y = Int(cxy + radius * Sin(a))
  32.         r = vScreenR(x, y)
  33.         g = vScreenG(x, y)
  34.         b = vScreenB(x, y)
  35.         PSet (cxy + radius * Cos(a + radius ^ swizzle * Pi / 180), cxy + radius * Sin(a + radius ^ swizzle * Pi / 180)), _RGB32(r, g, b)
  36.     Next
  37. GoTo restart
  38.  
  39.  
  40.  
Title: Re: Swizzle
Post by: bplus on May 30, 2021, 01:35:25 pm
I tried an image of Picasso:
Code: QB64: [Select]
  1. _Title "Swizzle Image" ' b+ 2021-05-30  Make use of 2D Arrays
  2. Const Xmax = 600, Ymax = 600, cxy = 300, Pi = _Pi
  3. Screen _NewImage(Xmax, Ymax, 32)
  4. _Delay .25
  5. Dim As _Unsigned Long vScreen(Xmax, Ymax)
  6. p& = _LoadImage("Picasso.PNG")
  7. restart:
  8. r = .0041: g = .001: b = .0072 ' <<<<<<<<<<<<<<< adjust as needed
  9. For x = 0 To .5 * Xmax
  10.     Line (x, x)-(Xmax - x, Ymax - x), _RGB32(128 - 64 * Sin(r * x), 64 - 64 * Sin(g * x), 128 - 64 * Sin(b * x)), B
  11. _PutImage ((_Width - _Width(p&)) / 2, (_Height - _Height(p&)) / 2)-Step(_Width(p&), _Height(p&)), p&, 0
  12. For y = 0 To Ymax
  13.     For x = 0 To Xmax
  14.         vScreen(x, y) = Point(x, y)
  15.     Next
  16. swizzle = .85
  17. _Title "Swizzle @" + _Trim$(Str$(swizzle))
  18. For radius = 1 To 300
  19.     For a = 0 To 2 * Pi Step 1 / (2 * Pi * radius)
  20.         x = Int(cxy + radius * Cos(a))
  21.         y = Int(cxy + radius * Sin(a))
  22.         PSet (cxy + radius * Cos(a + radius ^ swizzle * Pi / 180), cxy + radius * Sin(a + radius ^ swizzle * Pi / 180)), vScreen(x, y)
  23.     Next
  24.  
  25.  
  26.  
Title: Re: Swizzle
Post by: Ashish on May 31, 2021, 07:27:37 am
@bplus woah! amazing work! :D