Author Topic: Colorful Triangle Geometry  (Read 4984 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Colorful Triangle Geometry
« on: May 04, 2021, 02:01:02 am »
Hi everyone!
I made a program which makes colorful and beautiful pattern full of triangles.
I hope you will like like it. :)

Code: QB64: [Select]
  1. 'Coded By Ashish
  2. '4 may, 2021
  3. 'edited : 9 may,2021
  4. _Title "Triangular Geometry"
  5. Screen _NewImage(800, 600, 32)
  6. 404 pattern 400, 0, 0, 600, 800, 600, 600, Int(Rnd * 255), 50 + Int(Rnd * 205)
  7. _Delay 0.25
  8. GoTo 404
  9. Sub pattern (x0, y0, x1, y1, x2, y2, d_max, s_min, s_width)
  10.     mx = (x1 + x2) / 2: my = (y1 + y2) / 2
  11.     d = Int(Sqr((mx - x0) ^ 2 + (my - y0) ^ 2))
  12.     drawTriangleSolid x0, y0, x1, y1, x2, y2, hsb(map(d, 10, d_max, s_min, (s_max + s_width) Mod 255), 255, 128, map(d, 10, d_max, 50, 255))
  13.     If d > 10 Then
  14.         If d >= d_max Then
  15.             pattern mx, my, x0, y0, x1, y1, d_max, s_min, s_width
  16.             pattern mx, my, x0, y0, x2, y2, d_max, s_min, s_width
  17.  
  18.         Else
  19.             If Rnd > 0.5 Then pattern mx, my, x0, y0, x1, y1, d_max, s_min, s_width Else pattern mx, my, x0, y0, x2, y2, d_max, s_min, s_width
  20.         End If
  21.     End If
  22.  
  23. Sub drawTriangleSolid (x1, y1, x2, y2, x3, y3, c~&)
  24.     Static temp_color_img As Long, drawTriangle_init As _Byte
  25.     If drawTriangle_init = 0 Then
  26.         temp_color_img = _NewImage(1, 1, 32)
  27.         drawTriangle_init = 1
  28.     End If
  29.     preDest = _Dest
  30.     _Dest temp_color_img
  31.     PSet (0, 0), c~&
  32.     _Dest preDest
  33.  
  34.     _MapTriangle (0, 0)-(0, 0)-(0, 0), temp_color_img To(x1, y1)-(x2, y2)-(x3, y3), 0, _Smooth
  35.  
  36.  
  37. Sub drawTriangle (x1, y1, x2, y2, x3, y3, c~&)
  38.     Line (x1, y1)-(x2, y2), c~&
  39.     Line (x2, y2)-(x3, y3), c~&
  40.     Line (x3, y3)-(x1, y1), c~&
  41.  
  42. 'method adapted form http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors
  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 = map(__H, 0, 255, 0, 360)
  47.     S = map(__S, 0, 255, 0, 1)
  48.     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.  
  102.  
  103. 'from p5js.bas
  104. Function map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  105.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  106.  
  107.  


SCRST_1.png
« Last Edit: May 09, 2021, 01:13:31 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 johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Colorful Triangle Geometry
« Reply #1 on: May 04, 2021, 08:10:15 am »
I was all set to ask, "Does it come in blue?".... Never mind.... Bonus: I like it...
Logic is the beginning of wisdom.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Colorful Triangle Geometry
« Reply #2 on: May 05, 2021, 01:45:12 am »
nice use of recursion, reminds me of

