November 28, 2020, 02:13:33 AM

Author Topic: [bb] Fractal Triangles by zoqfotpik [ 1+ years ago ]  (Read 755 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Fractal Triangles by zoqfotpik [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Fractal Triangles
Author : zoqfotpik
Posted : 1+ years ago

Description : This draws recursive triangles.  Subdivision by midpoint is a very useful tool for a number of things, including processing of data with large contiguous blocks.

Code :
Code: BlitzBasic
  1. ' Recursive subdivision of triangles
  2.  
  3. Global tri#[]
  4. Global tmppoint = New point
  5. Global seed:Int=0
  6. Global SCREENWIDTH=1600
  7. Global SCREENHEIGHT=1000
  8. Global COLORXFACTOR = 255/SCREENWIDTH
  9. Global COLORYFACTOR = 255/SCREENHEIGHT
  10. Global MAXITER:Int = 7
  11. Global MIDTRIANGLEITER:Int = 4
  12. Global HOLDSTILL:Int = 0
  13. Global TRI1:Int = 1
  14. Global TRI2:Int = 1
  15. Global TRI3:Int = 1
  16.  
  17. midpoint:point = New point
  18. Global vertex1:point=New point
  19. Global vertex2:point=New point
  20. Global vertex3:point=New point
  21.  
  22. Graphics SCREENWIDTH,SCREENHEIGHT
  23. While Not KeyHit(KEY_ESCAPE)
  24.         Cls
  25.                 If HOLDSTILL = 0
  26.                         vertex1.update()
  27.                         vertex2.update()
  28.                         vertex3.update()
  29.                 Else
  30.                         vertex1.x = 0
  31.                         vertex1.y = SCREENHEIGHT
  32.                         vertex2.x = SCREENWIDTH/2
  33.                         vertex2.y = 0
  34.                         vertex3.x = SCREENWIDTH
  35.                         vertex3.y = SCREENHEIGHT
  36.                 EndIf
  37.                
  38.                 seed = 0
  39.                 recursivesubdivide(vertex1,vertex2,vertex3,1)
  40.                 SetColor 255,255,255
  41.                 DrawText "Outer Triangle Iterations (change with q / a):"+maxiter, 10, 10
  42.                 DrawText "Inner Triangle Iterations (change with w / s):"+midtriangleiter , 10, 25
  43.                 DrawText "Toggle Hold Still with Spacebar", 10, 40
  44.                 DrawText "Toggle the Three Outer Triangles (1-3)",10, 55
  45.        Flip
  46.                 If KeyHit(KEY_q) And maxiter < 10 maxiter = maxiter + 1
  47.                 If KeyHit(KEY_a) And maxiter > 0  maxiter = maxiter - 1
  48.                 If KeyHit(KEY_w) And midtriangleiter < 10 midtriangleiter = midtriangleiter + 1
  49.                 If KeyHit(KEY_s) And midtriangleiter > -1 midtriangleiter = midtriangleiter - 1
  50.                 If KeyHit(KEY_SPACE) HOLDSTILL = (HOLDSTILL = 0)
  51.                 If KeyHit(KEY_1) TRI1=(TRI1=0)
  52.                 If KeyHit(KEY_2) TRI2=(TRI2=0)
  53.                 If KeyHit(KEY_3) TRI3=(TRI3=0)
  54. Wend
  55.  
  56. Function recursivesubdivide(p1:point,p2:point,p3:point, iter:Int)
  57.         seed = seed + 1
  58.         If iter > maxiter
  59.                 Local poly:Float[]=[p1.x,p1.y,p2.x,p2.y,p3.x,p3.y]
  60.                 SeedRnd(seed)
  61.                 SetColor(Rand(255),Rand(255),Rand(255))
  62.                 DrawPoly poly
  63.                 Return 1
  64.         EndIf
  65.         Local midpoint1:point = Fmidpoint(p1,p2)
  66.         Local midpoint2:point = Fmidpoint(p2,p3)
  67.         Local midpoint3:point = Fmidpoint(p3,p1)
  68.         If TRI1
  69.                 'SetColor 0,midpoint1.x*COLORXFACTOR,0
  70.                 recursivesubdivide(p1,midpoint1,midpoint3,iter+1)
  71.         EndIf
  72.        
  73.         If TRI2
  74.                 'SetColor 0,255,0
  75.             recursivesubdivide(p2,midpoint2,midpoint1,iter+1)
  76.         EndIf
  77.        
  78.         If TRI3
  79.                 'SetColor 0,0,255
  80.             recursivesubdivide(p3,midpoint3,midpoint2,iter+1)
  81.         EndIf
  82.         If iter < midtriangleiter recursivesubdivide(midpoint1,midpoint2,midpoint3, iter+1)
  83. End Function
  84.        
  85. Type point
  86.         Field x#
  87.         Field y#
  88.         Field vx:Float
  89.         Field vy:Float
  90.         Field ox#,oy#
  91.         Method New()
  92.                 x = Rand(SCREENWIDTH)
  93.                 y = Rand(SCREENHEIGHT)
  94.                 vx = (Rand(-8,7)+RndFloat()) /2
  95.                 vy = (Rand(-8,7)+RndFloat()) /2
  96.         End Method
  97.         Method update()
  98.                 ox = x
  99.                 oy = y
  100.                 If inlimits(x+vx,0,SCREENWIDTH)
  101.                         x = x + vx
  102.                 Else
  103.                         vx = vx * -1
  104.                 EndIf
  105.                 If inlimits(y+vy,0,SCREENHEIGHT)
  106.                         y = y + vy
  107.                 Else
  108.                         vy=vy*-1
  109.                 EndIf
  110.         End Method
  111. End Type
  112.  
  113. Function Fmidpoint:point(in1:point,in2:point)
  114.         Local p:point = New point
  115.         p.x = (in1.x+in2.x)/2
  116.         p.y = (in1.y+in2.y)/2
  117.         Return p
  118. End Function
  119.  
  120. Function inlimits:Int(num:Float,lowerlimit:Float, upperlimit:Float)
  121.         If num > lowerlimit And num < upperlimit Return 1
  122. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal