Author Topic: Ectoplasm  (Read 11542 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Ectoplasm
« Reply #30 on: January 23, 2020, 12:31:21 am »
Pretty cool B+! I changed the F number to .05  and all the set loops to 2 so now it goes a bit faster and looks like blobs in a lava lamp. :D So I also changed the window shape to that more of like a lava lamp, a rectangle. Press the space bar a bunch of times and this is SOOO COOL! I just wish I knew the math in it. :)

Code: QB64: [Select]
  1. 'Lava Lamp modified by Ken G. and from b+ and from SmallBASIC.
  2. _TITLE "press spacebar" ' b+ 2020-01-20 translated and modified from SmallBASIC
  3. 'Plasma Magnifico - updated 2015-11-26 for Android
  4. 'This program creates a plasma surface, which looks oily or silky.
  5.  
  6. CONST xmax = 250, ymax = 600
  7. TYPE xy
  8.     x AS SINGLE
  9.     y AS SINGLE
  10.     dx AS SINGLE
  11.     dy AS SINGLE
  12. SCREEN _NEWIMAGE(xmax, ymax, 32)
  13. _SCREENMOVE 300, 40
  14.  
  15. DIM c(360) AS _UNSIGNED LONG, p(6) AS xy, f(6)
  16. restart:
  17. r = RND: g = RND: b = RND: i = 0
  18. FOR n = 1 TO 2
  19.     r1 = r: g1 = g: b1 = b
  20.     DO: r = RND: LOOP UNTIL ABS(r - r1) > .2
  21.     DO: g = RND: LOOP UNTIL ABS(g - g1) > .2
  22.     DO: b = RND: LOOP UNTIL ABS(g - g1) > .2
  23.     FOR m = 0 TO 17: m1 = 17 - m
  24.         f1 = (m * r) / 18: f2 = (m * g) / 18: f3 = (m * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  25.     NEXT
  26.     FOR m = 0 TO 17: m1 = 17 - m
  27.         f1 = (m + m1 * r) / 18: f2 = (m + m1 * g) / 18: f3 = (m + m1 * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  28.     NEXT
  29.     FOR m = 0 TO 17: m1 = 17 - m
  30.         f1 = (m1 + m * r) / 18: f2 = (m1 + m * g) / 18: f3 = (m1 + m * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  31.     NEXT
  32.     FOR m = 0 TO 17: m1 = 17 - m
  33.         f1 = (m1 * r) / 18: f2 = (m1 * g) / 18: f3 = (m1 * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  34.     NEXT
  35.  
  36. FOR n = 0 TO 2
  37.     p(n).x = RND * xmax: p(n).y = RND * ymax: p(n).dx = RND * 2 - 1: p(n).dy = RND * 2 - 1
  38.     f(n) = RND * .05
  39.  
  40. WHILE _KEYDOWN(27) = 0
  41.     IF INKEY$ = " " THEN GOTO restart
  42.     FOR i = 0 TO 2
  43.         p(i).x = p(i).x + p(i).dx
  44.         IF p(i).x > xmax OR p(i).x < 0 THEN p(i).dx = -p(i).dx
  45.         p(i).y = p(i).y + p(i).dy
  46.         IF p(i).y > ymax OR p(i).y < 0 THEN p(i).dy = -p(i).dy
  47.     NEXT
  48.     FOR y = 0 TO ymax - 1 STEP 2
  49.         FOR x = 0 TO xmax - 1 STEP 2
  50.             d = 0
  51.             FOR n = 0 TO 2
  52.                 dx = x - p(n).x: dy = y - p(n).y
  53.                 k = SQR(dx * dx + dy * dy)
  54.                 d = d + (SIN(k * f(n)) + 1) / 2
  55.             NEXT n: d = d * 60
  56.             LINE (x, y)-STEP(2, 2), c(d), BF
  57.         NEXT
  58.     NEXT
  59.     _DISPLAY
  60.     _LIMIT 100
  61.  
  62. FUNCTION rgbf~& (n1, n2, n3)
  63.     rgbf~& = _RGB32(n1 * 255, n2 * 255, n3 * 255)
  64.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Ectoplasm
« Reply #31 on: January 23, 2020, 01:35:11 am »
Here is my mod of your mod for Lava Lamp
Code: QB64: [Select]
  1. 'Lava Lamp modified by Ken G. and from b+ and from SmallBASIC.   mod again B+
  2. _TITLE "press spacebar" ' b+ 2020-01-20 translated and modified from SmallBASIC
  3. 'Plasma Magnifico - updated 2015-11-26 for Android
  4. 'This program creates a plasma surface, which looks oily or silky.
  5.  
  6. CONST xmax = 250, ymax = 600
  7. TYPE xy
  8.     x AS SINGLE
  9.     y AS SINGLE
  10.     dx AS SINGLE
  11.     dy AS SINGLE
  12. SCREEN _NEWIMAGE(xmax, ymax, 32)
  13. _SCREENMOVE 300, 40
  14.  
  15. DIM c(360) AS _UNSIGNED LONG, p(6) AS xy, f(6)
  16.  
  17. restart:
  18. r = RND: g = RND: b = RND: i = 0
  19. FOR n = 1 TO 2
  20.     r1 = r: g1 = g: b1 = b
  21.     DO: r = RND: LOOP UNTIL ABS(r - r1) > .2
  22.     DO: g = RND: LOOP UNTIL ABS(g - g1) > .2
  23.     DO: b = RND: LOOP UNTIL ABS(g - g1) > .2
  24.     FOR m = 0 TO 17: m1 = 17 - m
  25.         f1 = (m * r) / 18: f2 = (m * g) / 18: f3 = (m * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  26.     NEXT
  27.     FOR m = 0 TO 17: m1 = 17 - m
  28.         f1 = (m + m1 * r) / 18: f2 = (m + m1 * g) / 18: f3 = (m + m1 * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  29.     NEXT
  30.     FOR m = 0 TO 17: m1 = 17 - m
  31.         f1 = (m1 + m * r) / 18: f2 = (m1 + m * g) / 18: f3 = (m1 + m * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  32.     NEXT
  33.     FOR m = 0 TO 17: m1 = 17 - m
  34.         f1 = (m1 * r) / 18: f2 = (m1 * g) / 18: f3 = (m1 * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  35.     NEXT
  36.  
  37. FOR n = 0 TO 2
  38.     p(n).x = RND * xmax: p(n).y = RND * ymax: p(n).dx = .25 * (RND * 2 - 1): p(n).dy = 2 * (RND * 2 - 1)
  39.     f(n) = .015
  40.  
  41. WHILE _KEYDOWN(27) = 0
  42.     CLS
  43.     IF INKEY$ = " " THEN GOTO restart
  44.     FOR i = 0 TO 2
  45.         p(i).x = p(i).x + p(i).dx
  46.         IF p(i).x > xmax - 50 OR p(i).x < 50 THEN p(i).dx = -p(i).dx
  47.         p(i).y = p(i).y + p(i).dy
  48.         IF p(i).y > ymax + 100 OR p(i).y < -100 THEN p(i).dy = -p(i).dy
  49.     NEXT
  50.     FOR y = 0 TO ymax - 1 STEP 2
  51.         FOR x = 0 TO xmax - 1 STEP 2
  52.             d = 0
  53.             FOR n = 0 TO 2
  54.                 dx = x - p(n).x: dy = y - p(n).y
  55.                 k = SQR(dx * dx + dy * dy)
  56.                 d = d + (SIN(k * f(n)) + 1) / 2
  57.             NEXT n: d = d * 60
  58.             LINE (x, y)-STEP(2, 2), c(d), BF
  59.         NEXT
  60.     NEXT
  61.     _DISPLAY
  62.     _LIMIT 20
  63.  
  64. FUNCTION rgbf~& (n1, n2, n3)
  65.     rgbf~& = _RGB32(n1 * 255, n2 * 255, n3 * 255)
  66.  
  67.  
  68.  

« Last Edit: January 23, 2020, 02:06:12 am by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Ectoplasm
« Reply #32 on: January 23, 2020, 01:36:52 pm »
Great job B+! Looks just like a lava lamp now pretty much.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Ectoplasm
« Reply #33 on: January 23, 2020, 05:23:06 pm »
OK I have studied the code for palette creation and can simplify the job immensely!

Code: QB64: [Select]
  1. _TITLE "Color Mixing 4 Plasmatic" ' b+ 2020-01-23
  2. ' continued study of what makes Plasmatic tick, here the color pallete created
  3.  
  4. CONST xmax = 800, ymax = 720
  5. TYPE xy
  6.     x AS SINGLE
  7.     y AS SINGLE
  8.     dx AS SINGLE
  9.     dy AS SINGLE
  10. SCREEN _NEWIMAGE(xmax, ymax, 32)
  11. _SCREENMOVE 300, 10
  12.  
  13. DIM c(360) AS _UNSIGNED LONG, p(6) AS xy, f(6), i AS INTEGER, r AS INTEGER, g AS INTEGER, b AS INTEGER, m AS INTEGER
  14. restart:
  15. FOR i = 0 TO 360 'look at pallette
  16.     IF i MOD 60 = 0 THEN r = RND * 255: g = RND * 255: b = RND * 255
  17.     m = i MOD 60
  18.     SELECT CASE m
  19.         CASE IS < 15: c(i) = midInk(0, 0, 0, r, g, b, m / 15)
  20.         CASE IS < 30: c(i) = midInk(r, g, b, 255, 255, 255, (m - 15) / 15)
  21.         CASE IS < 45: c(i) = midInk(255, 255, 255, r, g, b, (m - 30) / 15)
  22.         CASE IS < 60: c(i) = midInk(r, g, b, 0, 0, 0, (m - 45) / 15)
  23.     END SELECT
  24.  
  25. FOR n = 0 TO 5
  26.     p(n).x = RND * xmax: p(n).y = RND * ymax: p(n).dx = RND * 2 - 1: p(n).dy = RND * 2 - 1
  27.     f(n) = RND * .1
  28.  
  29. WHILE _KEYDOWN(27) = 0
  30.     IF INKEY$ = " " THEN GOTO restart
  31.     FOR i = 0 TO 5
  32.         p(i).x = p(i).x + p(i).dx
  33.         IF p(i).x > xmax OR p(i).x < 0 THEN p(i).dx = -p(i).dx
  34.         p(i).y = p(i).y + p(i).dy
  35.         IF p(i).y > ymax OR p(i).y < 0 THEN p(i).dy = -p(i).dy
  36.     NEXT
  37.     FOR y = 0 TO ymax - 1 STEP 2
  38.         FOR x = 0 TO xmax - 1 STEP 2
  39.             d = 0
  40.             FOR n = 0 TO 5
  41.                 dx = x - p(n).x: dy = y - p(n).y
  42.                 k = SQR(dx * dx + dy * dy)
  43.                 d = d + (SIN(k * f(n)) + 1) / 2
  44.             NEXT n: d = d * 60
  45.             LINE (x, y)-STEP(2, 2), c(d), BF
  46.         NEXT
  47.     NEXT
  48.     _DISPLAY
  49.     _LIMIT 100
  50.  
  51. FUNCTION midInk~& (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  52.     midInk~& = _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  53.  
  54.  

This means I might be able to do a Plasmatic background for clock face or anything with far less code PLUS really mess with Plasma coloring that previously depends on a black border and white center like coloring a pipe in fake-3D.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Ectoplasm
« Reply #34 on: January 23, 2020, 05:47:19 pm »
Amazing job there. But I wonder if the slow speed will affect the speed of any more code you add?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Ectoplasm
« Reply #35 on: January 23, 2020, 06:09:15 pm »
You were talking about getting rid of SQR...  Can’t you use _HYPOT?

                k = SQR(dx * dx + dy * dy)
k = _HYPOT(dx, dy)
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: Ectoplasm
« Reply #36 on: January 23, 2020, 10:10:07 pm »
You were talking about getting rid of SQR...  Can’t you use _HYPOT?

                k = SQR(dx * dx + dy * dy)
k = _HYPOT(dx, dy)

Thanks for suggestion, I tried it in following demo and it looks like SQR is a tiny bit faster, on my system the loop time is usually under .10 secs .09xxx with .10xxx showing more often with _HYPOT and .08xxx showing more with SQR. What is definitely slower is using ^.5 instead of SQR!


This is a demo of the new color Options available for Plasmatic effect, use t to toggle between Traditional Plasma that has high contrast Black between cells and White in cell centers, whereas the New Options available put any color between cells and any color as cell center:

again t to toggle between Traditional Plasma and New Options Plasma, use spacebar to change color sets:
Code: QB64: [Select]
  1. _TITLE "Color Mixing 4 Plasmatic" ' b+ 2020-01-23
  2. ' continued study of what makes Plasmatic tick, here the color pallete created
  3.  
  4. CONST xmax = 800, ymax = 600
  5. TYPE xy
  6.     x AS SINGLE
  7.     y AS SINGLE
  8.     dx AS SINGLE
  9.     dy AS SINGLE
  10. SCREEN _NEWIMAGE(xmax, ymax, 32)
  11. _SCREENMOVE 300, 10
  12. DIM c(360) AS _UNSIGNED LONG, p(6) AS xy, f(6), i AS INTEGER, r AS INTEGER, g AS INTEGER, b AS INTEGER, m AS INTEGER
  13. restart:
  14. IF mode < .5 THEN '<< sorry I was using RND before mode but this line still works
  15.     r1 = RND * 255: g1 = RND * 255: b1 = RND * 255
  16.     r2 = RND * 255: g2 = RND * 255: b2 = RND * 255
  17.     t$ = "t to toggle Current Mode New Plasma Option:  Between Cell Color" + STR$(r1) + STR$(g1) + STR$(b1) + "   Center Cell Color " + STR$(r2) + STR$(g2) + STR$(b2)
  18.     r2 = 255: g2 = 255: b2 = 255 'regular Plasma
  19.     r1 = 0: g1 = 0: b1 = 0
  20.     t$ = "t to toggle Current Mode Traditional Plasma:   Between Cell Color" + STR$(r1) + STR$(g1) + STR$(b1) + "   Center Cell Color" + STR$(r2) + STR$(g2) + STR$(b2)
  21.  
  22. FOR i = 0 TO 360
  23.     IF i MOD 60 = 0 THEN r = RND * 255: g = RND * 255: b = RND * 255
  24.     m = i MOD 60
  25.     SELECT CASE m
  26.         CASE IS < 15: c(i) = midInk(r1, g1, b1, r, g, b, m / 15)
  27.         CASE IS < 30: c(i) = midInk(r, g, b, r2, g2, b2, (m - 15) / 15)
  28.         CASE IS < 45: c(i) = midInk(r2, g2, b2, r, g, b, (m - 30) / 15)
  29.         CASE IS < 60: c(i) = midInk(r, g, b, r1, g1, b1, (m - 45) / 15)
  30.     END SELECT
  31.  
  32. FOR n = 0 TO 5
  33.     p(n).x = RND * xmax: p(n).y = RND * ymax: p(n).dx = RND * 2 - 1: p(n).dy = RND * 2 - 1
  34.     f(n) = .1 * RND
  35.  
  36. WHILE _KEYDOWN(27) = 0
  37.     k$ = INKEY$
  38.     IF k$ = " " THEN GOTO restart
  39.     IF k$ = "t" THEN mode = 1 - mode: GOTO restart
  40.     t = TIMER(.001)
  41.     FOR i = 0 TO 5
  42.         p(i).x = p(i).x + p(i).dx
  43.         IF p(i).x > xmax OR p(i).x < 0 THEN p(i).dx = -p(i).dx
  44.         p(i).y = p(i).y + p(i).dy
  45.         IF p(i).y > ymax OR p(i).y < 0 THEN p(i).dy = -p(i).dy
  46.     NEXT
  47.     _TITLE t$
  48.     FOR y = 0 TO ymax - 1 STEP 2
  49.         FOR x = 0 TO xmax - 1 STEP 2
  50.             d = 0
  51.             FOR n = 0 TO 5
  52.                 dx = x - p(n).x: dy = y - p(n).y
  53.                 k = SQR(dx * dx + dy * dy)
  54.                 'k = _HYPOT(dx, dy)
  55.                 d = d + (SIN(k * f(n)) + 1) / 2
  56.             NEXT n: d = d * 60
  57.             LINE (x, y)-STEP(2, 2), c(d), BF
  58.         NEXT
  59.     NEXT
  60.     LOCATE 1, 1: PRINT USING "#.####"; TIMER(.001) - t
  61.     _DISPLAY
  62.     '_LIMIT 100
  63.  
  64. FUNCTION midInk~& (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  65.     midInk~& = _RGBA32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##, 255)
  66.  
  67.  
New Plasma Option.PNG
* New Plasma Option.PNG (Filesize: 199.28 KB, Dimensions: 806x627, Views: 221)
« Last Edit: January 23, 2020, 10:16:04 pm by bplus »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Ectoplasm
« Reply #37 on: January 24, 2020, 07:35:29 am »
Plasmas are great! Flashback to the late 60's and 70's... So cool... or should that be... "Heavy man" or "Far out"?
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Ectoplasm
« Reply #38 on: January 24, 2020, 05:09:44 pm »
Regarding my Lava Lamp mod, and yours B+, I changed it a bit more today making it only red shades of color without the ability to use the Space Bar to change anything. I did this to make it more realistic for a lava lamp. I also increased some numbers inside the bottom main loop on 2 IF/THEN statements so it would keep moving longer before everything changes direction. Do you happen to know who made the original code in SmallBasic where you got this from? I ask because I want to add their name to a comment in the code thanking them for posting it on a forum.
Tell me what you think of this, thanks.

Code: QB64: [Select]
  1. 'Lava Lamp modified by Ken G. and from b+ and from SmallBASIC.   mod again B+
  2. _TITLE "Lava Lamp" '
  3. 'Plasma Magnifico - updated 2015-11-26 for Android
  4. 'This program creates a plasma surface, which looks oily or silky.
  5. 'By SmallBasic and mods by B+ and SierraKen
  6.  
  7. CONST xmax = 250, ymax = 600
  8. TYPE xy
  9.     x AS SINGLE
  10.     y AS SINGLE
  11.     dx AS SINGLE
  12.     dy AS SINGLE
  13. SCREEN _NEWIMAGE(xmax, ymax, 32)
  14. _SCREENMOVE 300, 40
  15.  
  16. DIM c(360) AS _UNSIGNED LONG, p(6) AS xy, f(6)
  17.  
  18. restart:
  19. r = RND: g = .002: b = .002: i = 0
  20. FOR n = 1 TO 2
  21.     r1 = r: g1 = g: b1 = b
  22.     DO: r = RND: LOOP UNTIL ABS(r - r1) > .55
  23.  
  24.     FOR m = 0 TO 17: m1 = 17 - m
  25.         f1 = (m * r) / 18: f2 = (m * g) / 18: f3 = (m * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  26.     NEXT
  27.     FOR m = 0 TO 17: m1 = 17 - m
  28.         f1 = (m + m1 * r) / 18: f2 = (m + m1 * g) / 18: f3 = (m + m1 * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  29.     NEXT
  30.     FOR m = 0 TO 17: m1 = 17 - m
  31.         f1 = (m1 + m * r) / 18: f2 = (m1 + m * g) / 18: f3 = (m1 + m * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  32.     NEXT
  33.     FOR m = 0 TO 17: m1 = 17 - m
  34.         f1 = (m1 * r) / 18: f2 = (m1 * g) / 18: f3 = (m1 * b) / 18: c(i) = rgbf(f1, f2, f3): i = i + 1
  35.     NEXT
  36.  
  37. FOR n = 0 TO 2
  38.     p(n).x = RND * xmax: p(n).y = RND * ymax: p(n).dx = .25 * (RND * 2 - 1): p(n).dy = 2 * (RND * 2 - 1)
  39.     f(n) = .015
  40.  
  41.     CLS
  42.     IF INKEY$ = CHR$(27) THEN END
  43.     FOR i = 0 TO 6
  44.         p(i).x = p(i).x + p(i).dx
  45.         IF p(i).x > xmax - 500 OR p(i).x < 500 THEN p(i).dx = -p(i).dx
  46.         p(i).y = p(i).y + p(i).dy
  47.         IF p(i).y > ymax + 2000 OR p(i).y < -200 THEN p(i).dy = -p(i).dy
  48.     NEXT
  49.     FOR y = 0 TO ymax - 1 STEP 2
  50.         FOR x = 0 TO xmax - 1 STEP 2
  51.             d = 0
  52.             FOR n = 0 TO 2
  53.                 dx = x - p(n).x: dy = y - p(n).y
  54.                 k = SQR(dx * dx + dy * dy)
  55.                 d = d + (SIN(k * f(n)) + 1) / 2
  56.             NEXT n: d = d * 60
  57.             LINE (x, y)-STEP(2, 2), c(d), BF
  58.         NEXT
  59.     NEXT
  60.     _DISPLAY
  61.     _LIMIT 20
  62.  
  63. FUNCTION rgbf~& (n1, n2, n3)
  64.     rgbf~& = _RGB32(n1 * 255, n2 * 55, n3 * 55)
  65.  
  66.  
« Last Edit: January 24, 2020, 05:12:52 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Ectoplasm
« Reply #39 on: January 24, 2020, 06:58:04 pm »
Quote
Do you happen to know who made the original code in SmallBasic where you got this from? I ask because I want to add their name to a comment in the code thanking them for posting it on a forum.

Hi Ken,
from reply #18 I left link to SmallBASIC Library and source code name
Quote
The program I (B+) translated and modified looked like this (plasmajvsh.bas from SmallBASIC code library,
http://smallbasic.github.io/pages/samples.html

That's all I know about it, that should be more than enough.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Ectoplasm
« Reply #40 on: January 24, 2020, 07:14:41 pm »
Thanks B+ :). So for the credit I have this now:

'Lava Lamp
_TITLE "Lava Lamp"
'This program creates a plasma surface, which looks oily or silky.
'By SmallBasic code plasmajvsh.bas from here: http://smallbasic.github.io/pages/samples.html
'and mods by B+ and SierraKen from the QB64.org forum.
« Last Edit: January 24, 2020, 07:16:20 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Ectoplasm
« Reply #41 on: January 26, 2020, 02:22:58 pm »
Last night we played around with a Nautilus like Shell program from JB forum, yeah more eye candy but can you see the little gem in this code that may have great effect on the Plasmatic effect?

Code: QB64: [Select]
  1. _TITLE "Shell of another color" 'b+2020-01-25
  2. 'inspired by "shell-like thing" by tsh73 Jan 2020 at JB
  3.  
  4. SCREEN _NEWIMAGE(600, 600, 32)
  5. DIM x(1600), y(1600), c AS _UNSIGNED LONG
  6. cx = 300: cy = 300
  7. FOR a = 0 TO _PI(8) STEP _PI(2 / 400) ' load x, y arrays
  8.     x(i) = cx + ra * COS(a): y(i) = cy + ra * SIN(a)
  9.     dr = dr + 1 / 4800: ra = ra + dr
  10.     PSET (x(i), y(i)), &HFFFFFFFF
  11.     i = i + 1
  12.     r = RND: G = RND: B = RND: PN = 0
  13.     FOR i = 0 TO 399 'this block is a cheat to fill half a hole!
  14.         dx = x(i + 400) - x(i): dy = y(i + 400) - y(i)
  15.         dist = SQR(dx * dx + dy * dy)
  16.         dx = dx / dist: dy = dy / dist
  17.         PN = PN + .5
  18.         FOR j = 0 TO dist
  19.             shade = 1 - ((dist / 2 - j + 1 / 2) / (dist / 2)) ^ 2
  20.             c = _RGB32(shade * INT(127 + 127 * SIN(r * PN)), shade * INT(127 + 127 * SIN(G * PN)), shade * INT(127 + 127 * SIN(B * PN)))
  21.             fcirc cx + j * dx, cy + j * dy, 1, c
  22.         NEXT
  23.     NEXT
  24.     PN = PN - 200
  25.     FOR i = 0 TO 1199
  26.         dx = x(i + 400) - x(i): dy = y(i + 400) - y(i)
  27.         dist = SQR(dx * dx + dy * dy)
  28.         dx = dx / dist: dy = dy / dist
  29.         PN = PN + .5
  30.         FOR j = 0 TO dist
  31.             shade = 1 - ((dist / 2 - j + 1 / 2) / (dist / 2)) ^ 2
  32.             c = _RGB32(shade * INT(127 + 127 * SIN(r * PN)), shade * INT(127 + 127 * SIN(G * PN)), shade * INT(127 + 127 * SIN(B * PN)))
  33.             fcirc x(i) + j * dx, y(i) + j * dy, 2, c
  34.         NEXT
  35.     NEXT
  36.     _DISPLAY
  37.     _DELAY 2
  38.  
  39. 'from Steve Gold standard
  40. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  41.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  42.     DIM X AS INTEGER, Y AS INTEGER
  43.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  44.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  45.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  46.     WHILE X > Y
  47.         RadiusError = RadiusError + Y * 2 + 1
  48.         IF RadiusError >= 0 THEN
  49.             IF X <> Y + 1 THEN
  50.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  51.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  52.             END IF
  53.             X = X - 1
  54.             RadiusError = RadiusError - X * 2
  55.         END IF
  56.         Y = Y + 1
  57.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  58.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  59.     WEND
  60.  
  61.  


I haven't tried to recode Plasmatic yet but I am pretty sure it will be a very nice modification.
Shell of another color QB64.PNG
* Shell of another color QB64.PNG (Filesize: 253.56 KB, Dimensions: 601x623, Views: 227)
« Last Edit: January 26, 2020, 03:50:11 pm by bplus »

Marked as best answer by bplus on January 26, 2020, 10:51:49 am

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Ectoplasm
« Reply #42 on: January 26, 2020, 03:46:32 pm »
Yes the new color options are no longer so flat looking, so here is best yet Plasmatic 5:

Code: QB64: [Select]
  1. _TITLE "Plasmatic 5 Color Shading" ' b+ 2020-01-26
  2. ' Hopefully this will add shading to the new color options found in Pasmatic 4.
  3.  
  4. CONST xxmax = 800, yymax = 600, xmax = 900, ymax = 740, xoff = (xmax - xxmax) \ 2, yoff = (ymax - yymax) \ 2
  5. TYPE xy
  6.     x AS SINGLE
  7.     y AS SINGLE
  8.     dx AS SINGLE
  9.     dy AS SINGLE
  10. SCREEN _NEWIMAGE(xmax, ymax, 32)
  11. DIM c(360) AS _UNSIGNED LONG, p(6) AS xy, f(6)
  12. DIM i AS INTEGER, m AS INTEGER, n AS INTEGER, mode AS INTEGER, k$, t, x, y, d, dx, dy, dist, s
  13.  
  14. restart: 'select rgb1 and rgb2 based on mode of color mixing
  15. IF mode = 0 THEN 'new plasma option ANY color for border and ANY color for middle
  16.     r1 = RND * 255: g1 = RND * 255: b1 = RND * 255: r2 = RND * 255: g2 = RND * 255: b2 = RND * 255
  17. ELSE ' traditional high contrast plasma black borders, white centers
  18.     r1 = 0: g1 = 0: b1 = 0: r2 = 255: g2 = 255: b2 = 255 'regular Plasma
  19.  
  20. ' create 6 x 60 bands of color palette based on coloring mode (rgb1 set and rgb2 set)
  21. FOR i = 0 TO 360
  22.     IF i MOD 60 = 0 THEN r = RND * 255: g = RND * 255: b = RND * 255
  23.     m = i MOD 60
  24.     s = 1 - ((30 - m + .5) / 30) ^ 2
  25.     SELECT CASE m
  26.         CASE IS < 15: c(i) = midInk(s * r1, s * g1, s * b1, s * r, s * g, s * b, m / 15) '        1st stage increase rgb1 towards rgb color in 15 steps
  27.         CASE IS < 30: c(i) = midInk(s * r, s * g, s * b, s * r2, s * g2, s * b2, (m - 15) / 15) ' 2nd stage increase rgb color towards rgb2 set in 15 steps
  28.         CASE IS < 45: c(i) = midInk(s * r2, s * g2, s * b2, s * r, s * g, s * b, (m - 30) / 15) ' 3rd stage decrease rgb2 color back to rgb color in 15 steps
  29.         CASE IS < 60: c(i) = midInk(s * r, s * g, s * b, s * r1, s * g1, s * b1, (m - 45) / 15) ' 4th and finally decrease rgb back to starting rgb1 set in 15 steps
  30.     END SELECT
  31.  
  32. ' behind the scenes variables for motion, weighting and shaping color mixing
  33. FOR n = 0 TO 5
  34.     p(n).x = RND * xmax: p(n).y = RND * yymax: p(n).dx = RND * 2 - 1: p(n).dy = RND * 2 - 1
  35.     f(n) = .1 * RND
  36.  
  37. 'screen labeling 3 lines for title above, 1 line instructions below
  38. IF mode = 0 THEN
  39.     yCP yoff - 60, "New Color Options for Plasma:"
  40.     yCP yoff - 60, "Traditional High Contrast Plasma: Black Borders and White Centers"
  41. yCP yoff - 40, "Shaded Borders: RGB(" + TS$(r1 \ 1) + ", " + TS$(g1 \ 1) + ", " + TS$(b1 \ 1) + ")"
  42. yCP yoff - 20, "Centers: RGB(" + TS$(r2 \ 1) + ", " + TS$(g2 \ 1) + ", " + TS$(b2 \ 1) + ")"
  43. yCP yoff + yymax + 10, "Press t to toggle between Traditional and New Color Options Plasma"
  44. yCP yoff + yymax + 30, "Press spacebar to get a new color set."
  45.  
  46. WHILE _KEYDOWN(27) = 0
  47.     k$ = INKEY$
  48.     IF k$ = " " THEN GOTO restart
  49.     IF k$ = "t" THEN mode = 1 - mode: GOTO restart
  50.     t = TIMER(.001)
  51.     FOR i = 0 TO 5
  52.         p(i).x = p(i).x + p(i).dx
  53.         IF p(i).x > xxmax THEN p(i).dx = -p(i).dx: p(i).x = xxmax
  54.         IF p(i).x < 0 THEN p(i).dx = -p(i).dx: p(i).x = 0
  55.         p(i).y = p(i).y + p(i).dy
  56.         IF p(i).y > xxmax THEN p(i).dy = -p(i).dy: p(i).y = yymax
  57.         IF p(i).y < 0 THEN p(i).dy = -p(i).dy: p(i).y = 0
  58.     NEXT
  59.     FOR y = 0 TO yymax - 1 STEP 2
  60.         FOR x = 0 TO xxmax - 1 STEP 2
  61.             d = 0
  62.             FOR n = 0 TO 5
  63.                 dx = x - p(n).x: dy = y - p(n).y
  64.                 dist = SQR(dx * dx + dy * dy)
  65.                 'dist = _HYPOT(dx, dy)    'this may work faster on another system
  66.                 d = d + (SIN(dist * f(n)) + 1) / 2
  67.             NEXT n: d = d * 60
  68.             LINE (x + xoff, y + yoff)-STEP(2, 2), c(d), BF
  69.         NEXT
  70.     NEXT
  71.     yCP yoff + yymax + 50, SPACE$(50)
  72.     yCP yoff + yymax + 50, TS$(INT(1000 * (TIMER(.001) - t))) + " ms per frame"
  73.     _DISPLAY
  74.  
  75. FUNCTION midInk~& (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  76.     midInk~& = _RGBA32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##, 255)
  77.  
  78. SUB yCP (y, s$) 'for xmax pixel wide graphics screen Center Print at pixel y row
  79.     _PRINTSTRING ((_WIDTH - LEN(s$) * 8) / 2, y), s$
  80.  
  81.     TS$ = _TRIM$(STR$(n))
  82.  
  83.  

Plasmatic 5 Shading.PNG
* Plasmatic 5 Shading.PNG (Filesize: 212.31 KB, Dimensions: 903x768, Views: 243)

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Ectoplasm
« Reply #43 on: January 26, 2020, 04:07:35 pm »
Heya bplus-

Pretty bang-up job, lots of results crammed into a relatively small amount of code.

Maybe it's my system, or maybe this is a familiar demon from the old plasma days, but it runs slow on my end. This immediately caused me to look at this part:

Code: QB64: [Select]
  1.                 dx = x - p(n).x: dy = y - p(n).y
  2.                 dist = SQR(dx * dx + dy * dy)
  3.                 'dist = _HYPOT(dx, dy)    'this may work faster on another system
  4.                 d = d + (SIN(dist * f(n)) + 1) / 2

Two things that probably don't need pointing out, but here they are:

1) You can speed things up by using a lookup table for the SIN() function. Even a crappy lookup table with an interpolation formula will certainly get you the precision you need, but probably be faster than the native SIN() function. They did this in the old days, I can only imagine it works now.

2) I see you fiddled with different ways to calculate the square root. Two-and-a-half thoughts on this: (1) Just leave it squared, you'll probably get the same kind of results anyway. (1a) Leave it squared but modify f(n) to compensate, if you can. (2) Use a lookup table for square roots. Linear interpolation for the in-between values will only be arithmetic and won't ever involve a square root in the loop.

I suppose you can also optimize in the direction of _MEM, but it becomes very nontransparent after that.

The third option is I upgrade my system........
« Last Edit: January 26, 2020, 04:17:40 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Ectoplasm
« Reply #44 on: January 26, 2020, 04:20:34 pm »
Hi STxAxTIC,

How slow is it running for you?

More options for speed,
- decrease xxmax, yymax those are the inner screen dimensions
- decrease number n of points, really 2 or 3 are enough each point is another cycle over the whole drawing area.
- increase step size on x, y through xxmax and yymaxx
I had to employ all these for SmallBASIC which is only interpreted Basic.

I don't think tables would be practical (800 * 600)^2 x1, y1, x2, y2 the distance from any point to any other = 2.304*10^11 values.

I will try the suggestion of not squaring.