Code: QB64: [Select]
  1. pi = 4*atn(1)
  2. c = 2*sqr(3)/9
  3. cc = sqr(12) / 6
  4.  
  5. sw = 1024
  6. sh = 768
  7.  
  8. screen _newimage(sw, sh, 12)
  9. tri sw/2, sh/2, 600, 0
  10. tri sw/2, sh/2, 600, pi
  11. kock sw/2, sh/2, 600, 3
  12.  
  13. sub kock(x, y, s, i)
  14.         if i = 0 then exit sub
  15.         for a = pi/6 to 2*pi + pi/6 step pi/3
  16.                 xx = s*c*cos(a) + x
  17.                 yy = s*c*sin(a) + y
  18.        
  19.                 tri xx, yy, s/3, a+pi/6
  20.                 tri xx, yy, s/3, a+pi/6+pi
  21.  
  22.                 kock xx, yy, s/3, i - 1
  23.         next
  24.  
  25. sub tri(x, y, s, a)
  26.         line (x,y)-(x+s*cc*cos(pi/6 + a),y+s*cc*sin(pi/6 + a)),8
  27.         line (x,y)-(x+s*cc*cos(5*pi/6 + a),y+s*cc*sin(5*pi/6 + a)),8
  28.         line (x,y)-(x-s*cc*cos(pi/2 + a),y-s*cc*sin(pi/2 + a)),8
  29.  
  30.         line (x+s*cc*cos(pi/6 + a),y+s*cc*sin(pi/6 + a))-(x+s*cc*cos(5*pi/6 + a),y+s*cc*sin(5*pi/6 + a))
  31.         line-(x-s*cc*cos(pi/2 + a),y-s*cc*sin(pi/2 + a))
  32.         line-(x+s*cc*cos(pi/6 + a),y+s*cc*sin(pi/6 + a))
  33.  

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Colorful Triangle Geometry
« Reply #3 on: May 05, 2021, 02:22:35 am »
Thank you Johnno and Vince!
I like that program Vince!, Thanks for sharing
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: Colorful Triangle Geometry
« Reply #4 on: May 07, 2021, 01:34:16 pm »
@Ashish
cool ! speedy maptriangles colors.
I find it very good.
Just a feedback....
 it sometimes uses color very near in the spectrum of visible and my old eyes cannot distinguish them
 like in this screenshot
 
Ashish triangles.jpg
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Colorful Triangle Geometry
« Reply #5 on: May 07, 2021, 01:39:10 pm »
@vince
hey man with your math geometry I'm loosing my mind in its different prospectives
se here
 
prospective of _Vince.jpg


... I'm thinking how it can appear if it uses  many colors
Programming isn't difficult, only it's  consuming time and coffee

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Colorful Triangle Geometry
« Reply #6 on: May 09, 2021, 01:13:55 am »
@TempodiBasic
Thanks! I have updated the code now... I hope it works fine for you now
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 Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Colorful Triangle Geometry
« Reply #7 on: May 09, 2021, 01:17:24 am »
Here's a little modification I made to this -

