Author Topic: 2 Point Gradient  (Read 2727 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
2 Point Gradient
« on: August 03, 2018, 10:47:10 pm »
It's almost plasma like!

Code: QB64: [Select]
  1. _TITLE "2 point gradient"
  2.  
  3. DEFLNG A-Z
  4. CONST wW = 800
  5. CONST wH = 600
  6. SCREEN _NEWIMAGE(wW, wH, 32)
  7. _SCREENMOVE 200, 40
  8. d = 1
  9. k1 = _RGB32(128, 0, 0)
  10. k2 = _RGB(0, 128, 255)
  11.  
  12. 'test 2 point gradient
  13.     twoPtGrad 100, 100, 600, 400, RND * wW, RND * wH, RND * wW, RND * wH, k1, k2
  14.     _DISPLAY
  15.     _LIMIT 5
  16.  
  17. SUB twoPtGrad (x0, y0, w, h, x1, y1, x2, y2, c1, c2)
  18.     FOR y = y0 TO y0 + h
  19.         FOR x = x0 TO x0 + w
  20.             d1 = ((x1 - x) ^ 2 + (y1 - y) ^ 2) ^ .5
  21.             d2 = ((x2 - x) ^ 2 + (y2 - y) ^ 2) ^ .5
  22.             midInk c1, c2, d1 / (d1 + d2)
  23.             PSET (x, y)
  24.         NEXT
  25.     NEXT
  26.  
  27. 'c1 color to left, c2 color to right
  28. SUB horzGradRec (x0, y0, w, h, c1, c2)
  29.     FOR cx = 0 TO w
  30.         midInk c1, c2, cx / w
  31.         LINE (x0 + cx, y0)-STEP(0, h), , BF
  32.     NEXT
  33.  
  34. SUB vertGradRec (x0, y0, w, h, c1, c2)
  35.     FOR cy = 0 TO h
  36.         midInk c1, c2, cy / h
  37.         LINE (x0, y0 + cy)-STEP(w, 0), , BF
  38.     NEXT
  39.  
  40. 'let c1 be outer most color c2 the inner most
  41. SUB gradCirc (x0, y0, r, c1, c2)
  42.     FOR cr = r TO 0 STEP -1
  43.         midInk c2, c1, cr / r
  44.         fcirc x0, y0, cr
  45.     NEXT
  46.  
  47. ' let fr## be the fraction from 1st color to 2nd color 0 means all color 1, 1 means all color 2
  48. SUB midInk (c1, c2, fr##)
  49.     r1 = _RED(c1): g1 = _GREEN(c1): b1 = _BLUE(c1)
  50.     r2 = _RED(c2): g2 = _GREEN(c2): b2 = _BLUE(c2)
  51.     COLOR _RGB(r1 + (r2 - r1) * fr##, g1 + (g2 - g1) * fr##, b1 + (b2 - b1) * fr##)
  52.  
  53. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  54.     DIM Radius AS LONG, RadiusError AS LONG
  55.     DIM X AS LONG, Y AS LONG
  56.  
  57.     Radius = ABS(R)
  58.     RadiusError = -Radius
  59.     X = Radius
  60.     Y = 0
  61.  
  62.     IF Radius = 0 THEN PSET (CX, CY): EXIT SUB
  63.  
  64.     ' Draw the middle span here so we don't draw it twice in the main loop,
  65.     ' which would be a problem with blending turned on.
  66.     LINE (CX - X, CY)-(CX + X, CY), , BF
  67.  
  68.     WHILE X > Y
  69.         RadiusError = RadiusError + Y * 2 + 1
  70.         IF RadiusError >= 0 THEN
  71.             IF X <> Y + 1 THEN
  72.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  73.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  74.             END IF
  75.             X = X - 1
  76.             RadiusError = RadiusError - X * 2
  77.         END IF
  78.         Y = Y + 1
  79.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  80.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  81.     WEND
  82.  
  83.  
  84.  

Note: the points don't even have to be in the area displayed.
« Last Edit: August 03, 2018, 10:50:52 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: 2 Point Gradient
« Reply #1 on: August 04, 2018, 12:17:12 am »
2 Point Gradient Swirl:
Code: QB64: [Select]
  1. _TITLE "2 point gradient swirl"
  2. DEFLNG A-Z
  3. CONST wW = 800
  4. CONST wH = 600
  5. SCREEN _NEWIMAGE(wW, wH, 32)
  6. _SCREENMOVE 200, 40
  7. k1 = _RGB32(128, 0, 0)
  8. k2 = _RGB32(0, 128, 255)
  9. incr! = _PI(1 / 90)
  10. p2! = _PI(4)
  11. cx = wW / 2
  12. cy = wH / 2
  13. 'test 2 point gradient
  14.     a! = a! + incr!
  15.     IF a! >= p2! THEN a! = 0: CLS
  16.     x1 = cx + 15 * a! ^ 1.25 * COS(a! + _PI)
  17.     y1 = cy + 15 * a! ^ 1.25 * SIN(a! + _PI)
  18.     x2 = cx + 15 * a! ^ 1.25 * COS(a!)
  19.     y2 = cy + 15 * a! ^ 1.25 * SIN(a!)
  20.     twoPtGrad 150, 50, 500, 500, x1, y1, x2, y2, k1, k2
  21.     _DISPLAY
  22.     _LIMIT 10
  23.  
  24. SUB twoPtGrad (x0, y0, w, h, x1, y1, x2, y2, c1, c2)
  25.     FOR y = y0 TO y0 + h
  26.         FOR x = x0 TO x0 + w
  27.             d1 = ((x1 - x) ^ 2 + (y1 - y) ^ 2) ^ .5
  28.             d2 = ((x2 - x) ^ 2 + (y2 - y) ^ 2) ^ .5
  29.             midInk c1, c2, d1 / (d1 + d2)
  30.             PSET (x, y)
  31.         NEXT
  32.     NEXT
  33.  
  34. 'c1 color to left, c2 color to right
  35. SUB horzGradRec (x0, y0, w, h, c1, c2)
  36.     FOR cx = 0 TO w
  37.         midInk c1, c2, cx / w
  38.         LINE (x0 + cx, y0)-STEP(0, h), , BF
  39.     NEXT
  40.  
  41. SUB vertGradRec (x0, y0, w, h, c1, c2)
  42.     FOR cy = 0 TO h
  43.         midInk c1, c2, cy / h
  44.         LINE (x0, y0 + cy)-STEP(w, 0), , BF
  45.     NEXT
  46.  
  47. 'let c1 be outer most color c2 the inner most
  48. SUB gradCirc (x0, y0, r, c1, c2)
  49.     FOR cr = r TO 0 STEP -1
  50.         midInk c2, c1, cr / r
  51.         fcirc x0, y0, cr
  52.     NEXT
  53.  
  54. ' let fr## be the fraction from 1st color to 2nd color 0 means all color 1, 1 means all color 2
  55. SUB midInk (c1, c2, fr##)
  56.     r1 = _RED(c1): g1 = _GREEN(c1): b1 = _BLUE(c1)
  57.     r2 = _RED(c2): g2 = _GREEN(c2): b2 = _BLUE(c2)
  58.     COLOR _RGB32(r1 + (r2 - r1) * fr##, g1 + (g2 - g1) * fr##, b1 + (b2 - b1) * fr##, 3)
  59.  
  60. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  61.     DIM Radius AS LONG, RadiusError AS LONG
  62.     DIM X AS LONG, Y AS LONG
  63.  
  64.     Radius = ABS(R)
  65.     RadiusError = -Radius
  66.     X = Radius
  67.     Y = 0
  68.  
  69.     IF Radius = 0 THEN PSET (CX, CY): EXIT SUB
  70.  
  71.     ' Draw the middle span here so we don't draw it twice in the main loop,
  72.     ' which would be a problem with blending turned on.
  73.     LINE (CX - X, CY)-(CX + X, CY), , BF
  74.  
  75.     WHILE X > Y
  76.         RadiusError = RadiusError + Y * 2 + 1
  77.         IF RadiusError >= 0 THEN
  78.             IF X <> Y + 1 THEN
  79.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  80.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  81.             END IF
  82.             X = X - 1
  83.             RadiusError = RadiusError - X * 2
  84.         END IF
  85.         Y = Y + 1
  86.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  87.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  88.     WEND
  89.  
  90.  
  91.  

Offline keybone

  • Forum Regular
  • Posts: 116
  • My name a Nursultan Tulyakbay.
Re: 2 Point Gradient
« Reply #2 on: August 04, 2018, 04:09:57 am »
Nice gradients... Between the different ones people have written in the past few days we have quite a collection. :)
I am from a Kazakhstan, we follow the hawk.