Posted By


mikaelhc on 09/18/10

Tagged


Statistics


Viewed 101 times
Favorited by 0 user(s)

PenroseTiles.pbk


/ Published in: C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <languageVersion: 1.0;>
  2. /*
  3.   PenroseTiles.pbk
  4.  
  5.   Author: Syntopia (Mikael Hvidtfeldt Christensen)
  6.   This version: 18. september 2010
  7.  
  8.   Credits:
  9.   --------
  10.   This is a simple port of Tomasz Dobrowolski's (tomkh) "Procedural aperiodic fractals" EvalDraw script.
  11.   It originates from this FractalForums thread:
  12.   http://www.fractalforums.com/new-theories-and-research/procedural-aperiodic-fractals/
  13.  
  14.   Copyright & License:
  15.   --------------------
  16.   You can use this code for whatever purpose, but be sure to acknowledge the original author of the algorithm (Tomasz Dobrowolski).
  17. */
  18. kernel PenroseTiles
  19. < namespace : "Syntopia.Fractals";
  20. vendor : "Syntopia";
  21. version : 1;
  22. description : "Penrose Tile Generator.";
  23. >
  24.  
  25. {
  26. output pixel4 dst;
  27.  
  28. parameter int2 size
  29. <
  30. minValue:int2(100, 100); maxValue:int2(2000, 2000); defaultValue:int2(800, 600);
  31. >;
  32.  
  33. parameter float2 center
  34. <
  35. minValue:float2(-2.0, -1.0); maxValue:float2(2.0, 1.0); defaultValue:float2(0.8, 0.4);
  36. >;
  37.  
  38. parameter float zoom
  39. <
  40. minValue:0.0;maxValue:20.0;defaultValue:1.4;
  41. >;
  42.  
  43.  
  44. parameter int iterations
  45. <
  46. minValue:1; maxValue:50; defaultValue:10;
  47. >;
  48.  
  49. region generated()
  50. {
  51. return region(float4(0, 0, size.x, size.y));
  52. }
  53.  
  54. dependent float pi,sc,d1,d2,a1;
  55. dependent float2x2 m1,m2,m3,m4,m5;
  56. dependent float2 p1;
  57.  
  58. void evaluateDependents()
  59. {
  60. pi = 3.1415 ; // must be built-in somewhere
  61. sc = 2.0/(sqrt(5.0)-1.0); // inflation scale
  62.  
  63. // transformations constants
  64. d1 = tan(54.0*pi/180.0);
  65. d2 = tan(18.0*pi/180.0);
  66. a1 = .5/cos(36.0*pi/180.0);
  67. float a2 = (1.0+a1)*.5;
  68. float a3 = tan(36.0*pi/180.0)*a2;
  69. float cos1 = cos(144.0*pi/180.0)*sc;
  70. float sin1 = sin(144.0*pi/180.0)*sc;
  71. float cos2 = cos(108.0*pi/180.0)*sc;
  72. float sin2 = sin(108.0*pi/180.0)*sc;
  73.  
  74. m1 = float2x2(-sc,0.0, 0.0,sc);
  75. p1 = float2(-a2,-a3);
  76. m2= float2x2(cos1,-sin1,sin1,cos1);
  77. m3= float2x2(cos1,sin1,-sin1,cos1);
  78. m4= float2x2(-cos2,sin2,sin2,cos2);
  79. m5= float2x2(cos2,sin2,-sin2,cos2);
  80. }
  81.  
  82. pixel4 getValue(float2 z) {
  83. int triangleType = 0;
  84. for(int k=0; k<iterations; k++) {
  85. if (triangleType == 0) {
  86. if (1.0 - d1*z.y - z.x > 0.0) {
  87. z *= m1; z.x += sc;
  88. } else if (1.0 - d2*z.y - z.x > 0.0) {
  89. z += p1; z *= m2; triangleType = 1;
  90. } else {
  91. z.x-=(1.0+a1); z*= m3;
  92. }
  93. } else {
  94. if (d1*z.y - z.x > 0.0) {
  95. z*=m4; triangleType = 0;
  96. } else {
  97. z.x -= a1; z*= m5;
  98. }
  99. }
  100. }
  101. return (triangleType == 0) ? pixel4(218.0/256.0,213.0/256.0,118.0/256.0,1.0) : pixel4(74.0/256.0,101.0/256.0,166.0/256.0, 1.0) ;
  102. }
  103.  
  104. // Convert from screen space coords to model coords.
  105. float2 toModelSpace(float2 p) {
  106. // TODO: Some of this stuff could be precalculated as dependents...
  107. float x0 = center.x;
  108. float y0 = center.y;
  109. float xSpan = 1.0/exp(zoom);
  110. float x1 = x0 - xSpan*0.5;
  111. float x2 = x0 + xSpan*0.5;
  112. float ySpan = xSpan * (float(size.y) / float(size.x));
  113. float y1 = y0 - ySpan*0.5;
  114. float y2 = y0 + ySpan*0.5;
  115. return float2(x1, y1) + p * float2(xSpan / float(size.x), ySpan / float(size.y));
  116. }
  117.  
  118. void evaluatePixel()
  119. {
  120. // Simple anti-alias. aaSteps=1 corresponds to 3x3 sampling.
  121. int aaSteps = 1;
  122. float stepSize = 1.0/(3.0*float(aaSteps));
  123. dst = pixel4(0.0,0.0,0.0,0.0);
  124. for (int x = -aaSteps; x<=aaSteps; x++) {
  125. for (int y = -aaSteps; y<=aaSteps; y++) {
  126. dst += getValue(toModelSpace(outCoord()+float2(float(x)*stepSize,float(y)*stepSize)));
  127. }
  128. }
  129. dst /= float((aaSteps*2+1)*(aaSteps*2+1));
  130. }
  131. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.