Code: QB64: [Select]
  1. 'Coded By Ashish
  2. '4 may, 2021
  3. 'edited : 9 may,2021
  4. _Title "Triangular Geometry"
  5. Screen _NewImage(800, 600, 32)
  6. '404 pattern 400, 0, 0, 600, 800, 600, 600, Int(Rnd * 255), 50 + Int(Rnd * 205)
  7. Dim vx(2), vy(2)
  8. Dim ox, oy, r, start_ang, h, clr_start, clr_width
  9.     'creating new set of vertices of equilateral triangle
  10.     ox = Int(Rnd * 800): oy = Int(Rnd * 600)
  11.     r = 50 + Int(Rnd * 200)
  12.     start_ang = Rnd * _Pi(2)
  13.     n = 0
  14.     'Print start_ang, start_ang + _Pi(2)
  15.     For i = start_ang To start_ang + _Pi(2) Step _Pi(0.67)
  16.         'Print i
  17.         vx(n) = ox + r * Cos(i): vy(n) = oy + r * Sin(i)
  18.         n = n + 1
  19.     Next
  20.     h = r * 1.5
  21.     'color settings
  22.     clr_start = Int(Rnd * 255): clr_width = 50 + Int(Rnd * 255)
  23.     pattern vx(0), vy(0), vx(1), vy(1), vx(2), vy(2), h, clr_start, clr_width
  24.     _Display
  25.     _Limit 10
  26. '_Display
  27. '_Delay 0.25
  28. 'GoTo 404
  29. Sub pattern (x0, y0, x1, y1, x2, y2, d_max, s_min, s_width)
  30.     mx = (x1 + x2) / 2: my = (y1 + y2) / 2
  31.     d = Int(Sqr((mx - x0) ^ 2 + (my - y0) ^ 2))
  32.     drawTriangleSolid x0, y0, x1, y1, x2, y2, hsb(map(d, 10, d_max, s_min, (s_max + s_width) Mod 255), 255, 128, map(d, 10, d_max, 50, 255))
  33.     If d > 10 Then
  34.         If d >= d_max Then
  35.             pattern mx, my, x0, y0, x1, y1, d_max, s_min, s_width
  36.             pattern mx, my, x0, y0, x2, y2, d_max, s_min, s_width
  37.  
  38.         Else
  39.             If Rnd > 0.5 Then pattern mx, my, x0, y0, x1, y1, d_max, s_min, s_width Else pattern mx, my, x0, y0, x2, y2, d_max, s_min, s_width
  40.         End If
  41.     End If
  42.  
  43. Sub drawTriangleSolid (x1, y1, x2, y2, x3, y3, c~&)
  44.     Static temp_color_img As Long, drawTriangle_init As _Byte
  45.     If drawTriangle_init = 0 Then
  46.         temp_color_img = _NewImage(1, 1, 32)
  47.         drawTriangle_init = 1
  48.     End If
  49.     preDest = _Dest
  50.     _Dest temp_color_img
  51.     PSet (0, 0), c~&
  52.     _Dest preDest
  53.  
  54.     _MapTriangle (0, 0)-(0, 0)-(0, 0), temp_color_img To(x1, y1)-(x2, y2)-(x3, y3), 0, _Smooth
  55.  
  56.  
  57. Sub drawTriangle (x1, y1, x2, y2, x3, y3, c~&)
  58.     Line (x1, y1)-(x2, y2), c~&
  59.     Line (x2, y2)-(x3, y3), c~&
  60.     Line (x3, y3)-(x1, y1), c~&
  61.  
  62. 'method adapted form http://stackoverflow.com/questions/4106363/converting-rgb-to-hsb-colors
  63. Function hsb~& (__H As _Float, __S As _Float, __B As _Float, A As _Float)
  64.     Dim H As _Float, S As _Float, B As _Float
  65.  
  66.     H = map(__H, 0, 255, 0, 360)
  67.     S = map(__S, 0, 255, 0, 1)
  68.     B = map(__B, 0, 255, 0, 1)
  69.  
  70.     If S = 0 Then
  71.         hsb~& = _RGBA32(B * 255, B * 255, B * 255, A)
  72.         Exit Function
  73.     End If
  74.  
  75.     Dim fmx As _Float, fmn As _Float
  76.     Dim fmd As _Float, iSextant As Integer
  77.     Dim imx As Integer, imd As Integer, imn As Integer
  78.  
  79.     If B > .5 Then
  80.         fmx = B - (B * S) + S
  81.         fmn = B + (B * S) - S
  82.     Else
  83.         fmx = B + (B * S)
  84.         fmn = B - (B * S)
  85.     End If
  86.  
  87.     iSextant = Int(H / 60)
  88.  
  89.     If H >= 300 Then
  90.         H = H - 360
  91.     End If
  92.  
  93.     H = H / 60
  94.     H = H - (2 * Int(((iSextant + 1) Mod 6) / 2))
  95.  
  96.     If iSextant Mod 2 = 0 Then
  97.         fmd = (H * (fmx - fmn)) + fmn
  98.     Else
  99.         fmd = fmn - (H * (fmx - fmn))
  100.     End If
  101.  
  102.     imx = _Round(fmx * 255)
  103.     imd = _Round(fmd * 255)
  104.     imn = _Round(fmn * 255)
  105.  
  106.     Select Case Int(iSextant)
  107.         Case 1
  108.             hsb~& = _RGBA32(imd, imx, imn, A)
  109.         Case 2
  110.             hsb~& = _RGBA32(imn, imx, imd, A)
  111.         Case 3
  112.             hsb~& = _RGBA32(imn, imd, imx, A)
  113.         Case 4
  114.             hsb~& = _RGBA32(imd, imn, imx, A)
  115.         Case 5
  116.             hsb~& = _RGBA32(imx, imn, imd, A)
  117.         Case Else
  118.             hsb~& = _RGBA32(imx, imd, imn, A)
  119.     End Select
  120.  
  121.  
  122.  
  123. 'from p5js.bas
  124. Function map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  125.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  126.  
  127.  
SCRST_1.png
* SCRST_1.png (Filesize: 41.55 KB, Dimensions: 803x627, Views: 279)
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: Colorful Triangle Geometry
« Reply #8 on: May 09, 2021, 11:54:27 am »
@Ashish
very funny!
It works well.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Colorful Triangle Geometry
« Reply #9 on: May 11, 2021, 12:49:19 am »
Well @Ashish color and recursive subs, you know I had to join the fun!

Here is my rendition:
Code: QB64: [Select]
  1. _Title "Recursively Divide a Triangle into 2" 'b+ 2021-05-11
  2. ' inspired by Ashish post here:  https://www.qb64.org/forum/index.php?topic=3867.msg132129#msg132129
  3. ' Find the longest side middle point and draw line from that to angle point opposite that side.
  4. ' Then pass the 2 new tri's back to recursive sub until the longest side < 20, not 10
  5. ' See what happens... Ok way way to boring we need random filling and color!
  6. Screen _NewImage(1200, 600, 32)
  7. _Delay .25
  8.     Cls
  9.     DivideTriTo2 600, 0, 0, 600, 1200, 600
  10.     _Delay 2
  11.  
  12. Sub DivideTriTo2 (x1, y1, x2, y2, x3, y3)
  13.     s1 = _Hypot(x1 - x2, y1 - y2)
  14.     s2 = _Hypot(x3 - x2, y3 - y2)
  15.     s3 = _Hypot(x1 - x3, y1 - y3)
  16.     ftri x1, y1, x2, y2, x3, y3, _RGB32(Rnd * 255, Rnd * 255, Rnd * 255, Rnd * 255)
  17.     _Delay .002
  18.     If s1 >= s2 And s1 >= s3 And s1 > 20 Then
  19.         ptx = x3: pty = y3
  20.         mx = (x1 + x2) / 2: my = (y1 + y2) / 2
  21.         If Rnd < .75 Then DivideTriTo2 mx, my, ptx, pty, x1, y1
  22.         If Rnd < .75 Then DivideTriTo2 mx, my, ptx, pty, x2, y2
  23.     ElseIf s2 >= s1 And s2 >= s3 And s2 > 20 Then
  24.         ptx = x1: pty = y1
  25.         mx = (x3 + x2) / 2: my = (y3 + y2) / 2
  26.         If Rnd < .75 Then DivideTriTo2 mx, my, ptx, pty, x2, y2
  27.         If Rnd < .75 Then DivideTriTo2 mx, my, ptx, pty, x3, y3
  28.     ElseIf s3 >= s1 And s3 >= s2 And s3 > 20 Then
  29.         ptx = x2: pty = y2
  30.         mx = (x1 + x3) / 2: my = (y1 + y3) / 2
  31.         If Rnd < .75 Then DivideTriTo2 mx, my, ptx, pty, x1, y1
  32.         If Rnd < .75 Then DivideTriTo2 mx, my, ptx, pty, x3, y3
  33.     End If
  34.  
  35. '2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
  36. Sub ftri (x1, y1, x2, y2, x3, y3, K As _Unsigned Long)
  37.     Dim D As Long
  38.     Static a&
  39.     D = _Dest
  40.     If a& = 0 Then a& = _NewImage(1, 1, 32)
  41.     _Dest a&
  42.     _DontBlend a& '  '<<<< new 2019-12-16 fix
  43.     PSet (0, 0), K
  44.     _Blend a& '<<<< new 2019-12-16 fix
  45.     _Dest D
  46.     _MapTriangle _Seamless(0, 0)-(0, 0)-(0, 0), a& To(x1, y1)-(x2, y2)-(x3, y3)
  47.  

 
Recursively Divide Tri Into 2.PNG